Jest: babel-jest not working with es2015

Created on 22 Apr 2016  ·  4Comments  ·  Source: facebook/jest

I'm currently doing the basic sum test using es2015 but it does not seem to be working. Here is my sum.js

let sum = (value1, value2) => (
    value1 + value2
);

export default sum;

Here is the sum-test.js

jest.unmock('../sum');

describe('sum', function() {
    it('adds 1+2 to equal 3', function () {
        var sum = require('../sum');
        expect(sum(1,2)).toBe(3);
    });
});

When I run jest, (using npm test), I seem to be getting this error:

● sum › it adds 1+2 to equal 3

  • SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode in file 'sum.js'.

Make sure your preprocessor is set up correctly and ensure your 'preprocessorIgnorePatterns' configuration is correct: http://facebook.github.io/jest/docs/api.html#preprocessorignorepatterns-array-string
If you are currently setting up Jest or modifying your preprocessor, try jest --no-cache.
Preprocessor: node_modules/babel-jest.
Make sure your '.babelrc' is set up correctly, for example it should include the 'es2015' preset.
Jest tried to the execute the following preprocessed code:
let sum = (value1, value2) =>
value1 + value2;

export default sum;

at Object.eval (__tests__/sum-test.js:6:19) at attemptSync (node_modules/jest-jasmine2/vendor/jasmine-2.3.4.js:1791:24) at QueueRunner.run (node_modules/jest-jasmine2/vendor/jasmine-2.3.4.js:1779:9) at QueueRunner.execute (node_modules/jest-jasmine2/vendor/jasmine-2.3.4.js:1764:10) at Spec.Env.queueRunnerFactory (node_modules/jest-jasmine2/vendor/jasmine-2.3.4.js:629:35) at Spec.execute (node_modules/jest-jasmine2/vendor/jasmine-2.3.4.js:355:10)

Looks like others are having the same problem
Stackoverflow
What could be wrong?

Most helpful comment

i was able to reproduce it inside examples/getting_started when i used your code.
here is what solved it for me:

npm install --save-dev babel-jest babel-preset-es2015
echo '{"presets": ["es2015"]}' >> .babelrc
jest

All 4 comments

hey @chemjournal
what is your node version and what is inside your .babelrc?

also, do you see babel-jest when you run jest?

$  jest
Using Jest CLI v11.0.2, jasmine2, babel-jest
PASS  __tests__/CheckboxWithLabel-test.js (0.494s)
1 test passed (1 total in 1 test suite, run time 1.209s)

hi @dmitriiabramov i'm using node 5.5.0 and my .babel-rc has

{
"presets": ["es2015"]
}

Yes I do see babel-jest, Using Jest CLI v11.0.2, jasmine2, babel-jest

Btw i've also noticed that the sample CheckboxWithLabel on the jest website doesn't work either. But that seems to be a different error (not related to babel). I'll open a different issue just so that we can focus on them individually.

Thanks for your help!

i was able to reproduce it inside examples/getting_started when i used your code.
here is what solved it for me:

npm install --save-dev babel-jest babel-preset-es2015
echo '{"presets": ["es2015"]}' >> .babelrc
jest

That's strange because I had the exact same presets loaded. It turned out that jest.autoMockOff() was causing the test to fail. Its working now! Thanks for your help!

btw I've created #932 for the problem in with the ChecklistboxWithLabel sample code error.

Was this page helpful?
0 / 5 - 0 ratings