Adding some good-ole unit tests to CI

This commit is contained in:
Collin Duncan 2024-03-28 17:57:16 +01:00
parent d41866b0ad
commit 11dc49e865
No known key found for this signature in database
8 changed files with 73 additions and 33 deletions

View file

@ -4,7 +4,18 @@ on:
branches:
- main
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v4
with:
node-version: lts/hydrogen
- run: npm ci
- run: npm run test:unit
build-image:
needs:
- test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

View file

@ -16,11 +16,8 @@
"start:prod": "node dist/main",
"repl": "npm run start -- --entryFile repl",
"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:unit": "jest --config ./test/jest-unit.json",
"migrations:generate": "npx typeorm-ts-node-commonjs migration:generate -d data-source.ts database/migrations/$npm_config_name",
"migrations": "npx typeorm-ts-node-commonjs migration:run -d data-source.ts"
},
@ -79,7 +76,7 @@
"json",
"ts"
],
"rootDir": "src",
"rootDir": "test",
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"

View file

@ -39,6 +39,10 @@ export class RecurringReservation {
@Column('varchar', { length: 255, nullable: false })
opponentName: string
constructor(partial: Partial<RecurringReservation>) {
Object.assign(this, partial)
}
@Exclude()
public createReservationInAdvance(daysInAdvance = 7): Reservation {
const [hourStart, minuteStart] = this.timeStart.split(':')
@ -48,11 +52,13 @@ export class RecurringReservation {
.set('hour', Number.parseInt(hourStart))
.set('minute', Number.parseInt(minuteStart))
.add(daysInAdvance, 'days')
.tz('Europe/Amsterdam', true)
const dateRangeEnd = dayjs()
.set('day', this.dayOfWeek)
.set('hour', Number.parseInt(hourEnd))
.set('minute', Number.parseInt(minuteEnd))
.add(daysInAdvance, 'days')
.tz('Europe/Amsterdam', true)
const reservation = new Reservation({
ownerId: this.ownerId,
dateRangeStart: dateRangeStart,

View file

@ -1,25 +0,0 @@
import { INestApplication } from '@nestjs/common'
import { Test, TestingModule } from '@nestjs/testing'
import * as request from 'supertest'
import { AppModule } from './../src/app.module'
describe('AppController (e2e)', () => {
let app: INestApplication
beforeEach(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile()
app = moduleFixture.createNestApplication()
await app.init()
})
it('/ (GET)', () => {
return request(app.getHttpServer())
.get('/')
.expect(200)
.expect('Hello World!')
})
})

View file

@ -2,7 +2,7 @@ import { INestApplication } from '@nestjs/common'
import { Test, TestingModule } from '@nestjs/testing'
import * as request from 'supertest'
import { AppModule } from '../src/app.module'
import { AppModule } from '../../src/app.module'
describe('AppController (e2e)', () => {
let app: INestApplication

View file

@ -1,8 +1,8 @@
{
"moduleFileExtensions": ["js", "json", "ts"],
"rootDir": ".",
"rootDir": "./e2e",
"testEnvironment": "node",
"testRegex": ".e2e-spec.ts$",
"testRegex": ".spec.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
}

9
test/jest-unit.json Normal file
View file

@ -0,0 +1,9 @@
{
"moduleFileExtensions": ["js", "json", "ts"],
"rootDir": "./unit",
"testEnvironment": "node",
"testRegex": ".spec.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
}
}

View file

@ -0,0 +1,42 @@
import dayjs from '../../../src/common/dayjs'
import { RecurringReservation } from '../../../src/recurringReservations/entity'
describe('recurringReservations.entity', () => {
describe('createReservationInAdvance', () => {
it('should create reservation at same time in 7 days', async () => {
jest.useFakeTimers().setSystemTime(new Date('2024-01-01'))
const rr = new RecurringReservation({
ownerId: '1',
timeStart: '18:30',
timeEnd: '19:15',
dayOfWeek: 2,
})
const reservation = rr.createReservationInAdvance(7)
expect(reservation.dateRangeStart).toEqual(dayjs('2024-01-09T18:30'))
})
it('should create reservation at same time in 7 days (DST --> NDST)', async () => {
jest.useFakeTimers().setSystemTime(new Date('2024-03-28'))
const rr = new RecurringReservation({
ownerId: '1',
timeStart: '18:30',
timeEnd: '19:15',
dayOfWeek: 4,
})
const reservation = rr.createReservationInAdvance(7)
expect(reservation.dateRangeStart).toEqual(dayjs('2024-04-04T18:30'))
})
it('should create reservation at same time in 7 days (NDST --> DST)', async () => {
jest.useFakeTimers().setSystemTime(new Date('2024-10-24'))
const rr = new RecurringReservation({
ownerId: '1',
timeStart: '18:30',
timeEnd: '19:15',
dayOfWeek: 4,
})
const reservation = rr.createReservationInAdvance(7)
expect(reservation.dateRangeStart).toEqual(dayjs('2024-10-31T18:30'))
})
})
})