微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

我有一个节点/打字稿服务器。我想用 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

这应该设置您的测试环境

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。