Skip to content

fix: location of test db, awaiting strapi teardown #264

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 16, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions playground/config/env/test/database.ts
Original file line number Diff line number Diff line change
@@ -2,14 +2,17 @@ import path from 'path';

export default ({ env }) => ({
connection: {
client: "sqlite",
client: 'sqlite',
connection: {
filename: path.join(
__dirname,
// Get out of config/env/test
'..',
'..',
'..',
// We need to go back once more to get out of the dist folder
'..',
env("DATABASE_TEST_FILENAME", ".tmp/test.db"),
env('DATABASE_TEST_FILENAME', '.tmp/test.db'),
),
},
useNullAsDefault: true,
28 changes: 16 additions & 12 deletions playground/tests/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import fs from 'node:fs';
import assert from 'node:assert';
import { createStrapi } from '@strapi/strapi';
import fs, { PathLike } from 'fs';
import type { Core } from '@strapi/types';

let instance;
let instance: Core.Strapi | undefined;

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

instance = strapi; // strapi is global now

await instance.server.mount();
}
return instance;
}

/**
* Closes strapi after testing
*/
export async function stopStrapi() {
if (instance) {
await instance.server.httpServer.close();
await instance.db.connection.destroy();
instance.destroy();
const tmpDbFile = strapi.config.get(
const tmpDbFile = instance.config.get(
'database.connection.connection.filename',
);

if (fs.existsSync(tmpDbFile as PathLike)) {
fs.unlinkSync(tmpDbFile as PathLike);
assert(typeof tmpDbFile === 'string');

instance.server.httpServer.close();
await instance.db.connection.destroy();
await instance.destroy();

if (fs.existsSync(tmpDbFile)) {
fs.unlinkSync(tmpDbFile);
}

instance = undefined;
}
return instance;
}