Skip to content

Commit 4aeda23

Browse files
committed
feat(tests): refactor global setup and teardown for e2e tests with improved structure and environment handling
1 parent 03c0c09 commit 4aeda23

16 files changed

+101
-78
lines changed
136 KB
Binary file not shown.

services/backend/jest.config.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ module.exports = {
55
moduleNameMapper: {
66
'^@src/(.*)$': '<rootDir>/src/$1',
77
},
8-
globalSetup: '<rootDir>/tests/globalSetup.ts',
9-
globalTeardown: '<rootDir>/tests/globalTeardown.ts',
8+
globalSetup: '<rootDir>/tests/e2e/globalSetup.ts',
9+
globalTeardown: '<rootDir>/tests/e2e/globalTeardown.ts',
1010
transform: {
1111
'^.+\\.ts$': ['ts-jest', {
1212
useESM: false,
@@ -15,5 +15,5 @@ module.exports = {
1515
// Run tests sequentially to ensure proper order
1616
maxWorkers: 1,
1717
// Use custom test sequencer to ensure correct order
18-
testSequencer: '<rootDir>/tests/testSequencer.js',
18+
testSequencer: '<rootDir>/tests/e2e/testSequencer.js',
1919
};

services/backend/src/routes/db/setup.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,14 @@ async function setupDbHandler(
2727
const clientRequestBody = DbSetupRequestBodySchema.parse(request.body);
2828

2929
let internalConfigObject: InternalDbConfig;
30-
const fixedSQLiteDbPath = 'persistent_data/database/deploystack.db';
30+
// Determine DB path based on environment
31+
const isTestEnv = process.env.NODE_ENV === 'test';
32+
const sqliteDbFileName = isTestEnv ? 'deploystack.test.db' : 'deploystack.db';
33+
// dbPath should be relative to services/backend, and the actual db file is usually in a 'database' subfolder
34+
const sqliteDbPath = `database/${sqliteDbFileName}`;
3135

3236
if (clientRequestBody.type === DatabaseType.SQLite) {
33-
internalConfigObject = { type: DatabaseType.SQLite, dbPath: fixedSQLiteDbPath };
37+
internalConfigObject = { type: DatabaseType.SQLite, dbPath: sqliteDbPath };
3438
} else {
3539
return reply.status(400).send({ error: 'Invalid database type specified. Only SQLite is supported.' });
3640
}

services/backend/tests/1-setup.e2e.test.ts renamed to services/backend/tests/e2e/1-setup.e2e.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import * as fs from 'fs-extra';
44
import * as path from 'path';
55
import { getTestContext } from './testContext';
66

7-
const PERSISTENT_DATA_PATH = path.join(__dirname, '..', 'persistent_data');
8-
const DB_FILE_PATH = path.join(PERSISTENT_DATA_PATH, 'database', 'deploystack.db');
7+
// __dirname is services/backend/tests/e2e
8+
const APP_BACKEND_ROOT = path.join(__dirname, '..', '..'); // Resolves to services/backend/
9+
const DB_FILE_PATH = path.join(APP_BACKEND_ROOT, 'database', 'deploystack.test.db'); // Path where the app creates the db
910

1011
describe('POST /api/db/setup and GET /api/db/status (E2E)', () => {
1112
let server: FastifyInstance;

services/backend/tests/2-user-registration.e2e.test.ts renamed to services/backend/tests/e2e/2-user-registration.e2e.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import * as fs from 'fs-extra';
44
import * as path from 'path';
55
import { getTestContext, updateTestContext } from './testContext';
66

7-
const PERSISTENT_DATA_PATH = path.join(__dirname, '..', 'persistent_data');
8-
const DB_FILE_PATH = path.join(PERSISTENT_DATA_PATH, 'database', 'deploystack.db');
7+
// __dirname is services/backend/tests/e2e
8+
const APP_BACKEND_ROOT = path.join(__dirname, '..', '..'); // Resolves to services/backend/
9+
const DB_FILE_PATH = path.join(APP_BACKEND_ROOT, 'database', 'deploystack.test.db'); // Path where the app creates the db
910

1011
describe('User Registration E2E Tests', () => {
1112
let server: FastifyInstance;

services/backend/tests/3-email-login.e2e.test.ts renamed to services/backend/tests/e2e/3-email-login.e2e.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import * as fs from 'fs-extra';
44
import * as path from 'path';
55
import { getTestContext, updateTestContext } from './testContext';
66

7-
const PERSISTENT_DATA_PATH = path.join(__dirname, '..', 'persistent_data');
8-
const DB_FILE_PATH = path.join(PERSISTENT_DATA_PATH, 'database', 'deploystack.db');
7+
// __dirname is services/backend/tests/e2e
8+
const APP_BACKEND_ROOT = path.join(__dirname, '..', '..'); // Resolves to services/backend/
9+
const DB_FILE_PATH = path.join(APP_BACKEND_ROOT, 'database', 'deploystack.test.db'); // Path where the app creates the db
910

1011
describe('Email Login and Logout E2E Tests', () => {
1112
let server: FastifyInstance;

services/backend/tests/4-global-settings-check.e2e.test.ts renamed to services/backend/tests/e2e/4-global-settings-check.e2e.test.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import * as fs from 'fs-extra';
22
import * as path from 'path';
33
import Database from 'better-sqlite3';
4-
import type { GlobalSettingsModule, GlobalSettingDefinition } from '../src/global-settings/types';
4+
import type { GlobalSettingsModule, GlobalSettingDefinition } from '../../src/global-settings/types';
55

66
// Helper function to dynamically get all defined core setting keys
77
async function getDefinedCoreSettingKeys(): Promise<string[]> {
88
const definedKeys: string[] = [];
9-
// Correct path from 'services/backend/tests/' to 'services/backend/src/global-settings/'
10-
const globalSettingsDir = path.join(__dirname, '..', 'src', 'global-settings');
9+
// __dirname is services/backend/tests/e2e
10+
// '..' -> services/backend/tests
11+
// '..' -> services/backend
12+
// then 'src/global-settings'
13+
const globalSettingsDir = path.join(__dirname, '..', '..', 'src', 'global-settings');
1114

1215
try {
1316
const files = fs.readdirSync(globalSettingsDir);
@@ -54,7 +57,9 @@ async function getDefinedCoreSettingKeys(): Promise<string[]> {
5457
}
5558

5659
describe('Global Settings Initialization Check', () => {
57-
const dbPath = path.join(__dirname, '..', 'persistent_data', 'database', 'deploystack.db');
60+
// __dirname is services/backend/tests/e2e
61+
const APP_BACKEND_ROOT = path.join(__dirname, '..', '..'); // Resolves to services/backend/
62+
const dbPath = path.join(APP_BACKEND_ROOT, 'database', 'deploystack.test.db'); // Correct path
5863
let db: Database.Database;
5964

6065
beforeAll(() => {

services/backend/tests/5-global-settings-api-access.e2e.test.ts renamed to services/backend/tests/e2e/5-global-settings-api-access.e2e.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import * as fs from 'fs-extra';
44
import * as path from 'path';
55
import { getTestContext } from './testContext';
66

7-
const PERSISTENT_DATA_PATH = path.join(__dirname, '..', 'persistent_data');
8-
const DB_FILE_PATH = path.join(PERSISTENT_DATA_PATH, 'database', 'deploystack.db');
7+
// __dirname is services/backend/tests/e2e
8+
const APP_BACKEND_ROOT = path.join(__dirname, '..', '..'); // Resolves to services/backend/
9+
const DB_FILE_PATH = path.join(APP_BACKEND_ROOT, 'database', 'deploystack.test.db'); // Path where the app creates the db
910

1011
describe('Global Settings Access Control E2E Tests', () => {
1112
let server: FastifyInstance;

services/backend/tests/6-global-settings-helpers.e2e.test.ts renamed to services/backend/tests/e2e/6-global-settings-helpers.e2e.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ import { FastifyInstance } from 'fastify';
33
import * as fs from 'fs-extra';
44
import * as path from 'path';
55
import { getTestContext } from './testContext';
6-
import { GlobalSettings } from '../src/global-settings';
7-
import { initializeDatabase } from '../src/db';
6+
import { GlobalSettings } from '../../src/global-settings';
7+
import { initializeDatabase } from '../../src/db';
88

9-
const PERSISTENT_DATA_PATH = path.join(__dirname, '..', 'persistent_data');
10-
const DB_FILE_PATH = path.join(PERSISTENT_DATA_PATH, 'database', 'deploystack.db');
9+
// __dirname is services/backend/tests/e2e
10+
const APP_BACKEND_ROOT = path.join(__dirname, '..', '..'); // Resolves to services/backend/
11+
const DB_FILE_PATH = path.join(APP_BACKEND_ROOT, 'database', 'deploystack.test.db'); // Path where the app creates the db
1112

1213
describe('Global Settings Helper Methods E2E Tests', () => {
1314
let server: FastifyInstance;

services/backend/tests/7-global-enable-login.e2e.test.ts renamed to services/backend/tests/e2e/7-global-enable-login.e2e.test.ts

File renamed without changes.

0 commit comments

Comments
 (0)