jest: Babel Jest Not Being used.
Seems the Babel-Jest Project hasn’t been touched in 4 months so Ill post this issue here.
Consider the following code:
var isClass = require('is-class');
export default () => {
let _container = [];
return {
/**
* Register the dependency under its own name.
*/
register(name, dependency) {
_container.push({name: name, dependency: dependency});
},
/**
*
*/
fetch(name) {
var toy = _container.filter(function(container){
return container.name === name;
});
console.log(toy);
},
get() {
return _container;
}
}
}
Consider the following test:
jest.dontMock('../src/toy_box/toy_box.js');
import ToyBox from '../src/toy_box/toy_box.js';
describe('registrater dependency', () => {
it ('should register a function to the container', () => {
var foo = function() {};
ToyBox.register('foo', foo);
expect(ToyBox.container.length).toEqual(1);
});
});
Now consider the following package.json:
{
"name": "toy-box",
"description": "Toy Box is a simple and efficient DI system based off symfony and other concepts.",
"version": "0.0.1",
"dependencies": {
"babel": "^6.1.18",
"babel-preset-es2015": "^6.1.18",
"babel-preset-stage-0": "^6.1.18",
"babelify": "7.2.0",
"browserify": "12.0.1",
"gulp": "3.9.0",
"vinyl-source-stream": "1.1.0",
"babel-plugin-lodash": "^1.0.1",
"is-class": "0.0.4",
"babel-runtime": "^6.2.0"
},
"devDependencies": {
"babel-cli": "^6.6.5",
"babel-core": "^6.2.1",
"babel-jest": "^6.0.1",
"babel-preset-es2015": "^6.6.0",
"babel-template": "^6.2.0",
"babel-traverse": "^6.2.0",
"babel-types": "^6.2.0",
"core-js": "^1.2.6",
"filesaver.js": "0.2.0",
"inherits": "^2.0.1",
"isarray": "0.0.1",
"jest-cli": "^0.8.2",
"lodash": "^3.10.1",
"rmmv-mrp-core": "0.0.14"
},
"scripts": {
"test": "jest"
},
"jest": {
"scriptPreprocessor": "./node_modules/babel-jest",
"testFileExtensions": [
"es6",
"js"
],
"moduleFileExtensions": [
"js",
"json",
"es6"
]
}
}
And the .babel-rc
{
"presets": [
"react"
"es2015",
],
"plugins": []
}
Finally the result from the test:
npm test
> toy-box@0.0.1 test /Users/AdamBalan/Documents/toy-box
> jest
Using Jest CLI v0.8.2, jasmine1
FAIL __tests__/toy_box-test.js
● Runtime Error
SyntaxError: Unexpected token import
npm ERR! Test failed. See above for more details.
Not sure how import is an unexpected token, also if you notice, babel-jest isnt being used in the compile step: Using Jest CLI v0.8.2, jasmine1
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 52 (12 by maintainers)
Oh, I think I know why this has happened: Jest uses caching, and when you added a
.babelrc
, it wasn’t recognized. Can you try again adding a.babelrc
file with the es2015 preset and running Jest with--no-cache
once? That should make sure that config changes are properly picked up 😃 Without the es2015 preset, import/export will definitely not work.I just ran into this same issue. I fixed it another way. The parsing error I was running into was on module related stuff like
import
andexport
.Turns out I had disabled
modules
in my.babelrc
in order to get tree shaking to work in webpack2.To work around it, I added a
test
specificenv
setting in my.babelrc
to disable it.I didn’t need to use
transform-es2015-modules-commonjs
.I was able to fix this with
Note the
transform-es2015-modules-commonjs
I also have this issue. It’s very mind boggling, because I have setup 2 repos and while first one works, second does not.
I was having a similar issue when I realized I had an
ignore
field in my Babel config to avoid building**/*.test.js
files. Turns out it also preventsbabel-jest
from doing its job on these files, which is why it seemed like it was not working at all.Same problem here…, my babelrc is exactly the same
and I’ve installed everything mentioned in docs
tried every solution in this thread (install
transform-es2015-modules-commonjs
plugin,--no-cache
, …), just keep getting this errorUnexpected token import
, still don’t know how to solve it.I just tried mocha, with
mocha --require babel-register
and it works without any other configuration. Not sure why jest doesn’t work.@jamedranoa I think I got things to work, now to make sure I can write meaningful tests. here is my configuration “jest”: { “transform”: { ** “^.+\.js$”: “<rootDir>/node_modules/babel-jest”, pretty sure this fixed issues** “.(ts|tsx|js)”: “<rootDir>/node_modules/ts-jest/preprocessor.js” }, “testRegex”: “(/react/tests/.*|\.(test|spec))\.(ts|tsx|js)$”, “moduleFileExtensions”: [ “ts”, “tsx”, “js” ], “unmockedModulePathPatterns”: [ “node_modules/react/”, “node_modules/enzyme/” ], “globals”: { “TS_CONFIG”: “tsconfig.json” } } tsconfig.json { “compilerOptions”: { “module”: “commonjs”, “target”: “ES6”, “noImplicitAny”: false, “jsx”: “react”, “sourceMap”: true, “moduleResolution”: “node”, “allowJs”: true, “allowSyntheticDefaultImports”: true } }
and this is my currently working tests, not sure how meaningful they are but its not complaining (yet)
import * as React from ‘react’; import TestUtils from ‘react-addons-test-utils’; import { createStore } from ‘redux’; import { expect } from ‘chai’; import { shallow } from ‘enzyme’;
import BuyerCatalogDashboard from ‘…/src/views/BuyerCatalogDashboard/BuyerCatalogDashboard’; import catalogReducer from ‘…/src/reducers/catalog_reducer’; describe(‘<BuyerCatalogDashboard />’, () => { it(‘renders joey’, () => { const store = createStore(catalogReducer); const element = React.createElement(BuyerCatalogDashboard, {store: store}, {});
}); describe(‘Addition’, () => { it(‘knows that 2 and 2 make 4’, () => { // expect(2 + 2).toBe(4); }); });
@jamedranoa thanks again for all your help, found the transform solution from this repo https://github.com/kulshekhar/ts-jest#known-limitations-for-ts-compiler-options
I have this same issue with jest-cli 14.0.
package.json:
.babelrc
How is babel-jest installed in such a way that it doesn’t have to be mentioned in any config?
Edit
I added
require('babel-polyfill')
, which is only mentioned as a dependency here, thoughreact-addons-test-utils
is imported but not listed as a dependency. However, running through the tutorial gets the tests to pass now, but there is an odd warning.Double edit
npm install --save react@15.3.0-rc.3
fixes that 2nd errorFWIW, my issue was simply that my setup framework script file name was “jest.config.js” in the root directory… and that’s the automatic configuration filename for Jest 🤕 🥇 Moving and renaming it to setup.js fixed it.
Yep, working on it as hard as I can! 😃