From e079feafc01b07a6e886db2e1bd5b9fd8f7eab16 Mon Sep 17 00:00:00 2001 From: Julian Grande Date: Tue, 1 Feb 2022 22:44:00 +0100 Subject: [PATCH] Updated documentation to support Strapi v4 This is an updated version of the unit-testing guide that supports Strapi v4. The last test, 'should return users data for authenticated user' is not yet functioning but the rest of the guide is. --- .../latest/guides/unit-testing.md | 94 ++++++++++--------- 1 file changed, 48 insertions(+), 46 deletions(-) diff --git a/docs/developer-docs/latest/guides/unit-testing.md b/docs/developer-docs/latest/guides/unit-testing.md index 3b5c488a4a..a5002baec5 100644 --- a/docs/developer-docs/latest/guides/unit-testing.md +++ b/docs/developer-docs/latest/guides/unit-testing.md @@ -114,25 +114,39 @@ These tasks require adding some files - let's create a folder `tests` where all **Path —** `./tests/helpers/strapi.js` ```js -const Strapi = require('strapi'); -const http = require('http'); +const Strapi = require("@strapi/strapi"); +const fs = require("fs"); let instance; async function setupStrapi() { if (!instance) { - /** the following 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()); + instance = strapi; + + await instance.server.mount(); } return instance; } -module.exports = { setupStrapi }; + +async function cleanupStrapi() { + const dbSettings = strapi.config.get("database.connections.default.settings"); + + //close server to release the db-file + await strapi.server.httpServer.close(); + + //delete test database after all tests have completed + if (dbSettings && dbSettings.filename) { + const tmpDbFile = `${__dirname}/../${dbSettings.filename}`; + if (fs.existsSync(tmpDbFile)) { + fs.unlinkSync(tmpDbFile); + } + } + // close the connection to the database + await strapi.db.connection.destroy(); +} + +module.exports = { setupStrapi, cleanupStrapi }; ``` ### Test strapi instance @@ -143,30 +157,17 @@ We need a main entry file for our tests, one that will also test our helper file ```js const fs = require('fs'); -const { setupStrapi } = require('./helpers/strapi'); +const { setupStrapi, cleanupStrapi } = require("./helpers/strapi"); -/** this code is called once before any test is called */ beforeAll(async () => { - await setupStrapi(); // singleton so it can be called many times + await setupStrapi(); }); -/** this code is called once before all the tested are finished */ afterAll(async () => { - const dbSettings = strapi.config.get('database.connections.default.settings'); - - //close server to release the db-file - await strapi.destroy(); - - //delete test database after all tests - if (dbSettings && dbSettings.filename) { - const tmpDbFile = `${__dirname}/../${dbSettings.filename}`; - if (fs.existsSync(tmpDbFile)) { - fs.unlinkSync(tmpDbFile); - } - } + await cleanupStrapi(); }); -it('strapi is defined', () => { +it("strapi is defined", () => { expect(strapi).toBeDefined(); }); ``` @@ -210,14 +211,15 @@ Let's create a separate test file where `supertest` will be used to check if end ```js const request = require('supertest'); -it('should return hello world', async () => { - await request(strapi.server) // app server is an instance of Class: http.Server - .get('/hello') +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 + .then((data) => { + expect(data.text).toBe("Hello World!"); // expect the response text }); }); + ``` Then include this code to `./tests/app.test.js` at the bottom of that file @@ -263,31 +265,31 @@ const request = require('supertest'); // user mock data const mockUserData = { - username: 'tester', - email: 'tester@strapi.com', - provider: 'local', - password: '1234abc', + username: "tester", + email: "tester@strapi.com", + provider: "local", + password: "1234abc", confirmed: true, blocked: null, }; -it('should login user and return jwt token', async () => { +it("should login user and return jwt token", async () => { /** Creates a new user and save it to the database */ - await strapi.plugins['users-permissions'].services.user.add({ + await strapi.plugins["users-permissions"].services.user.add({ ...mockUserData, }); - await request(strapi.server) // app server is an instance of Class: http.Server - .post('/auth/local') - .set('accept', 'application/json') - .set('Content-Type', 'application/json') + await request(strapi.server.httpServer) // app server is an instance of Class: http.Server + .post("/api/auth/local") + .set("accept", "application/json") + .set("Content-Type", "application/json") .send({ identifier: mockUserData.email, password: mockUserData.password, }) - .expect('Content-Type', /json/) + .expect("Content-Type", /json/) .expect(200) - .then(data => { + .then((data) => { expect(data.body.jwt).toBeDefined(); }); }); @@ -310,8 +312,8 @@ it('should return users data for authenticated user', async () => { id: user.id, }); - await request(strapi.server) // app server is an instance of Class: http.Server - .get('/users/me') + await request(strapi.server.httpServer) // app server is an instance of Class: http.Server + .get('/api/users/me') .set('accept', 'application/json') .set('Content-Type', 'application/json') .set('Authorization', 'Bearer ' + jwt)