【bug】npm run test 报错: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. #18
我拉取了最新的 feat/add_unit_test 分支的代码,然后运行 pnpm i 更新了依赖,然后 pnpm run test 后发现报错:
F:\OpenSource\gbkill>pnpm run test
> gbkill@0.0.1-beta.6 test F:\OpenSource\gbkill
> jest --no-cache
No tests found, exiting with code 1
Run with `--passWithNoTests` to exit with code 0
In F:\OpenSource\gbkill
41 files checked.
testMatch: /__tests__/**/*.{tsx,ts} - 0 matches
testPathIgnorePatterns: \\node_modules\\ - 41 matches
testRegex: - 0 matches
Pattern: - 0 matches
ELIFECYCLE Test failed. See above for more details.
复现场景
运行 pnpm run test 后100% 复现。
尝试过的方案
看了报错信息里说 No tests found,看起来像是 jest 没有找到用例。但我看 jest.config.js 中有个 testMatch 配置:
F:\OpenSource\gbkill>pnpm run test
> gbkill@0.0.1-beta.6 test F:\OpenSource\gbkill
> jest --no-cache
FAIL __tests__/ui/Exit.test.tsx
● Test suite failed to run
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 op
tion.
You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/configuration
For information about custom transformations, see:
https://jestjs.io/docs/code-transformation
Details:
F:\OpenSource\gbkill\node_modules\.pnpm\ink@4.4.1_@types+react@18.2.48_react@18.2.0\node_modules\ink\build\index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export { default as render } from './render.js';
^^^^^^
SyntaxError: Unexpected token 'export'
1 | import React from 'react';
> 2 | import { Box, Text } from 'ink';
| ^
3 | interface IExit {
4 | code: number;
5 | }
at Runtime.createScriptFromCode (node_modules/.pnpm/jest-runtime@29.7.0/node_modules/jest-runtime/build/index.js:1505:14)
at Object.<anonymous> (src/ui/Exit.tsx:2:1)
at Object.<anonymous> (__tests__/ui/Exit.test.tsx:2:1)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 3.902 s
Ran all test suites.
ELIFECYCLE Test failed. See above for more details.
我的设备 & 环境
我的设备和环境配置可见 【bug】项目无法启动 · Issue #14 · AKclown/gbkill 中的描述,此处不再赘述。
ts版本(和项目配置一致):
jest版本(和项目配置一致):
现象
我拉取了最新的
feat/add_unit_test
分支的代码,然后运行pnpm i
更新了依赖,然后pnpm run test
后发现报错:复现场景
运行
pnpm run test
后100% 复现。尝试过的方案
看了报错信息里说
No tests found
,看起来像是jest
没有找到用例。但我看jest.config.js
中有个testMatch
配置:上网搜了一下,看到有人反馈: typescript - Jest does not find tests even if testMatch has matches - Stack Overflow
试着把
testMatch
换成testRegex
:没有以上的
No tests found
报错了,但是遇到了新的报错:看起来是编译环节出了问题。
我看了报错中提到的链接:
但我看你在
jest.config.js
中配置过ts-jest
,我看到GitHub
也有人反馈这个问题: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.[Bug]: · Issue #14169 · jestjs/jest但是他说他是换了
Jest
版本并且更新了一些其他依赖就好了。 目前我还是碰到这个问题,希望作者能看看。