当我在 Jest 环境中运行 npm test 命令时,出现以下错误 Unexpected keyword 'new' 错误报告看起来像
● 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.
• 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/configuration
For information about custom transformations, see:
https://jestjs.io/docs/code-transformation
Details:
SyntaxError: /Users/steinko/development/TutorialKubernetesClient/JestConfig/cluster.test.js: Unexpected keyword 'new'. (5:11)
3 |
4 | it('should exist a kubeconfig', {
> 5 | expect(new kubernetesClient.KubeConfig()).not.toBeNull();
| ^
6 | })
at Parser._raise (node_modules/@babel/parser/src/parser/error.js:134:45)
at Parser.raiseWithData (node_modules/@babel/parser/src/parser/error.js:129:17)
at Parser.raise (node_modules/@babel/parser/src/parser/error.js:78:17)
at Parser.checkReservedWord (node_modules/@babel/parser/src/parser/expression.js:2386:12)
at Parser.parseIdentifierName (node_modules/@babel/parser/src/parser/expression.js:2336:12)
at Parser.parseIdentifier (node_modules/@babel/parser/src/parser/expression.js:2306:23)
at Parser.parseBindingAtom (node_modules/@babel/parser/src/parser/lval.js:299:17)
at Parser.parseMaybeDefault (node_modules/@babel/parser/src/parser/lval.js:370:25)
at Parser.parseAssignableListItem (node_modules/@babel/parser/src/parser/lval.js:346:23)
at Parser.parseBindingList (node_modules/@babel/parser/src/parser/lval.js:336:24)
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
sum.js | 100 | 100 | 100 | 100 |
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 failed, 1 passed, 2 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 1.069 s
<块引用>
失败的测试是这样的
const kubernetesClient = require('@kubernetes/client-node');
it('should exist a kubeconfig', {
expect(new kubernetesClient.KubeConfig()).not.toBeNull();
})
<块引用>
我已经通过以下方式设置了 Jest 环境:
npm install --save-dev jest
<块引用>
jest --init
<块引用>
使用以下配置 以下问题将帮助 Jest 为您的项目创建合适的配置
✔ Would you like to use Typescript for the configuration file? … no
✔ Choose the test environment that will be used for testing › jsdom (browser-like)
✔ Do you want Jest to add coverage reports? … yes
✔ Which provider should be used to instrument code for coverage? › v8
✔ Automatically clear mock calls and instances between every test? … yes
<块引用>
当我运行测试时出现以下错误
<块引用>测试 开玩笑
PASS ./sum.test.js
FAIL ./cluster.test.js
● 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.
• 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/configuration
For information about custom transformations, see:
https://jestjs.io/docs/code-transformation
Details:
SyntaxError: /Users/steinko/development/TutorialKubernetesClient/JestConfig/cluster.test.js: Unexpected keyword 'new'. (5:11)
3 |
4 | it('should exist a kubeconfig', {
> 5 | expect(new kubernetesClient.KubeConfig()).not.toBeNull();
| ^
6 | })
at Parser._raise (node_modules/@babel/parser/src/parser/error.js:134:45)
at Parser.raiseWithData (node_modules/@babel/parser/src/parser/error.js:129:17)
at Parser.raise (node_modules/@babel/parser/src/parser/error.js:78:17)
at Parser.checkReservedWord (node_modules/@babel/parser/src/parser/expression.js:2386:12)
at Parser.parseIdentifierName (node_modules/@babel/parser/src/parser/expression.js:2336:12)
at Parser.parseIdentifier (node_modules/@babel/parser/src/parser/expression.js:2306:23)
at Parser.parseBindingAtom (node_modules/@babel/parser/src/parser/lval.js:299:17)
at Parser.parseMaybeDefault (node_modules/@babel/parser/src/parser/lval.js:370:25)
at Parser.parseAssignableListItem (node_modules/@babel/parser/src/parser/lval.js:346:23)
at Parser.parseBindingList (node_modules/@babel/parser/src/parser/lval.js:336:24)
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
sum.js | 100 | 100 | 100 | 100 |
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 failed, 1 passed, 2 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 1.069 s
<块引用>
我需要做些什么来修复这个错误?
答案 0 :(得分:1)
it('should exist a kubeconfig', {
应该
it('should exist a kubeconfig', function() {