"describe is not defined"将 Jest 与 Node.JS 服务器应用程序一起使用时



我有一个相对简单的 Node.js 应用程序,我在其中尝试使用 Jest 进行单元测试(因为我在我的其他客户端项目中使用它,并希望在这里使用它的一致性(。为了深入了解它,我试图创建一个精简的项目

,以最低限度重现问题。我的包.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-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.

正如@jonrsharpe所发现的,我的潜在问题是我使用的节点版本(10.13.0(。通过更新到版本 12.18.0,问题消失了。

相关内容

最新更新