我在《国际笔录》中树立了一个新针(Typescript)的测试环境。 但我无法成功地进行试验。
这是我的助手档案,例如:
const Strapi = require("@strapi/strapi");
const fs = require("fs");
let instance;
async function setupStrapi() {
if (!instance) {
await Strapi().load();
instance = strapi;
await instance.server.mount();
}
return instance;
}
async function cleanupStrapi() {
const dbSettings = strapi.config.get("database.connection");
//close server to release the db-file
await strapi.server.httpServer.close();
// close the connection to the database before deletion
await strapi.db.connection.destroy();
//delete test database after all tests have completed
if (dbSettings && dbSettings.connection && dbSettings.connection.filename) {
const tmpDbFile = dbSettings.connection.filename;
if (fs.existsSync(tmpDbFile)) {
fs.unlinkSync(tmpDbFile);
}
}
}
module.exports = { setupStrapi, cleanupStrapi };
这是我的试验文件:
const fs = require("fs");
const { setupStrapi, cleanupStrapi } = require("./helpers/strapi");
const request = require("supertest");
beforeAll(async () => {
await setupStrapi();
}, 10000);
afterAll(async () => {
await cleanupStrapi();
}, 10000);
it("strapi is defined", () => {
expect(strapi).toBeDefined();
});
it("should return hello world", async () => {
await request(strapi.server.httpServer)
.get("/api/hello")
.expect(200) // Expect response http code 200
.then((data) => {
expect(data.text).toBe("Hello World!"); // expect the response text
});
});
在我进行这一试验时:
第一次测试——“限制的定义”
但下面的第二次测试失败了。
it("should return hello world", async () => {
await request(strapi.server.httpServer)
.get("/api/hello")
.expect(200) // Expect response http code 200
.then((data) => {
expect(data.text).toBe("Hello World!"); // expect the response text
});
});
我发现这一错误:
expected 200 “OK”, got 404 “Notborn”
https://i.stack.imgur.com/ly9rh.png”rel=“nofollow noreferer”> log/ log
当我用“http://127.0.0.1:1337”取代平线镜时,测试成功进行。
我需要做些什么来解决这个问题?