I would like to create test suites and before all tests I'd like to run a process in a before hook. For organization purposes, I'd like to have a setup file that contains all project setup logic. Then there would be test suites to actually run the tests. My current file structure is: tests/setup.test.ts with the before and after hook and tests/employeeAPI/index.test.ts that contains tests for an employeeAPI. The code for this is below. For now, I've written trivial tests to ensure the before and after hooks are working as required, which currently they are not.
tests/setup.test.ts
import { before, after } from 'node:test';
before(async () => {
console.log("SETUP BEFORE RAN");
});
after(async () => {
console.log("SETUP AFTER RAN");
});
tests/employeeAPI/index.test.ts
import { describe, it } from 'node:test';
describe('Employee Test Suite', () => {
it('Dummy Employee Test', async (t) => {
assert.strictEqual(1, 1);
});
});
The test scripts are:
"test:compile": "swc --config-file ./.swcrc src --out-dir src && swc --config-file ./.swcrc tests --out-dir test",
"test:script": "node --experimental-specifier-resolution=node --test --experimental-test-coverage test",
"test": "npm run knex:latest && npm run test:compile && npm run test:script ",
The test logs are:
npm run knex:latest && npm run test:compile && npm run test:script
app-local-backend |
app-local-backend |
app-local-backend | > [email protected] knex:latest
app-local-backend | > node --loader ./node_modules/ts-node/esm/transpile-only.mjs ./node_modules/knex/bin/cli.js --knexfile ./src/db/knexfile.ts migrate:latest
app-local-backend |
app-local-backend | (node:30) ExperimentalWarning: Custom ESM Loaders is an experimental feature and might change at any time
app-local-backend | (Use `node --trace-warnings ...` to show where the warning was created)
app-local-backend | Working directory changed to /app/src/db
app-local-backend | Using environment: testing
app-local-backend | Batch 1 run: 3 migrations
app-local-backend |
app-local-backend | > [email protected] test:compile
app-local-backend | > swc --config-file ./.swcrc src --out-dir src && swc --config-file ./.swcrc tests --out-dir test
app-local-backend |
app-local-backend | Successfully compiled: 35 files with swc (80.23ms)
app-local-backend | Successfully compiled: 4 files with swc (26.71ms)
app-local-backend |
app-local-backend | > [email protected] test:script
app-local-backend | > node --experimental-specifier-resolution=node --test --experimental-test-coverage test
app-local-backend |
app-local-backend | (node:120) ExperimentalWarning: The Node.js specifier resolution flag is experimental. It could change or be removed at any time.
app-local-backend | (Use `node --trace-warnings ...` to show where the warning was created)
app-local-backend | TAP version 13
app-local-backend | # (node:127) ExperimentalWarning: The Node.js specifier resolution flag is experimental. It could change or be removed at any time.
app-local-backend | # (Use `node --trace-warnings ...` to show where the warning was created)
app-local-backend |
app-local-backend | # Subtest: Employee Test Suite
app-local-backend | # Subtest: Dummy Employee Test
app-local-backend | ok 1 - Dummy Employee Test
app-local-backend | ---
app-local-backend | duration_ms: 0.806551
app-local-backend | ...
app-local-backend | 1..1
app-local-backend | ok 2 - Employee Test Suite
app-local-backend | ---
app-local-backend | duration_ms: 4.464019
app-local-backend | type: 'suite'
app-local-backend | ...
app-local-backend | # (node:129) ExperimentalWarning: The Node.js specifier resolution flag is experimental. It could change or be removed at any time.
app-local-backend | # (Use `node --trace-warnings ...` to show where the warning was created)
app-local-backend | # Subtest: /app/test/sample.test.js
app-local-backend | ok 3 - /app/test/sample.test.js
app-local-backend | ---
app-local-backend | duration_ms: 136.870416
app-local-backend | ...
app-local-backend | # (node:130) ExperimentalWarning: The Node.js specifier resolution flag is experimental. It could change or be removed at any time.
app-local-backend | # (Use `node --trace-warnings ...` to show where the warning was created)
app-local-backend | # SETUP AFTER RAN
app-local-backend | 1..0
app-local-backend | # Subtest: /app/test/setup.test.js
app-local-backend | ok 4 - /app/test/setup.test.js
app-local-backend | ---
app-local-backend | duration_ms: 157.697727
app-local-backend | ...
app-local-backend | 1..4
app-local-backend | # tests 4
app-local-backend | # suites 1
app-local-backend | # pass 4
app-local-backend | # fail 0
app-local-backend | # cancelled 0
app-local-backend | # skipped 0
app-local-backend | # todo 0
app-local-backend | # duration_ms 191.367127
app-local-backend | # start of coverage report
app-local-backend | # file | line % | branch % | funcs % | uncovered lines
app-local-backend | # test/category/index.test.js | 100.00 | 100.00 | 100.00 |
app-local-backend | # test/offering/index.test.js | 100.00 | 100.00 | 100.00 |
app-local-backend | # test/sample.test.js | 100.00 | 100.00 | 100.00 |
app-local-backend | # test/setup.test.js | 75.00 | 66.67 | 50.00 | 4, 5, 6, 7, 8, 9, 10, 11, 21, 22, 23
app-local-backend | # all files | 95.74 | 87.50 | 75.00 |
app-local-backend | # end of coverage report
app-local-backend exited with code 0
The problem I'm facing is that the before doesn't seem to be running. From the logs, it is clear that the after hook is running but the before hook is not running. That being said, is there anything wrong with my current setup preventing the before to run?
Any help would be much appreciated!