Skip to content

Commit

Permalink
test(api): Add E2E tests to API
Browse files Browse the repository at this point in the history
  • Loading branch information
rajdip-b committed Feb 2, 2024
1 parent 5e3456c commit 4eaf58d
Show file tree
Hide file tree
Showing 36 changed files with 1,007 additions and 455 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,13 @@ jobs:
run: |
pnpm run lint:api
- name: Test
- name: Unit tests
run: |
pnpm run db:generate-types
pnpm run test:api
- name: E2E tests
run: |
docker compose up -d
pnpm run e2e:api
docker compose down
3 changes: 3 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
. "$(dirname -- "$0")/_/husky.sh"

pnpm run lint:fix && pnpm run prettier:fix && pnpm run test:api
docker compose up -d
pnpm run e2e:api
docker compose down
27 changes: 25 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ VERSION ?= $(shell echo $$1)
RELEASE_TYPE ?= $(shell echo $$2)
CURRENT_BRANCH := $(shell git rev-parse --abbrev-ref HEAD)

all: check_args update_version generate_changelog commit_changes tag_release push_changes switch_branch rebase_and_push
all: check_args update_version generate_changelog commit_changes tag_release push_changes switch_branch rebase_and_push api_e2e

release: check_args update_version generate_changelog commit_changes tag_release push_changes switch_branch rebase_and_push

api_e2e: run_api run_e2e docker_down

check_args:
@if [ -z "$(VERSION)" ]; then echo "Usage: make <version> <release_type>"; exit 1; fi
@if [ -z "$(RELEASE_TYPE)" ]; then echo "Usage: make <version> <release_type>"; exit 1; fi
Expand Down Expand Up @@ -41,4 +43,25 @@ rebase_and_push:
@echo "Rebasing main branch"
@git rebase main
@echo "Pushing the changes to $(RELEASE_TYPE)"
@git push origin $(RELEASE_TYPE) --tags
@git push origin $(RELEASE_TYPE) --tags


# -----------------------_E2E TESTS----------------------- #

docker_up:
@echo "Starting docker containers"
@docker compose up -d

run_api: docker_up
@echo "Running API"
@DATABASE_URL="postgresql://prisma:prisma@localhost:5433/tests" pnpm run dev:api&

run_e2e:
@sleep 10
@echo "Running tests"
@nx run api-e2e:e2e

docker_down: run_e2e
@echo "Stopping docker containers"
@docker compose down
@kill -9 $(shell lsof -t -i:4200)
18 changes: 0 additions & 18 deletions apps/api-e2e/.eslintrc.json

This file was deleted.

19 changes: 0 additions & 19 deletions apps/api-e2e/jest.config.ts

This file was deleted.

27 changes: 0 additions & 27 deletions apps/api-e2e/project.json

This file was deleted.

10 changes: 0 additions & 10 deletions apps/api-e2e/src/api/api.spec.ts

This file was deleted.

10 changes: 0 additions & 10 deletions apps/api-e2e/src/support/global-setup.ts

This file was deleted.

7 changes: 0 additions & 7 deletions apps/api-e2e/src/support/global-teardown.ts

This file was deleted.

10 changes: 0 additions & 10 deletions apps/api-e2e/src/support/test-setup.ts

This file was deleted.

13 changes: 0 additions & 13 deletions apps/api-e2e/tsconfig.json

This file was deleted.

9 changes: 0 additions & 9 deletions apps/api-e2e/tsconfig.spec.json

This file was deleted.

2 changes: 2 additions & 0 deletions apps/api/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ export default {
displayName: 'api',
preset: '../../jest.preset.js',
testEnvironment: 'node',
testMatch: ['**/*.spec.ts'],
testPathIgnorePatterns: ['.*.e2e.spec.ts'],
transform: {
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }]
},
Expand Down
12 changes: 12 additions & 0 deletions apps/api/jest.e2e-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/* eslint-disable */
export default {
displayName: 'api',
preset: '../../jest.preset.js',
testEnvironment: 'node',
testMatch: ['**/*.e2e.spec.ts'],
transform: {
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }]
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../coverage/apps/api'
}
13 changes: 13 additions & 0 deletions apps/api/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@
"jestConfig": "apps/api/jest.config.ts"
}
},
"test:e2e": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage-e2e/{projectRoot}"],
"options": {
"devServerTarget": "api:serve",
"jestConfig": "apps/api/jest.e2e-config.ts"
},
"configurations": {
"development": {
"devServerTarget": "api:serve:development"
}
}
},
"prisma:generate": {
"command": "prisma generate",
"options": {
Expand Down
35 changes: 35 additions & 0 deletions apps/api/src/app/app.e2e.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {
FastifyAdapter,
NestFastifyApplication
} from '@nestjs/platform-fastify'
import { Test } from '@nestjs/testing'
import { AppModule } from './app.module'

describe('Health Check', () => {
let app: NestFastifyApplication

beforeAll(async () => {
const moduleRef = await Test.createTestingModule({
imports: [AppModule]
}).compile()
app = moduleRef.createNestApplication<NestFastifyApplication>(
new FastifyAdapter()
)

await app.init()
await app.getHttpAdapter().getInstance().ready()
})

it(`should pass health check`, async () => {
const result = await app.inject({
method: 'GET',
url: '/health'
})
expect(result.statusCode).toEqual(200)
expect(result.body).toEqual('UP')
})

afterAll(async () => {
await app.close()
})
})
58 changes: 58 additions & 0 deletions apps/api/src/app/e2e.setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { Injectable, Logger, OnModuleInit } from '@nestjs/common'
import { PrismaClient } from '@prisma/client'

@Injectable()
export class E2ESetup implements OnModuleInit {
private readonly prisma: PrismaClient
private readonly logger: Logger = new Logger(E2ESetup.name)

constructor(prisma: PrismaClient) {
this.prisma = prisma
}

async onModuleInit() {
if (process.env.NODE_ENV === 'e2e') {
// Clean the DB
await this.prisma.user.deleteMany()

// Create admin user
const adminUser = await this.prisma.user.create({
data: {
email: '[email protected]',
isActive: true,
isAdmin: true,
isOnboardingFinished: true,
name: 'Admin'
}
})
this.logger.log(`Created admin user: ${adminUser.email}`)

// Create regular user
const regularUser = await this.prisma.user.create({
data: {
email: '[email protected]',
isActive: true,
isAdmin: false,
isOnboardingFinished: true,
name: 'John Doe'
}
})
this.logger.log(`Created regular user: ${regularUser.email}`)

// Create regular user's workspace
await this.prisma.workspace.create({
data: {
name: `My Workspace`,
description: 'My default workspace',
isDefault: true,
ownerId: regularUser.id,
lastUpdatedBy: {
connect: {
id: regularUser.id
}
}
}
})
}
}
}
Loading

0 comments on commit 4eaf58d

Please sign in to comment.