Skip to content

Commit d8fc793

Browse files
committed
chore: add helpers for starting & stopping the playground in a testing environment
1 parent abd5e0f commit d8fc793

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

playground/tests/helpers.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const { createStrapi, compileStrapi } = require("@strapi/strapi");
2+
const fs = require("fs");
3+
4+
let instance;
5+
6+
/**
7+
* Setups strapi for futher testing
8+
*/
9+
async function setupStrapi() {
10+
if (!instance) {
11+
const app = await createStrapi({
12+
appDir: "./playground",
13+
distDir: "./playground/dist",
14+
}).load();
15+
16+
instance = app; // strapi is global now
17+
18+
await instance.server.mount();
19+
}
20+
return instance;
21+
}
22+
23+
/**
24+
* Closes strapi after testing
25+
*/
26+
async function stopStrapi() {
27+
if (instance) {
28+
await instance.server.httpServer.close();
29+
await instance.db.connection.destroy();
30+
instance.destroy();
31+
const tmpDbFile = strapi.config.get(
32+
"database.connection.connection.filename"
33+
);
34+
35+
if (fs.existsSync(tmpDbFile)) {
36+
fs.unlinkSync(tmpDbFile);
37+
}
38+
39+
}
40+
return instance;
41+
}
42+
43+
module.exports = {
44+
setupStrapi,
45+
stopStrapi,
46+
};

0 commit comments

Comments
 (0)