简体   繁体   中英

How to run an external script in mocha

This might be a silly question, but I've got to ask the community anyways.

I am using Zombie.js and Mocha for my test and I have an external script named:external.js.

// external.js

module.exports = "console.log('hey');";

I would like to load this external script into the mocha test (not Zombie.js's opened browser) and run it before running the test.

var myScript = require('../external.js');

describe('test script load', function() {
  browser.visit('www.example.com', done);

  // I want to load the external script here and run it before perfoming the test

  it('loads script', function (done) {
    browser.assert.success();
    done();
  });
});

I've tried several methods like creating a script tag and inserting my external script but seems to work when in HTML (because it works well in Zombie's browser) but I want the script before running the test.

Do you mean like injecting a script into the page zombie.js is loading? See: Injecting javascript into zombie.js .

If not that, you could try something like this:

external.js:

 function doSomething() {
    console.log('hi there!');
 } 

 module.exports = doSomething;

mocha.js:

 var doSomething = require('./external.js');

 your test....

 doSomething();

 your test continued...

That should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM