如何使用 jest 在 Strapi 版本 4 中添加单元测试?

Muh*_*ram 5 unit-testing node.js jestjs strapi

我正在尝试在全新的 Strapi 应用程序上添加单元测试。官方文档仍在完善中。那么,在文档准备好之前,有没有办法将 jest 单元测试添加到 Strapi 应用程序中?我按照 v3 文档中的方法操作,但没有成功。

Jos*_*nez 5

在初始化 Strapi 应用程序的 API 方面,从 Strapi V3 到 Strapi V4 有相当多的变化。最重要的变化是 Strapi 如何填充 KOA 路由,以及如何向 http 服务器发出请求。

要填充 KOA 路由,请使用

await instance.server.mount();
Run Code Online (Sandbox Code Playgroud)

代替

await instance.app
       .use(instance.router.routes()) // populate KOA routes
       .use(instance.router.allowedMethods()); // populate KOA methods
instance.server = http.createServer(instance.app.callback());
Run Code Online (Sandbox Code Playgroud)

调用http服务器使用

 strapi.server.httpServer
Run Code Online (Sandbox Code Playgroud)

代替

 strapi.server
Run Code Online (Sandbox Code Playgroud)

定义测试数据库时,您还需要使用新的数据库配置架构。您可以使用以下内容作为测试的初始设置。

以下是基于Strapi V3 单元测试指南的更新(和 WIP)指南。

第一次运行

 yarn add --dev jest supertest sqlite3
Run Code Online (Sandbox Code Playgroud)

或者

 npm install --save-dev jest supertest sqlite3
Run Code Online (Sandbox Code Playgroud)

然后将以下内容添加到您的./package.json脚本中:

"scripts": {
   // ...strapi scripts
  "test": "jest --forceExit --detectOpenHandles", //add
  "watch": "yarn test --watch", // optional
}
Run Code Online (Sandbox Code Playgroud)

然后添加以下文件:

./jest.config.js

module.exports = {
    verbose: true,
    testPathIgnorePatterns: [
        "/node_modules/",
        ".tmp",
        ".cache"
    ],
    modulePaths: [
        "/node_modules/",
    ],
    testEnvironment: "node",
};
Run Code Online (Sandbox Code Playgroud)

./config/env/test/database.json

{
    "connection": {
        "client": "sqlite",
        "connection": {
            "filename": ".tmp/test.db"
        },
        "useNullAsDefault": true,
        "pool": {
            "min": 0,
            "max": 1
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

./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 };
Run Code Online (Sandbox Code Playgroud)

请注意,您需要在项目中拥有Strapi 文档中指定的 /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')
Run Code Online (Sandbox Code Playgroud)

./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
});
Run Code Online (Sandbox Code Playgroud)

我希望这可以帮助任何遇到同样问题的人。我会随着进展更新答案。