From 11dc49e86585641531dc3f3adea1e15a42231ea5 Mon Sep 17 00:00:00 2001 From: Collin Duncan <3679940+cgduncan7@users.noreply.github.com> Date: Thu, 28 Mar 2024 17:57:16 +0100 Subject: [PATCH] Adding some good-ole unit tests to CI --- .github/workflows/main.yml | 11 +++++ package.json | 7 +--- src/recurringReservations/entity.ts | 6 +++ test/app.e2e-spec.ts | 25 ----------- test/{app.e2e.spec.ts => e2e/app.spec.ts} | 2 +- test/jest-e2e.json | 4 +- test/jest-unit.json | 9 ++++ .../unit/recurringReservations/entity.spec.ts | 42 +++++++++++++++++++ 8 files changed, 73 insertions(+), 33 deletions(-) delete mode 100644 test/app.e2e-spec.ts rename test/{app.e2e.spec.ts => e2e/app.spec.ts} (91%) create mode 100644 test/jest-unit.json create mode 100644 test/unit/recurringReservations/entity.spec.ts diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 14d7ef0..f07b2ff 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -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 diff --git a/package.json b/package.json index 4d62338..62447ac 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/src/recurringReservations/entity.ts b/src/recurringReservations/entity.ts index 589faeb..be97bb1 100644 --- a/src/recurringReservations/entity.ts +++ b/src/recurringReservations/entity.ts @@ -39,6 +39,10 @@ export class RecurringReservation { @Column('varchar', { length: 255, nullable: false }) opponentName: string + constructor(partial: Partial) { + 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, diff --git a/test/app.e2e-spec.ts b/test/app.e2e-spec.ts deleted file mode 100644 index 4b4f941..0000000 --- a/test/app.e2e-spec.ts +++ /dev/null @@ -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!') - }) -}) diff --git a/test/app.e2e.spec.ts b/test/e2e/app.spec.ts similarity index 91% rename from test/app.e2e.spec.ts rename to test/e2e/app.spec.ts index 6e068fe..3eab7ba 100644 --- a/test/app.e2e.spec.ts +++ b/test/e2e/app.spec.ts @@ -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 diff --git a/test/jest-e2e.json b/test/jest-e2e.json index e9d912f..ffeaf6d 100644 --- a/test/jest-e2e.json +++ b/test/jest-e2e.json @@ -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" } diff --git a/test/jest-unit.json b/test/jest-unit.json new file mode 100644 index 0000000..7c2ba09 --- /dev/null +++ b/test/jest-unit.json @@ -0,0 +1,9 @@ +{ + "moduleFileExtensions": ["js", "json", "ts"], + "rootDir": "./unit", + "testEnvironment": "node", + "testRegex": ".spec.ts$", + "transform": { + "^.+\\.(t|j)s$": "ts-jest" + } +} diff --git a/test/unit/recurringReservations/entity.spec.ts b/test/unit/recurringReservations/entity.spec.ts new file mode 100644 index 0000000..f7750dc --- /dev/null +++ b/test/unit/recurringReservations/entity.spec.ts @@ -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')) + }) + }) +})