backend/tests/helpers/strapi.js (view raw)
1const Strapi = require("@strapi/strapi");
2const fs = require("fs");
3
4let instance;
5
6async function setupStrapi() {
7 if (!instance) {
8 await Strapi().load();
9 instance = strapi;
10
11 await instance.server.mount();
12 }
13 return instance;
14}
15
16async function cleanupStrapi() {
17 const dbSettings = strapi.config.get("database.connections.default.settings");
18
19 //close server to release the db-file
20 await strapi.server.httpServer.close();
21
22 //delete test database after all tests have completed
23 if (dbSettings && dbSettings.filename) {
24 const tmpDbFile = `${__dirname}/../${dbSettings.filename}`;
25 if (fs.existsSync(tmpDbFile)) {
26 fs.unlinkSync(tmpDbFile);
27 }
28 }
29 // close the connection to the database
30 await strapi.db.connection.destroy();
31}
32
33module.exports = { setupStrapi, cleanupStrapi };