【发布时间】:2022-01-13 22:26:07
【问题描述】:
我正在尝试在全新的 Strapi 应用程序上添加单元测试。官方文档仍在进行中。那么,在文档准备好之前,有没有办法将 jest 单元测试添加到strapi 应用程序?我按照 v3 文档中的方法没有运气。
【问题讨论】:
-
太宽泛了。请说明具体问题。
标签: node.js unit-testing jestjs strapi
我正在尝试在全新的 Strapi 应用程序上添加单元测试。官方文档仍在进行中。那么,在文档准备好之前,有没有办法将 jest 单元测试添加到strapi 应用程序?我按照 v3 文档中的方法没有运气。
【问题讨论】:
标签: node.js unit-testing jestjs strapi
在初始化 Strapi 应用程序的 API 时,从 Strapi V3 到 Strapi V4 有很多变化。最重要的变化是 Strapi 如何填充 KOA 路由,以及如何向 http 服务器发出请求。
要填充 KOA 路线,请使用
await instance.server.mount();
而不是
await instance.app
.use(instance.router.routes()) // populate KOA routes
.use(instance.router.allowedMethods()); // populate KOA methods
instance.server = http.createServer(instance.app.callback());
调用http服务器使用
strapi.server.httpServer
而不是
strapi.server
在定义测试数据库时,您还需要使用新的数据库配置架构。您可以使用以下内容作为测试的初始设置。
第一次运行
yarn add --dev jest supertest sqlite3
或
npm install --save-dev jest supertest sqlite3
然后将以下内容添加到您的 ./package.json 脚本中:
"scripts": {
// ...strapi scripts
"test": "jest --forceExit --detectOpenHandles", //add
"watch": "yarn test --watch", // optional
}
然后添加以下文件:
./jest.config.js
module.exports = {
verbose: true,
testPathIgnorePatterns: [
"/node_modules/",
".tmp",
".cache"
],
modulePaths: [
"/node_modules/",
],
testEnvironment: "node",
};
./config/env/test/database.json
{
"connection": {
"client": "sqlite",
"connection": {
"filename": ".tmp/test.db"
},
"useNullAsDefault": true,
"pool": {
"min": 0,
"max": 1
}
}
}
./tests/helpers/strapi.js
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");
const tmpDbFile = dbSettings.connection.filename
//close server to release the db-file
await strapi.server.httpServer.close();
//delete test database after all tests
if (dbSettings && tmpDbFile) {
if (fs.existsSync(tmpDbFile)) {
fs.unlinkSync(tmpDbFile);
}
}
// close the connection to the database
await strapi.db.connection.destroy();
}
module.exports = { setupStrapi, cleanupStrapi };
请注意,您需要在项目中指定 in the strapi docs 的 /hello 端点才能通过下一个测试。
./tests/app.test.js
const { setupStrapi, cleanupStrapi } = require("./helpers/strapi");
jest.setTimeout(15000);
beforeAll(async () => {
await setupStrapi();
});
afterAll(async () => {
await cleanupStrapi();
});
it("strapi is defined", () => {
expect(strapi).toBeDefined();
});
require('./hello')
./tests/hello/index.js
const request = require('supertest');
it('should return hello world', async () => {
await request(strapi.server.httpServer)
.get('/api/hello')
.expect(200) // Expect response http code 200
});
我希望这可以帮助任何在同样问题上苦苦挣扎的人。我会随着进度更新答案。
【讨论】: