Why simple babel plugin breaks jest test?

358 views Asked by At

I wrote a simple plugin:

export default function() {
    return {
        visitor: {
            Program: {
                enter() {
                    console.log('Program Entered!');
                },
                exit() {
                    console.log('Program Exited!');
                },
            },
        },
    };
}

when I run webpack - it compiles my project successfully but when I run jest test - it fails with error:

Starting script: test
FAIL   unit-tests  src/common/App.test.js
● Test suite failed to run

Jest encountered an unexpected token

This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

Here's what you can do:
 • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
 • If you need a custom transformation specify a "transform" option in your config.
 • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/en/configuration.html

Details:

/../project/config/babel/myplugin.js:1
(function (exports, require, module, __filename, __dirname) { export default function() {
                                                              ^^^^^^

SyntaxError: Unexpected token export

if I remove my plugin from babel configuration - tests pass

why it complaints on my plugin? it is located outside jests rootDir - src.

what is wrong?

0

There are 0 answers