【问题标题】:How to implement unit testing within the strapi framework如何在strapi框架内实现单元测试
【发布时间】:2020-06-02 18:39:13
【问题描述】:

我正在尝试使用 Strapi,并希望创建一个通过单元测试验证的控制器。

如何在 Strapi 中设置单元测试?

我已经写了下面的测试

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

但是,在我的 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);

Strapi 和测试的最佳做法是什么?

【问题讨论】:

    标签: node.js unit-testing jestjs strapi


    【解决方案1】:

    我已经通过创建一个帮助程序在 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 };
    

    您现在可以从app.controllers 获取所有控制器并一一测试。

    我对 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
    });
    

    我已尝试在我的博客 https://medium.com/qunabu-interactive/strapi-jest-testing-with-gitlab-ci-82ffe4c5715a 上介绍此主题

    【讨论】:

    【解决方案2】:

    关于版本升级有一些变化。

    我建议切换到stable 3.0.0并使用以下sn -p

    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 是我的示例项目。

    Guide for this is hopefully coming to Strapi documentation sooner then later.

    【讨论】:

      猜你喜欢
      • 2023-04-09
      • 2020-11-03
      • 2013-10-17
      • 1970-01-01
      • 1970-01-01
      • 2012-08-25
      • 2017-07-03
      • 2023-03-22
      • 1970-01-01
      相关资源
      最近更新 更多