-
Notifications
You must be signed in to change notification settings - Fork 8
feat:add e2e testing initial setup #17
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
Open
MasterBrian99
wants to merge
1
commit into
wavezync:main
Choose a base branch
from
MasterBrian99:feat/e2e-testing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { StartedPostgreSqlContainer } from '@testcontainers/postgresql'; | ||
|
||
declare global { | ||
// eslint-disable-next-line no-var | ||
var __TEST__: boolean; | ||
// eslint-disable-next-line no-var | ||
var __Container__: { | ||
postgres: StartedPostgreSqlContainer | null; | ||
}; | ||
} | ||
|
||
export {}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { TestingModule } from '@nestjs/testing'; | ||
import { TestModuleFactory } from './factory/test-module.factory'; | ||
import { INestApplication } from '@nestjs/common'; | ||
|
||
describe('AppController (e2e)', () => { | ||
let testingModule: TestingModule; | ||
let app: INestApplication; | ||
|
||
beforeAll(async () => { | ||
testingModule = await TestModuleFactory.createTestModule(); | ||
app = testingModule.createNestApplication(); | ||
await app.init(); | ||
}, 60000); | ||
it('should return "Hello World!"', () => { | ||
expect(true).toBe(true); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { ConfigModule, ConfigService } from '@nestjs/config'; | ||
import { createTestConfig } from '../helpers/test-configuration'; | ||
import { TestAppModule } from '../test-app.module'; | ||
import { StartedPostgreSqlContainer } from '@testcontainers/postgresql'; | ||
export class TestModuleFactory { | ||
static async createTestModule(): Promise<TestingModule> { | ||
const postgresContainer = globalThis.__Container__ | ||
.postgres as StartedPostgreSqlContainer; | ||
|
||
// this is to ignore warning from env not found error. does not matter what we put | ||
process.env.DATABASE_URL = postgresContainer.getConnectionUri(); | ||
process.env.SECRET = 'ASD'; | ||
const testConfig = createTestConfig( | ||
postgresContainer.getConnectionUri() + '?sslmode=disable', | ||
); | ||
const moduleRef = await Test.createTestingModule({ | ||
imports: [ | ||
ConfigModule.forRoot({ | ||
load: [() => testConfig], | ||
isGlobal: true, | ||
}), | ||
TestAppModule, | ||
], | ||
}) | ||
.overrideProvider(ConfigService) | ||
.useValue({ | ||
get: (key: string) => { | ||
const keys = key.split('.'); | ||
let value = testConfig; | ||
for (const k of keys) { | ||
value = value[k]; | ||
} | ||
return value; | ||
}, | ||
}) | ||
.compile(); | ||
|
||
return moduleRef; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// test/config/test-configuration.ts | ||
import { AppConfig, LoggerFormat } from '../../src/config/configuration'; | ||
|
||
export const createTestConfig = (databaseUrl: string): AppConfig => ({ | ||
corsMaxAge: 86400, | ||
database: { | ||
poolSize: 5, | ||
url: databaseUrl, | ||
}, | ||
port: 3000, | ||
secret: 'kugk2iz30q5mlc6056der8sdnadibb', | ||
logger: { | ||
format: LoggerFormat.Json, | ||
level: 'error', | ||
}, | ||
isDevEnv: false, | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { | ||
PostgreSqlContainer, | ||
StartedPostgreSqlContainer, | ||
} from '@testcontainers/postgresql'; | ||
import { DB } from 'database/schema/db'; | ||
import { | ||
CamelCasePlugin, | ||
FileMigrationProvider, | ||
Kysely, | ||
Migrator, | ||
PostgresDialect, | ||
} from 'kysely'; | ||
import { join } from 'path'; | ||
import { promises as fs } from 'fs'; | ||
import * as path from 'path'; | ||
|
||
import { Pool } from 'pg'; | ||
|
||
export class TestContainerHelper { | ||
private static instance: TestContainerHelper; | ||
private static container: StartedPostgreSqlContainer; | ||
|
||
private constructor() {} | ||
|
||
public static getInstance(): TestContainerHelper { | ||
if (!TestContainerHelper.instance) { | ||
TestContainerHelper.instance = new TestContainerHelper(); | ||
} | ||
return TestContainerHelper.instance; | ||
} | ||
|
||
public async startPostgresContainer(): Promise<StartedPostgreSqlContainer> { | ||
if (!TestContainerHelper.container) { | ||
TestContainerHelper.container = await new PostgreSqlContainer( | ||
'postgres:15-alpine', | ||
).start(); | ||
console.log('PostgreSQL container started !!!'); | ||
|
||
const db = new Kysely<DB>({ | ||
dialect: new PostgresDialect({ | ||
pool: new Pool({ | ||
connectionString: `${TestContainerHelper.container.getConnectionUri()}?sslmode=disable`, | ||
max: 5, | ||
}), | ||
}), | ||
plugins: [new CamelCasePlugin()], | ||
}); | ||
await this.runMigrations(db); | ||
await db.destroy(); | ||
} | ||
return TestContainerHelper.container; | ||
} | ||
|
||
public async stopPostgresContainer(): Promise<void> { | ||
if (TestContainerHelper.container) { | ||
await TestContainerHelper.container.stop(); | ||
TestContainerHelper.container = undefined; | ||
} | ||
} | ||
|
||
private async runMigrations(db: Kysely<DB>): Promise<void> { | ||
console.log('Running migrations...'); | ||
|
||
const migrationsPath = join(__dirname, '../../src/database/migrations'); | ||
|
||
try { | ||
await fs.access(migrationsPath); | ||
|
||
const migrator = new Migrator({ | ||
db: db, | ||
provider: new FileMigrationProvider({ | ||
fs, | ||
path, | ||
migrationFolder: migrationsPath, | ||
}), | ||
allowUnorderedMigrations: true, | ||
}); | ||
|
||
const { error, results } = await migrator.migrateToLatest(); | ||
|
||
results?.forEach((it) => { | ||
if (it.status === 'Success') { | ||
console.log( | ||
`Migration "${it.migrationName}" was executed successfully`, | ||
); | ||
} else if (it.status === 'Error') { | ||
console.error(`Failed to execute migration "${it.migrationName}"`); | ||
} | ||
}); | ||
|
||
if (error) { | ||
console.error('Failed to migrate:', error); | ||
throw error; | ||
} | ||
|
||
console.log('Migrations completed successfully'); | ||
} catch (error) { | ||
console.error('Migration error:', error); | ||
throw error; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,17 @@ | ||
{ | ||
"moduleFileExtensions": ["js", "json", "ts"], | ||
"rootDir": ".", | ||
"rootDir": "./", | ||
"moduleNameMapper": { | ||
"^src/(.*)$": "<rootDir>/src/$1" | ||
}, | ||
"modulePaths": ["<rootDir>"], | ||
"moduleDirectories": ["<rootDir>/", "node_modules", "src", "<rootDir>/../"], | ||
"testEnvironment": "node", | ||
"testRegex": ".e2e-spec.ts$", | ||
"transform": { | ||
"^.+\\.(t|j)s$": "ts-jest" | ||
} | ||
}, | ||
"globalSetup": "./setup/global-setup.ts", | ||
"globalTeardown": "./setup/global-teardown.ts", | ||
"setupFilesAfterEnv": ["<rootDir>/@types/globals.d.ts"] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const MOCK_USERS = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
user1: { | ||
email: 'user1@example.com', | ||
password: 'password1', | ||
}, | ||
user2: { | ||
email: 'user2@example.com', | ||
password: 'password2', | ||
}, | ||
}; | ||
|
||
export default MOCK_USERS; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can have multiple test containers for the project, so it would be a better approach to name this as
pgInstance