我正在从库中导入一些函数,这些函数已作为依赖项添加,但是当我运行测试时,jest 会抛出错误,提示“jest 遇到了意外的标记”
以下是错误详情
详情:
C:\Users\sbhuyar\ami-ui-booking-analysis_angular\node_modules\@vana\ami-ui-authentication\src\index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export { USER_LOGGED_OUT_ERROR, DEV_ENV } from './constants';
^^^^^^
SyntaxError: Unexpected token 'export'
> 1 | import { initialize, login } from '@vana/ami-ui-authentication';
| ^
2 | const domainSuffix = '.travel-intelligence.com';
3 | export const DEV_ENV = '__dev__';
4 |
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1728:14)
at Object.<anonymous> (src/api/env.js:1:1)
失败 src/app/login/loginController.test.js ● 测试套件运行失败
Jest encountered an unexpected token
Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.
Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.
By default "node_modules" folder is ignored by transformers.
Here's what you can do:
• If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
• If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
• 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.
由于 jest 默认情况下会忽略
node_modules
并且不会使用 Babel 对其进行转换,因此我认为必要的解决方案是手动确保 jest 使用 Babel 转换此模块,因为 Jest 本身不支持导入或导出。
我会参考这个答案:运行 Jest 测试时出现意外的令牌“导入”错误?