javascriptmocha.jszombie.jsexternal-script

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.


Solution

  • 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.