1

我写了一个简单的插件:

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

当我运行 webpack - 它成功编译了我的项目但是当我运行 jest test 时 - 它失败并出现错误:

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

如果我从 babel 配置中删除我的插件 - 测试通过

为什么它抱怨我的插件?它位于 jests rootDir - src 之外。

怎么了?

4

0 回答 0