I'm running unit tests using Jasmine (jasmine-es6 to be precise: https://github.com/vinsonchuong/jasmine-es6) and want to configure Jasmine to use absolute paths.
I configured Grunt to use 'app' path as a root directory for source files when browseryfing my scripts. After loading such browserified script in a browser it works fine. Here is part of my Gruntfile:
Gruntfile.js
newUIContentScript: {
options: {
transform: [
['babelify', {
presets: ['es2015', 'react'],
plugins: ["transform-object-rest-spread"],
babelrc: false,
sourceMaps: true
}]
],
browserifyOptions: {
paths: ["./app"] // enables using absolute paths with ./app as root
}
},
src: [
'app/scripts-old/*.js'
'app/scripts-new/*.js', // and new UI scripts
'app/common/*.js',
],
dest: '<%= config.srcBundle %>/newScriptBundle.js'
},
This way I'm able to use absolute paths in ES6 style imports in JS files:
app/scripts-old/application.js
import MyReducer from "scripts-new/Reducer";
import MyEventNames from "common/MyEventNames";
instead relative paths:
app/scripts-old/application.js
import MyReducer from "../scripts-new/Reducer";
import MyEventNames from "../common/MyEventNames";
Now when I run Jasmine tests I get the following error:
Error: Cannot find module 'scripts-new/Reducer'
at Module._resolveFilename (module.js:469:15)
at Function.Module._resolveFilename (<project>\node_modules\register-module\index.js:18:10)
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (<project>/spec/app/new-scripts/ReducerSpec.js:1:1)
at Module._compile (module.js:570:32)
at loader (<project>\node_modules\babel-register\lib\node.js:144:5)
at Object.require.extensions.(anonymous function) [as .js] (<project>\node_modules\babel-register\lib\node.js:154:7)
at Module.load (module.js:487:32)
I use default jasmine.json config file. I cannot see any configuration option to set up absolute paths the way I use them in the project. I don't want to fall-back to using relative paths, since absolute paths are making my imports unreadable (project structure is much more complicated, this is simplified example).
Are there any other approaches to testing ES6 imports? Is it possible to configure paths in some other way?