I'm jumping in on an older project, and it uses mocha for unit tests. Package.json has "npm run test" to kick off the tests, and I know that unit tests used to be able to work. However, running "npm run test" now throws an unexpected identifier error. On the first file tested, an error is thrown on "import ", such as import chai from 'chai'
. From what I found, this is because babel isn't being used.
The full test line is:
"test": "mocha --compilers js:./test/compiler.js ./test/components/patient/*.js ./test/components/patient-list/patient-list-action-spec.js ./test/reducer/*.js ./test/utils/*.js ",
compiler.js looks like:
'use strict'
var babel = require('babel-core/register');
function noop() {
return null;
}
require.extensions['.scss'] = noop;
require.extensions['.css'] = noop;
I also tried:
"test": "mocha --require babel-core/register ./test/components/patient/*.js ./test/components/patient-list/patient-list-action-spec.js ./test/reducer/*.js ./test/utils/*.js ",
This is an older project, and as such it's using old versions of many libraries:
package.json:
{
"name": "family-centered-rounds-ui",
"version": "1.0.0",
"description": "",
"main": "index.jsx",
"scripts": {
"test": "mocha --compilers js:./test/compiler.js ./test/components/patient/*.js ./test/components/patient-list/patient-list-action-spec.js ./test/reducer/*.js ./test/utils/*.js ",
"start": "webpack-dev-server",
"build": "webpack --config webpack-production.config.js -p"
},
"keywords": [],
"license": "ISC",
"dependencies": {
"babel-runtime": "^6.26.0",
"bootstrap": "^4.1.3",
"co": "^4.6.0",
"css-loader": "^0.25.0",
"extract-text-webpack-plugin": "^1.0.0",
"immutable": "^3.8.1",
"jquery": "^1.9.1",
"jsdom": "^11.6.2",
"prop-types": "^15.6.2",
"react": "^15.0.0",
"react-dom": "^15.0.0",
"react-sortable-hoc": "^0.8.3",
"reactstrap": "^6.4.0",
"redux": "^3.5.2",
"redux-logger": "^2.6.1",
"strip": "^3.0.0",
"strip-loader": "^0.1.2",
"style-loader": "^0.13.2"
},
"devDependencies": {
"babel": "^6.5.2",
"babel-cli": "^6.10.1",
"babel-core": "^6.10.4",
"babel-loader": "^6.2.4",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-plugin-transform-runtime": "^6.12.0",
"babel-polyfill": "^6.9.1",
"babel-preset-es2015": "^6.9.0",
"babel-preset-es2015-without-strict": "0.0.3",
"babel-preset-react": "^6.11.1",
"babel-register": "^6.9.0",
"chai": "^3.5.0",
"chai-immutable": "^1.6.0",
"deep-freeze": "0.0.1",
"eslint": "^3.17.1",
"eslint-config-airbnb": "^14.1.0",
"eslint-plugin-import": "^2.2.0",
"eslint-plugin-jsx-a11y": "^4.0.0",
"eslint-plugin-react": "^6.10.0",
"file-loader": "^0.9.0",
"http-server": "^0.9.0",
"mocha": "^2.5.3",
"react-hot-loader": "^1.3.0",
"react-redux": "5.0.0",
"redux-thunk": "^2.1.0",
"sinon": "^1.17.4",
"thunk": "0.0.1",
"webpack": "^1.13.2",
"webpack-dev-server": "^1.14.1"
}
}
To me, it looks like mocha should have been using babel, so I'm not sure why it's not.
I don't know if node version makes a difference. I have v10.15.3