简体   繁体   English

如何在trapi框架内实现单元测试

[英]How to implement unit testing within the strapi framework

I'm experimenting with Strapi and would like to create a controller verified by unit tests.我正在试验 Strapi 并想创建一个通过单元测试验证的控制器。

How do I setup Unit tests within Strapi?如何在 Strapi 中设置单元测试?

I have written the following test我已经编写了以下测试

test('checks entity inside boundary',async ()=> {
    ctx={};
    var result = await controller.findnearby(ctx);
    result = {};
    expect(result).anyting();
});

however, inside my Controller I have code that accesses a global strapi object, which causes this error ReferenceError: strapi is not defined但是,在我的 Controller 中,我有访问全局 Strapi 对象的代码,这会导致此错误ReferenceError: strapi is not defined

   strapi.log.info('findNearby');
   strapi.log.info(ctx.request.query.lat);
   strapi.log.info(ctx.request.query.long);

What is the best practice with Strapi and testing? Strapi 和测试的最佳实践是什么?

I've managed to achieve testing in Strapi by creating a helper我已经通过创建一个助手成功地在 Strapi 中实现了测试

const Strapi = require("strapi"); 
// above require creates a global named `strapi` that is an instance of Strapi 

let instance; // singleton 

async function setupStrapi() {
  if (!instance) {
    await Strapi().load();
    instance = strapi; 
    instance.app
      .use(instance.router.routes()) // this code in copied from app/node_modules/strapi/lib/Strapi.js
      .use(instance.router.allowedMethods());
  }
  return instance;
}

module.exports = { setupStrapi };

You can get all of the controllers from app.controllers now and test them one by one.您现在可以从app.controllers获取所有控制器并app.controllers测试它们。

My example test (in Jest) for API endpoint would look like我对 API 端点的示例测试(在 Jest 中)看起来像

const request = require("supertest");
const { setupStrapi } = require("./helpers/strapi");

// We're setting timeout because sometimes bootstrap can take 5-7 seconds (big apps)
jest.setTimeout(10000);

let app; // this is instance of the the strapi

beforeAll(async () => {
  app = await setupStrapi(); // return singleton so it can be called many times
});

it("should respond with 200 on /heartbeat", (done) => {
  request(app.server) // app server is and instance of Class: http.Server
    .get("/heartbeat")
    .expect(200) // expecting response header code to by 200 success
    .expect("Hello World!") // expecting reponse text to be "Hello World!"
    .end(done); // let jest know that test is finished
});

I've tried to cover this topic on my blog https://medium.com/qunabu-interactive/strapi-jest-testing-with-gitlab-ci-82ffe4c5715a我试图在我的博客https://medium.com/qunabu-interactive/strapi-jest-testing-with-gitlab-ci-82ffe4c5715a上讨论这个话题

There were some changes, regarding the version upgrade.有一些变化,关于版本升级。

I recommend to switch to stable 3.0.0 and use the following snippet我建议切换到稳定版 3.0.0 并使用以下代码段

const Strapi = require("strapi");
const http = require("http");

let instance;

async function setupStrapi() {
  if (!instance) {
    /** the follwing code in copied from `./node_modules/strapi/lib/Strapi.js` */
    await Strapi().load();
    instance = strapi; // strapi is global now
    await instance.app
      .use(instance.router.routes()) // populate KOA routes
      .use(instance.router.allowedMethods()); // populate KOA methods

    instance.server = http.createServer(instance.app.callback());
  }
  return instance;
}
module.exports = { setupStrapi };

Here is my example project.是我的示例项目。

Guide for this is hopefully coming to Strapi documentation sooner then later.这方面的指南有望迟早出现在 Strapi 文档中。

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

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