“在未将Deest与Node.JS服务器应用程序一起使用时定义”

问题描述 投票:0回答:1

我有一个相对简单的Node.js应用程序,我试图在其中使用Jest进行单元测试(因为我在其他客户端项目上使用了Jest,并且希望在这里使用它的一致性)。为了尝试深入研究,我尝试创建一个精简的项目,其最低限度要重现该问题。

我的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"
  }
}

我在项目中没有陪同人员或其他任何东西。

➜  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.

UPDATE:jest-debug的输出如下:

➜  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.
node.js jestjs
1个回答
0
投票

由@jonrsharpe发现,我的根本问题是我使用的Node版本(10.13.0)。通过更新到版本12.18.0,该问题消失了。

© www.soinside.com 2019 - 2024. All rights reserved.