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)

Commits related to this issue

Most upvoted comments

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 .babelrcfile 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 and export.

Turns out I had disabled modules in my .babelrc in order to get tree shaking to work in webpack2.

"presets":  [
  ["es2015", {"modules": false}]
]

To work around it, I added a test specific env setting in my .babelrc to disable it.

"env": {
  "test": {
    "presets": [
      "es2015"
    ]
  }
}

I didn’t need to use transform-es2015-modules-commonjs.

I was able to fix this with

{
  "presets": [
    "react",
    ["es2015", {"modules": false, "loose": true}]
  ],
  "plugins": ["transform-object-rest-spread", "transform-decorators-legacy"],
  "env": {
    "test": {
      "plugins": ["transform-object-rest-spread", "transform-es2015-modules-commonjs", "transform-decorators-legacy"]
    }
  }
}

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 prevents babel-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

{
  "presets": ["es2015", "react", "stage-0"],
  "plugins": [
    "react-hot-loader/babel"
  ]
}

and I’ve installed everything mentioned in docs

"devDependencies": {
  "babel-jest": "^18.0.0",
  "babel-polyfill": "^6.20.0",
  "jest": "^18.1.0"
},

tried every solution in this thread (install transform-es2015-modules-commonjs plugin, --no-cache, …), just keep getting this error Unexpected 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}, {});

    const wrapper = shallow(element);
    expect(true);
    // var renderer = TestUtils.createRenderer();
    // renderer.render(BuyerCatalogDashboard, {}).toMatchSnapshot();
})

}); 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.

import React from 'react';
    ^^^^^^
    SyntaxError: Unexpected token import

package.json:

  "jest": {
    "automock": false,
    "moduleFileExtensions": ["js", "jsx"],
    "moduleDirectories": ["node_modules"],
    "unmockedModulePathPatterns": [
      "<rootDir>/node_modules/react/",
      "<rootDir>/node_modules/react-dom/"
    ]
  }

.babelrc

{
  "presets": [
    "es2015",
    "stage-1",
    "react"
  ],
  "plugins": [
    "transform-runtime"
  ]
}
$ node -v
v6.2.2

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, though react-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.

❯ npm run jest --no-cache -s
Using Jest CLI v14.0.0, jasmine2, babel-jest
 PASS  __tests__/sum-test.js (0.271s)
Running 1 test suite...Warning: ReactComponentTreeDevtool: Missing React element for debugID 2 when building stack
Warning: ReactComponentTreeDevtool: Missing React element for debugID 2 when building stack
Warning: ReactComponentTreeDevtool: Missing React element for debugID 2 when building stack
Warning: ReactComponentTreeDevtool: Missing React element for debugID 2 when building stack
 PASS  __tests__/CheckboxWithLabel-test.js (0.778s)
2 tests passed (2 total in 2 test suites, run time 1.9s)

Double edit

npm install --save react@15.3.0-rc.3 fixes that 2nd error

FWIW, 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! 😃