【问题标题】:How to launch supertest files in node?如何在节点中启动超测文件?
【发布时间】:2021-06-30 14:34:16
【问题描述】:

我有一个节点/打字稿服务器。我希望用 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",我得到这个错误:

开玩笑:找不到命令

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

【问题讨论】:

    标签: node.js jestjs supertest


    【解决方案1】:

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

    "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

    【讨论】:

    • 您好,感谢您的回答!所以,我现在可以启动文件了,但是服务器因为这个新错误而崩溃:ReferenceError: You are trying to import a file after Jest 环境已被拆除。基本上,路由通过中间件对用户进行身份验证(我添加了 .set("Cookie", ["_id=567;locale=en"])),然后触发路由。
    • @DoneDeal0 与本题无关,可以尝试自己研究或新建一个SO题。
    【解决方案2】:

    在 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.ts。这里我解释一下:supertest not found error testing express endpoint

    这应该设置您的测试环境

    【讨论】:

    • 服务器不断崩溃,出现错误 500。You are trying to import` 在 Jest 环境被拆除后的文件。`
    • 您确定“预设”设置正确吗?也许你打错了
    • 它无法识别环境。你能升级你的节点吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-20
    • 1970-01-01
    • 2022-07-09
    • 1970-01-01
    • 2012-06-25
    相关资源
    最近更新 更多