简体   繁体   English

如何在节点中启动柴测试?

[英]How to launch a chai test in node?

I have a node/typescript application.我有一个节点/打字稿应用程序。 I'm trying to test routes with chai and chai-http.我正在尝试使用 chai 和 chai-http 测试路线。 I can launch a file when I write an explicit name: yarn test myroute.test.ts , but yarn test at the root does nothing.当我写一个明确的名称时,我可以启动一个文件: yarn test myroute.test.ts ,但根目录下的yarn test什么都不做。

Also, the test is never performed.此外,从不进行测试。 I only receive a Done in 0.06s.我只Done in 0.06s. in my terminal.在我的终端。 No "passed" or "failed".没有“通过”或“失败”。

Here is the test:这是测试:

 import chai from "chai"; import chaiHttp from "chai-http"; import { server } from "../index"; chai.use(chaiHttp); const chaiApi = chai.request(server); describe("GET /user/:id", () => { it("return user information", async () => { const res = await chaiApi.get("/user/123").set("Cookie", "_id=567;locale=en"); chai.assert.equal(res.status, 200); }); });

the package.json script is: "test": "test" . package.json 脚本是: "test": "test" I assume it's completely wrong, but chai doc says nothing about what to write here.我认为这是完全错误的,但是 chai doc 没有说明要在这里写什么。

chai is an assertion library, similar to Node's built-in assert . chai是一个断言库,类似于 Node 的内置assert It does NOT contain a test runner.它不包含测试运行器。 You need to use mocha to collect and run your declared test suites and test cases.您需要使用mocha来收集和运行您声明的测试套件和测试用例。

After adding the mocha test framework, you can declare npm test script like this:添加 mocha 测试框架后,可以像这样声明npm test脚本:

"test": "mocha --require ts-node/register --timeout 5000",

In order to run the TS test file, we need to specify ts-node/register .为了运行 TS 测试文件,我们需要指定ts-node/register Use it like this:像这样使用它:

yarn test myroute.test.ts

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM