public icon indicating copy to clipboard operation
public copied to clipboard

Babel 7 node_modules transform with Jest

Open CarloHagens opened this issue 7 years ago • 5 comments

Issue description or question

We use an npm package "spin.js" that contains some ES6 javascript. We are using jest to run our tests. In our package.json we use a jest section with a transform ignore pattern to include "spin.js" in the transform that is carried out. We are using babel 7 with Jest 23.

Jest runs our unit tests without issue doing the babel compile on "spin.js". However attempting to do the same with Wallaby we followed the instructions at https://wallabyjs.com/docs/integration/node.html in the ES6 node modules section. With this configuration (adjusted for @babel/register) we are not having any success in getting wallaby to make jest transform "spin.js".

Any advice would be appreciated. I've included all configuration files and package versions below.

Wallaby.core version

1.0.652

Package versions:

"@babel/cli": "7.2.3",
"@babel/core": "7.2.2",
"@babel/preset-env": "7.3.1",
"@babel/preset-react": "7.0.0",
"babel-core": "7.0.0-bridge.0",
"babel-eslint": "10.0.1",
"babel-jest": "23.6.0",
"babel-loader": "8.0.5"
"jest": "23.6.0",
"jest-cli": "23.6.0",

Wallaby.js configuration file

module.exports = wallaby => ({
    files: [
        { pattern: 'jest/*.js', instrument: false, load: true },
        { pattern: 'package.json', instrument: false, load: true },
        'app/**/*.js',
        '!app/**/__tests__/*.js'
    ],
    tests: [
        'app/**/__tests__/*.js'
    ],
    env: {
        type: 'node',
        runner: 'node'
    },
    compilers: {
        'app/**/*.js': wallaby.compilers.babel()
    },
    testFramework: 'jest',
    bootstrap: () => {
        require('@babel/register')({
            ignore: [],
            only: [/spin.js/]
        });
        const config = require('./package.json').jest;
        wallaby.testFramework.configure(config);
    }
});

package.json jest section

"jest": {
    "transformIgnorePatterns": [
      "node_modules/(?!(spin.js)/)"
    ]
}

babel.config.js

module.exports = {
    presets: [
        [
            '@babel/preset-env',
            {
                useBuiltIns: 'entry'
            }
        ],
        '@babel/preset-react'
    ]
};

Code editor or IDE name and version

Visual Studio Code v1.30.2

OS name and version

Windows 10

CarloHagens avatar Feb 08 '19 17:02 CarloHagens

Using Babel register will not work with Jest, as it has its own module import mechanism.

All you need to do to get it working is to include your babel.config.js config to the files list, here is the config that should work for you:

module.exports = wallaby => ({
    files: [
        { pattern: 'jest/*.js', instrument: false, load: true },
        { pattern: 'package.json', instrument: false, load: true },
        { pattern: 'babel.config.js, instrument: false, load: true },
        'app/**/*.js',
        '!app/**/__tests__/*.js'
    ],
    tests: [
        'app/**/__tests__/*.js'
    ],
    env: {
        type: 'node',
        runner: 'node'
    },
    compilers: {
        'app/**/*.js': wallaby.compilers.babel()
    },
    testFramework: 'jest'
});

ArtemGovorov avatar Feb 08 '19 23:02 ArtemGovorov

@ArtemGovorov can you please document how to properly use a babel.config.js in your documentation? I expected the file to be autodetected like .babelrc used to be.

ProLoser avatar Jun 13 '19 01:06 ProLoser

@ProLoser Is there any specific case you are referring to when babel.config.js is handled differently from .babelrc? From what I can see, babel.config.js is handled by Wallaby the exact same way as .babelrc.

ArtemGovorov avatar Jun 13 '19 01:06 ArtemGovorov

Didn't you say you have to add { pattern: 'babel.config.js, instrument: false, load: true }, to your files? You don't have to do this for .babelrc files.

ProLoser avatar Jun 13 '19 20:06 ProLoser

Didn't you say you have to add { pattern: 'babel.config.js, instrument: false, load: true }, to your files?

If you have transformIgnorePatterns that include some files that are not listed in Wallaby files (like some node modules in this specific case), file needs to be included for Jest (Wallaby doesn't need it).

You don't have to do this for .babelrc files.

You do, if you're facing the same scenario as described above.

ArtemGovorov avatar Jun 13 '19 23:06 ArtemGovorov