Skip to content
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

PDX-84: Add test environments #60

Draft
wants to merge 22 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 0 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ module.exports = {
root: true,
env: {
node: true,
jest: true,
},
ignorePatterns: ['.eslintrc.js'],
rules: {
Expand Down
15 changes: 15 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@ jobs:
shell: bash
run: yarn install --immutable

- name: Create database folder
run: mkdir -p .test/db

- name: Give permissions to test database
run: sudo chown -R $USER .test/db

- name: Init test database
run: docker compose up -d

- name: Wait for database
run: docker compose exec db bash -c 'while ! pg_isready -h localhost -p 5432 -U postgres; do sleep 1; done;'
Comment on lines +30 to +34
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it works, it seems okay to merge 👍🏻 As a note, I remember there is a service container feature in the GitHub Actions service. It would be nice to see if it's a good idea to utilize this feature in the future when you have some free time.

https://docs.github.com/en/actions/using-containerized-services/about-service-containers


- name: Test
run: yarn test:e2e

- name: Build
run: yarn build

Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,7 @@ lerna-debug.log*

# environment
.env
.env.*

# test database
.test
12 changes: 12 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: '3.8'
services:
db:
image: postgres:latest
volumes:
- ./.test/db:/var/lib/postgresql/data
ports:
- "15432:5432"
environment:
POSTGRES_USER: test
POSTGRES_PASSWORD: testpassword
POSTGRES_DB: 9c-rudolf-test
File renamed without changes.
38 changes: 12 additions & 26 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json",
"test": "dotenv -e .env.test -- vitest run",
"test:watch": "dotenv -e .env.test -- vitest",
"test:cov": "dotenv -e .env.test -- vitest run --coverage",
"test:debug": "dotenv -e .env.test -- vitest --inspect-brk --inspect --logHeapUsage --threads=false",
"test:e2e": "dotenv -e .env.test -- vitest run --config ./vitest.config.e2e.ts",
"prisma:deploy": "prisma migrate deploy"
},
"dependencies": {
Expand Down Expand Up @@ -48,42 +48,28 @@
"@nestjs/cli": "^10.0.0",
"@nestjs/schematics": "^10.0.0",
"@nestjs/testing": "^10.0.0",
"@swc/core": "^1.3.102",
"@types/express": "^4.17.17",
"@types/jest": "^29.5.2",
"@types/k6": "^0.47.0",
"@types/node": "^20.3.1",
"@types/supertest": "^2.0.12",
"@typescript-eslint/eslint-plugin": "^5.59.11",
"@typescript-eslint/parser": "^5.59.11",
"@vitest/coverage-v8": "^1.1.3",
"dotenv-cli": "^7.3.0",
"eslint": "^8.42.0",
"eslint-config-prettier": "^8.8.0",
"eslint-plugin-prettier": "^4.2.1",
"jest": "^29.5.0",
"prettier": "^2.8.8",
"source-map-support": "^0.5.21",
"supertest": "^6.3.3",
"ts-jest": "^29.1.0",
"ts-loader": "^9.4.3",
"ts-node": "^10.9.1",
"tsconfig-paths": "^4.2.0",
"typescript": "^5.1.3"
},
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
"typescript": "^5.1.3",
"unplugin-swc": "^1.4.4",
"vite-tsconfig-paths": "^4.2.3",
"vitest": "^1.1.3"
},
"packageManager": "[email protected]",
"resolutions": {
Expand Down
9 changes: 5 additions & 4 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ import { HttpResponseMiddleware } from './http-response.middleware';

@Module({
imports: [
PrismaModule,
ScheduleModule.forRoot(),
JobModule,
TransactionModule,
QueueModule,
ConfigModule.forRoot(),
CacheModule.register({ isGlobal: true }),
PrometheusModule.register(),

PrismaModule,
JobModule,
TransactionModule,
QueueModule,
TxModule,
],
controllers: [AppController],
Expand Down
3 changes: 2 additions & 1 deletion src/job/job.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { Module } from '@nestjs/common';
import { JobController } from './job.controller';
import { JobService } from './job.service';
import { TxModule } from 'src/tx/tx.module';
import { PrismaModule } from 'src/prisma/prisma.module';

@Module({
imports: [TxModule],
imports: [TxModule, PrismaModule],
controllers: [JobController],
providers: [JobService],
})
Expand Down
3 changes: 1 addition & 2 deletions src/prisma/prisma.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Global, Module } from '@nestjs/common';
import { Module } from '@nestjs/common';
import { PrismaService } from './prisma.service';

@Global()
@Module({
providers: [PrismaService],
exports: [PrismaService],
Expand Down
3 changes: 2 additions & 1 deletion src/queue/queue.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { QueueCronController } from './queue.cron';
import { TxModule } from 'src/tx/tx.module';
import { ConfigModule } from '@nestjs/config';
import { makeGaugeProvider } from '@willsoto/nestjs-prometheus';
import { PrismaModule } from 'src/prisma/prisma.module';

@Module({
imports: [TxModule, ConfigModule],
imports: [TxModule, ConfigModule, PrismaModule],
providers: [
QueueService,
QueueCronController,
Expand Down
3 changes: 2 additions & 1 deletion src/transaction/transaction.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { TransactionService } from './transaction.service';
import { TransactionController } from './transaction.controller';
import { TransactionCronController } from './transaction.cron';
import { TxModule } from 'src/tx/tx.module';
import { PrismaModule } from 'src/prisma/prisma.module';

@Module({
imports: [TxModule],
imports: [TxModule, PrismaModule],
providers: [TransactionService, TransactionCronController],
controllers: [TransactionController],
})
Expand Down
16 changes: 14 additions & 2 deletions test/app.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import * as request from 'supertest';
import { AppModule } from './../src/app.module';
import { describe, beforeEach, it, expect } from 'vitest';
import request from 'supertest';
import { AppModule } from '../src/app.module';
import { PrismaService } from '../src/prisma/prisma.service';

describe('AppController (e2e)', () => {
let app: INestApplication;
Expand All @@ -21,4 +23,14 @@ describe('AppController (e2e)', () => {
.expect(200)
.expect('Hello World!');
});

it('SQL SELECT 1', async () => {
const prisma = app.get(PrismaService);
type Result = { result: 1 };
const [result] = await prisma.$queryRaw<Result[]>`
SELECT 1 result;
`;

expect(result.result).toEqual(1);
});
});
9 changes: 0 additions & 9 deletions test/jest-e2e.json

This file was deleted.

2 changes: 1 addition & 1 deletion tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"extends": "./tsconfig.json",
"exclude": ["node_modules", "test", "dist", "**/*spec.ts"]
"exclude": ["node_modules", "test", "dist", "**/*spec.ts", "k6"]
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "ES2021",
"target": "es2021",
moreal marked this conversation as resolved.
Show resolved Hide resolved
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
Expand Down
18 changes: 18 additions & 0 deletions vitest.config.e2e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import swc from 'unplugin-swc';
import { defineConfig } from 'vitest/config';
import tsconfigPaths from 'vite-tsconfig-paths';

export default defineConfig({
test: {
include: ['**/*.e2e-spec.ts'],
exclude: ['k6/**/*', 'node_modules/**/*'],
globals: true,
root: './',
},
plugins: [
swc.vite({
module: { type: 'es6' },
}),
tsconfigPaths(),
],
});
17 changes: 17 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import swc from 'unplugin-swc';
import { defineConfig } from 'vitest/config';
import tsconfigPaths from 'vite-tsconfig-paths';

export default defineConfig({
test: {
exclude: ['k6/**/*', 'node_modules/**/*'],
globals: true,
root: './',
},
plugins: [
swc.vite({
module: { type: 'es6' },
}),
tsconfigPaths(),
],
});
Loading
Loading