node.js 18 releases the first version of node:test,
backported to 16.17 (LTS)
node:test features become stable on node.js 20
release of timers, suites, skip/todo/only
node.js 22, first release of module mocking
But that it means that we can say goodbye to __insert_thing_ from Node.js!
– Archibald, minimalistic dev
node --test
import dotenv from 'dotenv';
import { spec } from 'node:test/reporters';
import { run } from 'node:test';
import process from 'node:process';
import fs from 'node:fs';
import path from 'node:path';
const files = fs.readdirSync(
path.resolve(__dirname),
{ recursive: true, encoding: 'utf8', },
).filter(
file => file.endsWith('.spec.ts'),
).map(
file => path.resolve(__dirname, file),
);
run({
setup() { dotenv.config({ path: '.env' }); },
files,
}).compose(spec).pipe(process.stdout);
import dotenv from 'dotenv';
import { spec } from 'node:test/reporters';
import { run } from 'node:test';
import process from 'node:process';
import fs from 'node:fs';
import path from 'node:path';
const files = fs.readdirSync(
path.resolve(__dirname),
{ recursive: true, encoding: 'utf8', },
).filter(
file => file.endsWith('.spec.ts'),
).map(
file => path.resolve(__dirname, file),
);
run({
setup() { dotenv.config({ path: '.env' }); },
files,
}).compose(spec).pipe(process.stdout);
test files
to run
execute with spec
reporter
Ah, but I shall need to install TypeScript anyways. What difference does it make from using ts-jest?
– Archibald, the comparative dev
** import if node > 20.5.1
node --import tsx
npm i -D tsx
Expectations
node:test uses assert instead of expect
Mocks
node:test has a mocking system similar to other testing frameworks
Something more
Module mocking, timers, coverage and more.
expect(
value
).toBe(
true
);
assert.ok(
value
);
assert.strictEqual(
value,
true
);
assert(condition, errorMessage);
# Negativos
doesNotMatch
doesNotReject
doesNotThrow
notDeepEqual
notDeepStrictEqual
notEqual
notStrictEqual
# Positivos
deepEqual (objetos, ==)
deepStrictEqual (objetos, ===)
equal
strictEqual
ifError
match
ok
rejects
throws
import {
it,
describe
} from 'node:test';
import assert from 'node:assert/strict';
import db, { sql } from '../../src';
describe('Connection', () => {
it('should connect to the database', async () => {
const results = await db.query(
sql`SELECT 2 + 3 as result`
);
assert.deepEqual(results, [{ result: 5 }]);
});
});
import {
it,
describe
} from 'node:test';
import assert from 'node:assert/strict';
import db, { sql } from '../../src';
describe('Connection', () => {
it('should connect to the database', /* .... */);
it('should call onError when failing', async (ctx) => {
const onError = ctx.mock.fn(); // CREATE MOCK
const results = await db.query(
sql`SE 2 + 3 as result`,
{ onError } // PASS MOCK
);
// EVAL MOCK
assert.strictEqual(onError.mock.callCount(), 1);
})
});
Alas, I must know repeat code all the time. Where art thou, beforeEach, afterEach?
– Archibald, the structuralist dev
I bet you don't have snapshots though.
– Archibald, the photographic dev
test('snapshot without serializer', (ctx) => {
t.assert.snapshot({
value1: 1, value2: 2
});
});
test('snapshot with serializer', (ctx) => {
t.assert.snapshot({
value3: 3, value4: 4
}, {
serializers: [
(value) => JSON.stringify(value)
]
});
});
import { it, describe } from 'node:test';
import assert from 'node:assert/strict';
import db, { sql } from '../../src';
describe('Connection', () => {
it('should log queries', async (ctx) => {
const mockedLogFn = ctx.mock.fn();
const mockedLogger = ctx.mock.module("logger", {
namedExports: { log: mockedLog }
})
await db.query(sql`SELECT 2 + 3 as result`);
// EVAL MOCK
assert.strictEqual(mockedLogFn.log.callCount(), 1);
const [call] = mockedLogFn.calls;
assert.strictEqual(call.arguments, [
'Executing query: SELECT 2 + 3 as result'
]);
})
});
(uses --experimental-test-module-mocks)
drink water, have sunshine, touch grass
~ negru
thank you!