Skip to content

Commit a2f2c95

Browse files
authored
Merge pull request #264 from jorrit/testhelper
fix: location of test db, awaiting strapi teardown
2 parents 5db0235 + a8c8346 commit a2f2c95

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

playground/config/env/test/database.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@ import path from 'path';
22

33
export default ({ env }) => ({
44
connection: {
5-
client: "sqlite",
5+
client: 'sqlite',
66
connection: {
77
filename: path.join(
88
__dirname,
9+
// Get out of config/env/test
10+
'..',
11+
'..',
912
'..',
1013
// We need to go back once more to get out of the dist folder
1114
'..',
12-
env("DATABASE_TEST_FILENAME", ".tmp/test.db"),
15+
env('DATABASE_TEST_FILENAME', '.tmp/test.db'),
1316
),
1417
},
1518
useNullAsDefault: true,

playground/tests/helpers.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import fs from 'node:fs';
2+
import assert from 'node:assert';
13
import { createStrapi } from '@strapi/strapi';
2-
import fs, { PathLike } from 'fs';
4+
import type { Core } from '@strapi/types';
35

4-
let instance;
6+
let instance: Core.Strapi | undefined;
57

68
/**
79
* Setups strapi for futher testing
@@ -12,29 +14,31 @@ export async function setupStrapi() {
1214
appDir: './playground',
1315
distDir: './playground/dist',
1416
}).load();
17+
strapi.server.mount();
1518

1619
instance = strapi; // strapi is global now
17-
18-
await instance.server.mount();
1920
}
20-
return instance;
2121
}
2222

2323
/**
2424
* Closes strapi after testing
2525
*/
2626
export async function stopStrapi() {
2727
if (instance) {
28-
await instance.server.httpServer.close();
29-
await instance.db.connection.destroy();
30-
instance.destroy();
31-
const tmpDbFile = strapi.config.get(
28+
const tmpDbFile = instance.config.get(
3229
'database.connection.connection.filename',
3330
);
3431

35-
if (fs.existsSync(tmpDbFile as PathLike)) {
36-
fs.unlinkSync(tmpDbFile as PathLike);
32+
assert(typeof tmpDbFile === 'string');
33+
34+
instance.server.httpServer.close();
35+
await instance.db.connection.destroy();
36+
await instance.destroy();
37+
38+
if (fs.existsSync(tmpDbFile)) {
39+
fs.unlinkSync(tmpDbFile);
3740
}
41+
42+
instance = undefined;
3843
}
39-
return instance;
4044
}

0 commit comments

Comments
 (0)