lundi 3 juillet 2017

JS Mocha test ES6 Babel

I'm beginner in JavaScript world and my first task is to cover an existing project with tests. We are using ES6/babel/webpack2 and it was agreed to choose Mocha/Chai as testing frameworks.

The first thing I did was creating .babelrc file in the root of the project:

{
  "presets": ["es2015", "react", "stage-0"]
}

Afterwards I added the following script to package.json:

"test": "mocha --compilers js:babel-core/register ./src/test/*.js"

My simple test file looks like:

import {arraysAreEquals} from 'dashboard-app/utils/common-utils'
import {assert} from 'chai'

describe("common-utils", function () {

    it ("arraysAreEquals", function () {
       var array1 = [1, 2, 3];
       var array2 = [1, 2, 3];
       assert.equal(arraysAreEquals(array1, array2), true);
    });
});

Running my test (npm run test) I got the following error:

Error: Cannot find module 'dashboard-app/utils/common-utils'
    at Function.Module._resolveFilename (module.js:469:15)
    at Function.Module._load (module.js:417:25)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)

This is quite expected as dashboard-app is just an alias, which is set in both webpack.config.dev.js and webpack.config.prod.js:

 resolve: {
        modules: [
            path.resolve(__dirname),
            "node_modules"
        ],
        alias: {
            'dashboard-app/config': 'src/config',
            'dashboard-app/utils': 'src/utils',
            .......

So, basically my question is how should I amend test-script in package.json in order to bind my webpack.config.dev.js (it's suitable for test env I believe)? The following didn't work for me (neither of 2 variants):

"test": "NODE_ENV=dev mocha --compilers js:babel-core/register ./src/test/*.js"

"test": "NODE_ENV=dev --env=dev mocha --compilers js:babel-core/register ./src/test/*.js"

Aucun commentaire:

Enregistrer un commentaire