&引用;“描述未定义”;将Jest与Node.JS服务器应用程序一起使用时

&引用;“描述未定义”;将Jest与Node.JS服务器应用程序一起使用时,node.js,jestjs,Node.js,Jestjs,我有一个相对简单的Node.js应用程序,我试图在其中使用Jest进行单元测试(因为我在其他客户机项目上使用它,并且希望在这里使用它的一致性)。为了弄清问题的症结所在,我尝试创建一个精简的项目,尽可能少地重现问题 My package.json在这里: { "name": "jest-sample", "version": "0.2.0", "private": true, "description": "NodeJS backend to the Service Manager

我有一个相对简单的Node.js应用程序,我试图在其中使用Jest进行单元测试(因为我在其他客户机项目上使用它,并且希望在这里使用它的一致性)。为了弄清问题的症结所在,我尝试创建一个精简的项目,尽可能少地重现问题

My package.json在这里:

{
  "name": "jest-sample",
  "version": "0.2.0",
  "private": true,
  "description": "NodeJS backend to the Service Manager UI",
  "author": "Me",
  "license": "SEE LICENSE IN LICENSE",
  "deprecated": false,
  "main": "src/js/app.js",
  "type": "module",
  "engines": {
    "node": "^8.15.1"
  },
  "scripts": {
    "test": "npx jest"
  },
  "devDependencies": {
    "jest": "26.0.1",
    "supertest": "4.0.2"
  },
  "jest": {
    "testEnvironment": "node"
  }
}
我在项目中没有eslint或其他任何东西

➜  jest-sample ls -a
.                 ..                __tests__         node_modules      package-lock.json package.json
测试也很简单:

describe('Sample Test', function() {
    it('should be', function() {
        expect(true).toBe(true);
    })
});
然而,当我开玩笑时,我得到:

➜  jest-sample npx jest
 FAIL  __tests__/sample.spec.js
  ● Test suite failed to run

    ReferenceError: describe is not defined

    > 1 | describe('Sample Test', function() {
        | ^
      2 |     it('should be', function() {
      3 |         expect(true).toBe(true);
      4 |     })

      at Object.<anonymous> (__tests__/sample.spec.js:1:1)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        2.562 s
Ran all test suites.
➜  jest示例npx jest
失败_测试_/sample.spec.js
● 测试套件无法运行
ReferenceError:未定义描述
>1 |描述('样本测试',功能(){
| ^
2 |它('should be',function(){
3 |期望(正确)。托比(正确);
4 |     })
在对象上。(\uu tests_uuu/sample.spec.js:1:1)
测试套件:1个失败,共1个
测试:共0项
快照:共0个
时间:2.562秒
运行所有测试套件。
更新: jest调试的输出如下:

➜  jest-sample npx jest --debug
{
  "configs": [
    {
      "automock": false,
      "cache": true,
      "cacheDirectory": "/private/var/folders/4m/6tytrddj3g90b8rmbrccw3lm0000gn/T/jest_dx",
      "clearMocks": false,
      "coveragePathIgnorePatterns": [
        "/node_modules/"
      ],
      "cwd": "/Users/rbair/Projects/gitlab/iot/infrastructure/service-manager/jest-sample",
      "detectLeaks": false,
      "detectOpenHandles": false,
      "errorOnDeprecated": false,
      "extraGlobals": [],
      "forceCoverageMatch": [],
      "globals": {},
      "haste": {
        "computeSha1": false,
        "throwOnModuleCollision": false
      },
      "moduleDirectories": [
        "node_modules"
      ],
      "moduleFileExtensions": [
        "js",
        "json",
        "jsx",
        "ts",
        "tsx",
        "node"
      ],
      "moduleNameMapper": [],
      "modulePathIgnorePatterns": [],
      "name": "d652708218018aa212b40c53d7999ccc",
      "prettierPath": "prettier",
      "resetMocks": false,
      "resetModules": false,
      "restoreMocks": false,
      "rootDir": "/Users/rbair/Projects/gitlab/iot/infrastructure/service-manager/jest-sample",
      "roots": [
        "/Users/rbair/Projects/gitlab/iot/infrastructure/service-manager/jest-sample"
      ],
      "runner": "jest-runner",
      "setupFiles": [],
      "setupFilesAfterEnv": [],
      "skipFilter": false,
      "snapshotSerializers": [],
      "testEnvironment": "/Users/rbair/Projects/gitlab/iot/infrastructure/service-manager/jest-sample/node_modules/jest-environment-node/build/index.js",
      "testEnvironmentOptions": {},
      "testLocationInResults": false,
      "testMatch": [
        "**/__tests__/**/*.[jt]s?(x)",
        "**/?(*.)+(spec|test).[tj]s?(x)"
      ],
      "testPathIgnorePatterns": [
        "/node_modules/"
      ],
      "testRegex": [],
      "testRunner": "/Users/rbair/Projects/gitlab/iot/infrastructure/service-manager/jest-sample/node_modules/jest-jasmine2/build/index.js",
      "testURL": "http://localhost",
      "timers": "real",
      "transform": [
        [
          "^.+\\.[jt]sx?$",
          "/Users/rbair/Projects/gitlab/iot/infrastructure/service-manager/jest-sample/node_modules/babel-jest/build/index.js",
          {}
        ]
      ],
      "transformIgnorePatterns": [
        "/node_modules/"
      ],
      "watchPathIgnorePatterns": []
    }
  ],
  "globalConfig": {
    "bail": 0,
    "changedFilesWithAncestor": false,
    "collectCoverage": false,
    "collectCoverageFrom": [],
    "coverageDirectory": "/Users/rbair/Projects/gitlab/iot/infrastructure/service-manager/jest-sample/coverage",
    "coverageProvider": "babel",
    "coverageReporters": [
      "json",
      "text",
      "lcov",
      "clover"
    ],
    "detectLeaks": false,
    "detectOpenHandles": false,
    "errorOnDeprecated": false,
    "expand": false,
    "findRelatedTests": false,
    "forceExit": false,
    "json": false,
    "lastCommit": false,
    "listTests": false,
    "logHeapUsage": false,
    "maxConcurrency": 5,
    "maxWorkers": 3,
    "noStackTrace": false,
    "nonFlagArgs": [],
    "notify": false,
    "notifyMode": "failure-change",
    "onlyChanged": false,
    "onlyFailures": false,
    "passWithNoTests": false,
    "projects": [],
    "rootDir": "/Users/rbair/Projects/gitlab/iot/infrastructure/service-manager/jest-sample",
    "runTestsByPath": false,
    "skipFilter": false,
    "testFailureExitCode": 1,
    "testPathPattern": "",
    "testSequencer": "/Users/rbair/Projects/gitlab/iot/infrastructure/service-manager/jest-sample/node_modules/@jest/test-sequencer/build/index.js",
    "updateSnapshot": "new",
    "useStderr": false,
    "watch": false,
    "watchAll": false,
    "watchman": true
  },
  "version": "26.0.1"
}
 FAIL  __tests__/sample.spec.js
  ● Test suite failed to run

    ReferenceError: describe is not defined

    > 1 | describe('Sample Test', function() {
        | ^
      2 |     it('should be', function() {
      3 |         expect(true).toBe(true);
      4 |     })

      at Object.<anonymous> (__tests__/sample.spec.js:1:1)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        0.523 s
Ran all test suites.
➜  jest示例npx jest—调试
{
“配置”:[
{
“自动模拟”:错误,
“缓存”:正确,
“缓存目录”:“/private/var/folders/4m/6tytrddj3g90b8RMBCW3lm0000gn/T/jest_dx”,
“clearMocks”:错误,
“coveragePathIgnorePatterns”:[
“/node_modules/”
],
“cwd”:“/Users/rbair/Projects/gitlab/iot/infrastructure/service manager/jest sample”,
“detectLeaks”:错误,
“detectOpenHandles”:错误,
“errorOnDeprecated”:错误,
“全球外”:[],
“强制覆盖匹配”:[],
“全球”:{},
“匆忙”:{
“computeSha1”:false,
“throwOnModuleCollision”:错误
},
“模块目录”:[
“节点_模块”
],
“moduleFileExtensions”:[
“js”,
“json”,
“jsx”,
“ts”,
“tsx”,
“节点”
],
“moduleNameMapper”:[],
“modulePathIgnorePatterns”:[],
“名称”:“d652708218018aa212b40c53d7999ccc”,
“prettierPath”:“prettier”,
“重置模拟”:错误,
“重置模块”:错误,
“restoreMocks”:假,
“rootDir”:“/Users/rbair/Projects/gitlab/iot/infrastructure/service manager/jest sample”,
“根”:[
“/Users/rbair/Projects/gitlab/iot/infrastructure/service manager/jest sample”
],
“跑步者”:“开玩笑的跑步者”,
“设置文件”:[],
“SetupFileAfterEnv”:[],
“skipFilter”:错误,
“快照序列化程序”:[],
“测试环境”:“/Users/rbair/Projects/gitlab/iot/infrastructure/service manager/jest sample/node_modules/jest environment node/build/index.js”,
“测试环境选项”:{},
“testLocationInResults”:错误,
“测试匹配”:[
“***/\\\\\/***.[jt]s?(x)”,
“***/?(*)+(规范测试)。[tj]s?(x)”
],
“testPathIgnorePatterns”:[
“/node_modules/”
],
“testRegex”:[],
“testRunner”:“/Users/rbair/Projects/gitlab/iot/infrastructure/service manager/jest sample/node_modules/jest-jasmine2/build/index.js”,
“testURL”:”http://localhost",
“计时器”:“真实的”,
“转变”:[
[
“^.+\.[jt]sx?$”,
“/Users/rbair/Projects/gitlab/iot/infrastructure/service manager/jest sample/node_modules/babel jest/build/index.js”,
{}
]
],
“transformIgnorePatterns”:[
“/node_modules/”
],
“watchPathIgnorePatterns”:[]
}
],
“全球配置”:{
“保释”:0,
“ChangedFileWithAsentor”:false,
“保险范围”:虚假,
“CollectionCoverage from”:[],
“coverage目录”:“/Users/rbair/Projects/gitlab/iot/infrastructure/service manager/jest sample/coverage”,
“Coverage Provider”:“babel”,
“搬运工”:[
“json”,
“文本”,
“lcov”,
“三叶草”
],
“detectLeaks”:错误,
“detectOpenHandles”:错误,
“errorOnDeprecated”:错误,
“扩展”:错误,
“findRelatedTests”:错误,
“强制退出”:错误,
“json”:false,
“lastCommit”:false,
“列表测试”:错误,
“logHeapUsage”:错误,
“maxConcurrency”:5,
“maxWorkers”:3,
“noStackTrace”:false,
“非标签”:[],
“通知”:错误,
“notifyMode”:“故障更改”,
“仅更改”:错误,
“唯一的失败”:错误,
“passWithNoTests”:false,
“项目”:[…],
“rootDir”:“/Users/rbair/Projects/gitlab/iot/infrastructure/service manager/jest sample”,
“runTestsByPath”:false,
“skipFilter”:错误,
“testFailureExitCode”:1,
“testPathPattern”:“”,
“testSequencer”:“/Users/rbair/Projects/gitlab/iot/infrastructure/service manager/jest sample/node_modules/@jest/test sequencer/build/index.js”,
“更新快照”:“新建”,
“useStderr”:false,
“手表”:假的,
“watchAll”:错误,
“守望者”:真的吗
},
“版本”:“26.0.1”
}
失败_测试_/sample.spec.js
● 测试套件无法运行
ReferenceError:未定义描述
>1 |描述('样本测试',功能(){
| ^
2 |它('should be',function(){
3 |期望(正确)。托比(正确);
4 |     })
at对象。(uuu测试uu/sample.spec.js:1:1)
测试套件:1个失败,共1个
测试:共0项
快照:共0个
时间:0.523秒
运行所有测试套件。

正如@jornsharpe发现的,我的根本问题是我使用的节点版本(10.13.0)更新到12.18.0版后,问题就消失了。

你为什么要使用
npx jest
?这也是你的
软件包中的问题。json
,请给出一个答案。我想使用
npm install
安装的jest,而不需要全局安装。我对jest来说是新手,对npm来说也是新手,所以也许有更好的方法而不是npx?如果这个示例对您有效,那么我的环境中肯定有其他原因导致了问题。有什么想法会导致这个问题吗?
“test”:“jest”
已经使用了
jest