Skip to content
Merged
Show file tree
Hide file tree
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
17 changes: 15 additions & 2 deletions packages/card-service/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,23 @@ const packageName = require('./package.json').name
module.exports = {
...baseConfig,
clearMocks: true,
testTimeout: 30000,
roots: [`<rootDir>/packages/${packageName}`],
setupFiles: [`<rootDir>/packages/${packageName}/jest.env.js`],
globalSetup: `<rootDir>/packages/${packageName}/jest.setup.ts`,
globalTeardown: `<rootDir>/packages/${packageName}/jest.teardown.js`,
testRegex: `(packages/${packageName}/.*/__tests__/.*|\\.(test|spec))\\.tsx?$`,
moduleDirectories: [`node_modules`, `packages/${packageName}/node_modules`],
modulePaths: [`<rootDir>/packages/${packageName}/src/`],
testEnvironment: `<rootDir>/packages/${packageName}/jest.custom-environment.ts`,
moduleDirectories: [
`node_modules`,
`packages/${packageName}/node_modules`,
`<rootDir>/node_modules`
],
modulePaths: [
`node_modules`,
`<rootDir>/packages/${packageName}/src/`,
`<rootDir>/node_modules`
],
id: packageName,
displayName: packageName,
rootDir: '../..'
Expand Down
9 changes: 9 additions & 0 deletions packages/card-service/jest.custom-environment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { TestEnvironment } from 'jest-environment-node'
import nock from 'nock'

export default class CustomEnvironment extends TestEnvironment {
constructor(config, context) {
super(config, context)
this.global.nock = nock
}
}
3 changes: 3 additions & 0 deletions packages/card-service/jest.env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Jest environment configuration for card-service
process.env.NODE_ENV = 'test'
process.env.LOG_LEVEL = process.env.LOG_LEVEL || 'silent'
88 changes: 88 additions & 0 deletions packages/card-service/jest.setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { knex } from 'knex'
import { GenericContainer, Wait } from 'testcontainers'
require('./jest.env') // set environment variables

const POSTGRES_PORT = 5432
const REDIS_PORT = 6379

const setup = async (globalConfig): Promise<void> => {
const workers = globalConfig.maxWorkers

const setupDatabase = async () => {
if (!process.env.DATABASE_URL) {
const postgresContainer = await new GenericContainer('postgres:15')
.withExposedPorts(POSTGRES_PORT)
.withBindMounts([
{
source: __dirname + '/scripts/init.sh',
target: '/docker-entrypoint-initdb.d/init.sh'
}
])
.withEnvironment({
POSTGRES_PASSWORD: 'password'
})
.withHealthCheck({
test: ['CMD-SHELL', 'pg_isready -d testing'],
interval: 10000,
timeout: 5000,
retries: 5
})
.withWaitStrategy(Wait.forHealthCheck())
.start()

process.env.DATABASE_URL = `postgresql://postgres:password@localhost:${postgresContainer.getMappedPort(
POSTGRES_PORT
)}/testing`

global.__CARD_SERVICE_POSTGRES__ = postgresContainer
}

const db = knex({
client: 'postgresql',
connection: process.env.DATABASE_URL,
pool: {
min: 2,
max: 10
},
migrations: {
tableName: 'knex_migrations'
}
})

// node pg defaults to returning bigint as string. This ensures it parses to bigint
db.client.driver.types.setTypeParser(
db.client.driver.types.builtins.INT8,
'text',
BigInt
)
await db.migrate.latest({
directory: __dirname + '/migrations'
})

for (let i = 1; i <= workers; i++) {
const workerDatabaseName = `testing_${i}`

await db.raw(`DROP DATABASE IF EXISTS ${workerDatabaseName}`)
await db.raw(`CREATE DATABASE ${workerDatabaseName} TEMPLATE testing`)
}

global.__CARD_SERVICE_KNEX__ = db
}

const setupRedis = async () => {
if (!process.env.REDIS_URL) {
const redisContainer = await new GenericContainer('redis:7')
.withExposedPorts(REDIS_PORT)
.start()

global.__CARD_SERVICE_REDIS__ = redisContainer
process.env.REDIS_URL = `redis://localhost:${redisContainer.getMappedPort(
REDIS_PORT
)}`
}
}

await Promise.all([setupDatabase(), setupRedis()])
}

export default setup
13 changes: 13 additions & 0 deletions packages/card-service/jest.teardown.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = async () => {
await global.__CARD_SERVICE_KNEX__.migrate.rollback(
{ directory: __dirname + '/migrations' },
true
)
await global.__CARD_SERVICE_KNEX__.destroy()
if (global.__CARD_SERVICE_POSTGRES__) {
await global.__CARD_SERVICE_POSTGRES__.stop()
}
if (global.__CARD_SERVICE_REDIS__) {
await global.__CARD_SERVICE_REDIS__.stop()
}
}
11 changes: 8 additions & 3 deletions packages/card-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
"scripts": {
"build": "pnpm clean && tsc --build tsconfig.json",
"clean": "rm -fr dist/",
"test": "jest",
"test": "NODE_OPTIONS=--experimental-vm-modules jest --passWithNoTests --maxWorkers=50%",
"test:ci": "NODE_OPTIONS=--experimental-vm-modules jest --passWithNoTests --maxWorkers=2",
"test:cov": "pnpm test -- --coverage",
"dev": "ts-node-dev --inspect=0.0.0.0:9229 --respawn --transpile-only src/index.ts",
"knex": "knex"
},
Expand All @@ -17,8 +19,8 @@
"@koa/cors": "^5.0.0",
"@koa/router": "^12.0.2",
"koa-bodyparser": "^4.4.1",
"knex": "^3.1.0",
"koa": "^2.15.4",
"knex": "^3.1.0",
"objection": "^3.1.5",
"pg": "^8.11.3",
"objection-db-errors": "^1.1.2",
Expand All @@ -31,6 +33,9 @@
"@types/koa__cors": "^5.0.0",
"@types/koa__router": "^12.0.4",
"@types/uuid": "^9.0.8",
"ts-node-dev": "^2.0.0"
"jest-environment-node": "^29.7.0",
"testcontainers": "^10.16.0",
"ts-node-dev": "^2.0.0",
"nock": "14.0.0-beta.19"
}
}
8 changes: 8 additions & 0 deletions packages/card-service/scripts/init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash
set -e

psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
DROP DATABASE IF EXISTS TESTING;
CREATE DATABASE testing;
CREATE DATABASE development;
EOSQL
37 changes: 37 additions & 0 deletions packages/card-service/src/tests/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Knex } from 'knex'
import { IocContract } from '@adonisjs/fold'

import { App, AppServices } from '../app'

export interface TestContainer {
cardServicePort: number
app: App
knex: Knex
connectionUrl: string
shutdown: () => Promise<void>
container: IocContract<AppServices>
}

export const createTestApp = async (
container: IocContract<AppServices>
): Promise<TestContainer> => {
const config = await container.use('config')
config.cardServicePort = 0

const app = new App(container)
await app.boot()
await app.startCardServiceServer(config.cardServicePort)

const knex = await container.use('knex')

return {
app,
cardServicePort: app.getCardServicePort(),
knex,
connectionUrl: process.env.DATABASE_URL || '',
shutdown: async () => {
await app.shutdown()
},
container
}
}
42 changes: 42 additions & 0 deletions packages/card-service/src/tests/tableManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { IocContract } from '@adonisjs/fold'
import { Knex } from 'knex'
import { AppServices } from '../app'

export async function truncateTable(
knex: Knex,
tableName: string
): Promise<void> {
const RAW = `TRUNCATE TABLE "${tableName}" RESTART IDENTITY CASCADE`
await knex.raw(RAW)
}

export async function truncateTables(
deps: IocContract<AppServices>
): Promise<void> {
const knex = await deps.use('knex')

const ignoreTables = [
'knex_migrations',
'knex_migrations_lock',
'card_service_knex_migrations',
'card_service_knex_migrations_lock'
]

const tables = await getTables(knex, ignoreTables)
if (tables.length > 0) {
const RAW = `TRUNCATE TABLE "${tables.join('","')}" RESTART IDENTITY CASCADE`
await knex.raw(RAW)
}
}

async function getTables(
knex: Knex,
ignoredTables: string[]
): Promise<string[]> {
const result = await knex.raw(
`SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname='public'`
)
return result.rows
.map((val: { tablename: string }) => val.tablename)
.filter((tableName: string) => !ignoredTables.includes(tableName))
}
15 changes: 10 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading