如何在节点中启动超级测试文件?

问题描述

我有一个节点/打字稿服务器。我想用 supertest 测试 api 路由。我已经编写了我的第一个测试,但无法启动它,因为

TypeError [ERR_UNKNowN_FILE_EXTENSION]: /Users/myname/Desktop/Code/Reapr/server/src/tests/search.test.ts 的未知文件扩展名“.ts”

test.ts 文件是:

import request from "supertest";
import { server } from "../index";

describe("GET /user/:id",() => {
  it("return user information",(done) => {
    request(server)
      .get("/user/123")
      .set("Accept","application/json")
      .expect("Content-Type",/json/)
      .expect(200,done);
  });
});

package.json 是:

{
  "name": "my-server","version": "1.0.0","main": "index.js","license": "","scripts": {
    "build": "rimraf ./build && tsc","dev": "nodemon","lint": "eslint . --ext .js,.ts","start": "npm run build && node build/index.js","test": "node src/tests/search.test.ts"
  },"type": "module","dependencies": {
    "bcrypt": "^5.0.1","body-parser": "^1.19.0","cloudinary": "^1.25.1","compression": "^1.7.4","cookie-parser": "^1.4.5","cors": "^2.8.5","express": "^4.17.1","express-rate-limit": "^5.2.6","jsonwebtoken": "^8.5.1","mongoose": "^5.12.3","multer": "^1.4.2","nodemailer": "^6.5.0","nodemailer-express-handlebars": "^4.0.0","socket.io": "^4.0.1","stripe": "^8.142.0"
  },"devDependencies": {
    "@types/express": "^4.17.11","@types/jest": "^26.0.22","@types/mongoose": "^5.10.4","@types/node": "^14.14.37","@types/socket.io": "^2.1.13","@types/stripe": "^8.0.417","@typescript-eslint/eslint-plugin": "^4.20.0","@typescript-eslint/parser": "^4.20.0","babel-plugin-module-resolver": "^4.1.0","dotenv": "^8.2.0","eslint": "^7.23.0","nodemon": "^2.0.7","rimraf": "^3.0.2","supertest": "^6.1.3","ts-node": "^9.1.1","typescript": "^4.2.3"
  }
}

此外,只有在我明确编写脚本 "test": node src/tests/search.test.ts" 时才会出现此错误。如果我写 "test": "jest",我会收到这个错误

开玩笑:找不到命令

那么,如果可能的话,如何一次性正确启动我的超级测试文件,而不是更改每个文件的脚本行?

解决方法

您可以直接使用 ts 执行 ts-node 文件。

"test": "ts-node src/tests/search.test.ts"

或将其构建为 js 文件并使用 node 运行。

要执行 jest 命令,您必须安装它,并且您使用的是 Typescript (?) 那么您还需要 ts-jest:

npm install ts-jest jest -D

为 jest 创建一个配置文件:jest.config.js

module.exports = {
  roots: ['<rootDir>/src'],testMatch: [
    '**/__tests__/**/*.+(ts|tsx|js)','**/?(*.)+(spec|test).+(ts|tsx|js)',],transform: {
    '^.+\\.(ts|tsx)$': 'ts-jest',},};

现在,尝试执行 jest:npx jest

,

在 package.json 中:

"jest": {
    "preset": "ts-jest","testEnvironment": "node","setupFilesAfterEnv": [
      "./src/test/setup.ts"
    ]
  },

jest 不明白 typescript 是什么,"ts-jest" 会添加 typescript 支持。

"setupFilesAfterEnv" 是可选的。如果你有一个安装文件,它会运行它来设置额外的配置,比如添加“beforeAll,beforeEach”等。

这是您需要为测试环境安装的依赖项:

"devDependencies": {
    "@types/jest": "^26.0.15","@types/supertest": "^2.0.10","jest": "^26.6.1","supertest": "^6.0.0","ts-jest": "^26.4.3"
  }
  • 还要将此脚本添加到 package.json 中:

    "test": "jest --watchAll --no-cache"
    

--no-cache 标志与打字稿设置有关。 jest 有时无法捕捉到打字稿文件中的变化。

最后你导入了 server 但确保你正确设置了 server.ts。我在这里解释:supertest not found error testing express endpoint

这应该设置您的测试环境

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...