0

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.

Bartek Banachewicz
  • 38,596
  • 7
  • 91
  • 135
Ezekiel
  • 167
  • 3
  • 11

1 Answers1

3

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.

Community
  • 1
  • 1
  • 1
    No, not injecting into zombie's page. Injecting into mocha before or after zombie loads the page but definitely before running the test. – Ezekiel Feb 24 '16 at 21:29
  • Okay, but you want to use function from another script in your test. If you put it in a function and then export that function and assign it to an import with the same name and then invoke it in your test script, it should work. – Sam Saint-Pettersen Feb 24 '16 at 21:46
  • 1
    I gained some insight from your approach. Yes, done that. – Ezekiel Feb 24 '16 at 21:48
  • But the problem you're having is its not running **before** your test? I found an asynchronous solution I can post to you, but it might be overkill for what you need. Or does it work okay? – Sam Saint-Pettersen Feb 24 '16 at 21:49
  • Post it anyways, It might turn out very useful another day – Ezekiel Feb 24 '16 at 21:51
  • For waiting for asynchronous code in a mocha test: http://stackoverflow.com/questions/32366730/run-async-code-before-entire-mocha-test – Sam Saint-Pettersen Feb 24 '16 at 21:52
  • 1
    Thank you, I'll check it out – Ezekiel Feb 24 '16 at 21:56