我正在使用Jest测试框架为我的JavaScript/ React 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:
• If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/en/ecmascript-modules for how to enable it.
• 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:
C:\Users\{user}\{project}\app\node_modules\react-date-picker\node_modules\react-calendar\dist\Calendar.css:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){.react-calendar {错误来自node_modules文件夹中的包,\react-date-picker。
因此,为了解决这个问题,我修改了jest.config.js文件,如下所示。
const esModules = [ 'react-date-picker'].join('|');
module.exports = {
verbose: true,
transformIgnorePatterns: [`/node_modules/(?!${esModules})`]
};这是"npm run unit:test“的命令。
"jest --watchAll --testPathPattern=tests/unit --config jest.config.js",当我再次运行测试时,我仍然得到相同的错误。我怎么才能修复它?
发布于 2021-04-20 01:53:50
这里有一个非常类似的情况,他通过将"allowJs": true添加到每个lib/app的tsconfig.spec.json的compilerOptions中(或者添加到根tsconfig.json中),并设置transformIgnorePatterns来修复它。
https://stackoverflow.com/questions/67166768
复制相似问题