From d28af514d17972cc0b4f6b6da2c953a5543cd575 Mon Sep 17 00:00:00 2001 From: Heini Date: Fri, 30 Oct 2026 13:28:48 +0200 Subject: [PATCH 1/2] feat: add possibility to override current examination period for testing future examination periods in integration tests --- .../__tests__/examination-period.test.ts | 46 ++++++++++++++++++- .../src/examination-period.ts | 3 ++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/packages/examination-utils/__tests__/examination-period.test.ts b/packages/examination-utils/__tests__/examination-period.test.ts index 955a088..03f1b42 100644 --- a/packages/examination-utils/__tests__/examination-period.test.ts +++ b/packages/examination-utils/__tests__/examination-period.test.ts @@ -1,4 +1,4 @@ -import { test, describe } from 'node:test' +import { test, describe, beforeEach } from 'node:test' import assert from 'node:assert/strict' describe('examination-period', () => { @@ -25,5 +25,49 @@ describe('examination-period', () => { const currentExaminationPeriod = getCurrentExaminationPeriod() assert.equal(currentExaminationPeriod, '2012S') }) + + describe('examination period override', () => { + beforeEach(() => { + delete process.env.EXAMINATION_PERIOD_OVERRIDE + }) + + test('should use the clock when EXAMINATION_PERIOD_OVERRIDE is undefined', async context => { + context.mock.timers.enable({ apis: ['Date'], now: new Date(2012, 0, 15) }) + + const { getCurrentExaminationPeriod } = await import('../src/examination-period.js') + assert.equal(getCurrentExaminationPeriod(), '2012K') + }) + + test('should use EXAMINATION_PERIOD_OVERRIDE when it is set', async context => { + context.mock.timers.enable({ apis: ['Date'], now: new Date(2012, 0, 15) }) + process.env.EXAMINATION_PERIOD_OVERRIDE = '2027K' + + const { getCurrentExaminationPeriod } = await import('../src/examination-period.js') + assert.equal(getCurrentExaminationPeriod(), '2027K') + }) + + test('should use the clock when EXAMINATION_PERIOD_OVERRIDE is empty', async context => { + context.mock.timers.enable({ apis: ['Date'], now: new Date(2012, 0, 15) }) + process.env.EXAMINATION_PERIOD_OVERRIDE = '' + + const { getCurrentExaminationPeriod } = await import('../src/examination-period.js') + assert.equal(getCurrentExaminationPeriod(), '2012K') + }) + + test('should use the clock when process does not exist, as in frontend bundles', async context => { + context.mock.timers.enable({ apis: ['Date'], now: new Date(2012, 0, 15) }) + const { getCurrentExaminationPeriod } = await import('../src/examination-period.js') + + const processDescriptor = Object.getOwnPropertyDescriptor(globalThis, 'process')! + Object.defineProperty(globalThis, 'process', { value: undefined, configurable: true }) + let examinationPeriod: string + try { + examinationPeriod = getCurrentExaminationPeriod() + } finally { + Object.defineProperty(globalThis, 'process', processDescriptor) + } + assert.equal(examinationPeriod, '2012K') + }) + }) }) }) diff --git a/packages/examination-utils/src/examination-period.ts b/packages/examination-utils/src/examination-period.ts index 25df380..9c82b7f 100644 --- a/packages/examination-utils/src/examination-period.ts +++ b/packages/examination-utils/src/examination-period.ts @@ -1,4 +1,7 @@ export function getCurrentExaminationPeriod(): string { + const override = globalThis.process?.env?.EXAMINATION_PERIOD_OVERRIDE + if (override) return override + const date = new Date() const year = date.getFullYear() const month = date.getMonth() + 1 From 3911f4a8885377be90e5e78fef16aa2d2f046bfa Mon Sep 17 00:00:00 2001 From: Heini Date: Tue, 3 Nov 2026 10:08:00 +0200 Subject: [PATCH 2/2] test: make test description clearer --- packages/examination-utils/__tests__/examination-period.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/examination-utils/__tests__/examination-period.test.ts b/packages/examination-utils/__tests__/examination-period.test.ts index 03f1b42..493c7c0 100644 --- a/packages/examination-utils/__tests__/examination-period.test.ts +++ b/packages/examination-utils/__tests__/examination-period.test.ts @@ -54,7 +54,7 @@ describe('examination-period', () => { assert.equal(getCurrentExaminationPeriod(), '2012K') }) - test('should use the clock when process does not exist, as in frontend bundles', async context => { + test('should use the clock when process does not exist, because in frontend the browser does not have node.js', async context => { context.mock.timers.enable({ apis: ['Date'], now: new Date(2012, 0, 15) }) const { getCurrentExaminationPeriod } = await import('../src/examination-period.js')