首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我在Jest测试中得到一个意外的关键字“new”?

为什么我在Jest测试中得到一个意外的关键字“new”?
EN

Stack Overflow用户
提问于 2021-07-09 13:16:04
回答 1查看 144关注 0票数 0

当我在Jest中运行npm测试命令时,我得到以下错误意外关键字‘

代码语言:javascript
复制
● 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

失败的测试如下所示

代码语言:javascript
复制
const kubernetesClient = require('@kubernetes/client-node');

it('should exist a kubeconfig', {
    expect(new kubernetesClient.KubeConfig()).not.toBeNull();
})

我已经通过以下方式设置了Jest环境:

  1. 安装喷气机

代码语言:javascript
复制
npm install --save-dev jest

  1. 并通过

生成基本配置文件。

代码语言:javascript
复制
jest --init

具有以下配置的

将有助于Jest为您的项目创建合适的配置

代码语言:javascript
复制
✔ 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

当我运行测试时,当我在测试中出错时,

代码语言:javascript
复制
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

,我要做什么才能修复这个错误?

EN

回答 1

Stack Overflow用户

发布于 2021-07-09 13:22:09

代码语言:javascript
复制
it('should exist a kubeconfig', {

应该是

代码语言:javascript
复制
it('should exist a kubeconfig', function() {
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68317442

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档