1

I'm having trouble integrating unit tests in a particular folder of my project.

My architecture is as follows:

|_ shared
|_ job-board
|_ admin-panel

job_board and admin-panel each contain a symlink pointing to shared, at ./client/src/shared, and each contain a node_modules folder with babel-eslint installed. shared does NOT contain a node_modules folder, and relies on the 2 other folder's node_modules.

With this architecture, I've managed to configure all the tools I'm using (Webpack, eslint, eslint-loader, ... but Jest).

The problem is, when I create a *.test.js file in job_board/* or admin_panel/*, babel-jest is applied correctly on this file, but when I create this file in shared/*, babel-jest isn't applied (as babel-jest is not available in shared), as the following error proves:

FAIL  ..\shared\utils\tests\StringUtils.test.js
● Test suite failed to run
SyntaxError: Unexpected token import

Here is my jest-config.json file:

{
  "moduleFileExtensions": [
    "js"
  ],
  "moduleNameMapper": {
    "\\.(css|scss)$": "identity-obj-proxy"
  },
  "modulePaths": [
    "<rootDir>/client"
  ],
  "testPathDirs": [
    "<rootDir>",
    "<rootDir>/../shared"
  ],
  "testRegex": "(\\.|/)test\\.jsx?$"
}

(Note that I had to add "<rootDir>/../shared" to testPathDirs so that *.test.js files could be found)

And my .babelrc file:

{
  "plugins": [
    "transform-decorators-legacy",
    "transform-object-rest-spread"
  ],
  "presets": [
    ["es2015", { "modules": false }],
    "react",
    "stage-0"
  ],
  "env": {
    "development": {
      "presets": [
        "react-hmre"
      ]
    },
    "test": {
      "plugins": ["transform-es2015-modules-commonjs"]
    }
  }
}

Finally, I run my tests with: node --harmony_proxies node_modules/jest-cli/bin/jest.js --config ./jest-config.json

Am I missing something out, or is this really a lack of a feature giving the ability to provide a "root" for babel-eslint or something?

I also opened an issue on the Jest repo, but it has been closed. So here I am!

Jest configuration pasted above.

jest-cli: 18.1.0

node: 5.6.0

npm: 3.6.0

OS: Windows 10

Andreas Köberle
  • 106,652
  • 57
  • 273
  • 297
adrienharnay
  • 795
  • 2
  • 9
  • 27

2 Answers2

0

Looks like you are having a similar issue to this person.

Have you tried the following in your .babelrc file?

"env": {
  "start": {
    "presets": [
      "react-hmre"
    ]
  },
  "test": {
    "presets": ["es2015", "react", "stage-0"],
    "plugins": ["transform-es2015-modules-commonjs"]
  }
}
Community
  • 1
  • 1
NFab
  • 386
  • 4
  • 6
  • Yes I tried, no change. Because I am using webpack2, I disable the transformation of modules to commonJS in dev/prod, but enable it back in test env (see my .babelrc). – adrienharnay Feb 09 '17 at 18:05
0

For those interested with a workaround, I came up with a solution while going through the source files of babel-jest. Just create a .babelrc at the common root of your repos, extending the real one. So in my case:

{
  "extends": "./admin-panel/.babelrc"
}
adrienharnay
  • 795
  • 2
  • 9
  • 27