node.js - 将 Jest 与 Node.JS 服务器应用程序一起使用时“未定义描述”
问题描述
我有一个相对简单的 Node.js 应用程序,我在其中尝试使用 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"
}
}
我在项目中没有 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,问题就消失了。
推荐阅读
- python - 未加权图中的路径:从源头到源头的最短行程,施加一个点
- python - 使用 Python 在 10,000 多张图像上使用 Vision API 的最快方法
- javascript - 在自定义函数中链接中间件函数
- javascript - if 和 draw 语句的顺序
- ansible - 在 Ansible 中查找文件名并保存到文件
- react-native - 如何处理嵌套在 createDrawerNavigator 中的 createStackNavigator 屏幕的路由?
- python - 如何从文件中获取链接并在python中循环它?
- dart - 是否有 Dart 函数来转换 List
加倍? - flutter - 使用 setState() 在应用程序中保持用户登录
- c# - 使用 c#/WPF/livecharts。如何在 SeriesCollection 中设置单个项目的可见性?