为什么简单的babel插件无法通过测试?

时间:2018-09-19 12:08:54

标签: jestjs babel babel-jest babel-plugin

我写了一个简单的插件:

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

当我运行webpack时-它成功编译了我的项目 但是当我进行笑话测试时-它失败并显示错误:

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之外。

怎么了?

0 个答案:

没有答案