1

Recently I am trying to use jasmine-node to run unit test from console.

The problem is that my unit test test.js file is in another directory of the source source.js file.

How do I make sure my unit test test.js file can refer to the method of the source.js file?

Folder struture

spec\test.js
js\source.js
Dave Newton
  • 158,873
  • 26
  • 254
  • 302
Adam Lee
  • 24,710
  • 51
  • 156
  • 236

1 Answers1

1

You'd have to use some module dependency tool, like browserify, requireJS or webpack.

You'd need to export your code's variables:

//js/source.js
var SourceComponent = 'Hello'; // what you want to export
module.exports = SourceComponent; // export that variable

You can now include your source by calling require('js/source');

Setting this up can take a while, as you often need to make sure all dependencies are tied together, which can take a long time with large projects.

We used the webpack, which has helped, as you can “emulate” it being run in a script tag.

In webpack we imported source files like follows: require(script!js/source.js'); which allowed us to quickly get something working.

Daniel Apt
  • 2,468
  • 1
  • 21
  • 34