batteries
included

joel a. villarreal bertoldi

testing with node.js
and no external deps

idplans

let's talk about the things we use to make testing happen

#5:

patience.

2022

node.js 18 releases the first version of node:test, 
backported to 16.17 (LTS)

2023

node:test features become stable on node.js 20

2023

release of timers, suites, skip/todo/only

?

2024

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

YES*

*but keeping in mind that the most advanced features are feature-flagged as experimental, which means that they could break and change at any given time

CLI

Code

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

YES, but*

*node.js has a thing called loaders**.

** import if node > 20.5.1

node --import tsx
npm i -D tsx

let's migrate!

Expectations

node:test uses assert instead of expect

1.

Mocks

node:test has a mocking system similar to other testing frameworks

2.

Something more

Module mocking, timers, coverage and more.

3.

jest expect

node assert

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

node:assert

some of these go way back as of v0.1!

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 }]);
  });
});

test with asserts

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);
  })
});

test with mocks

Alas, I must know repeat code all the time. Where art thou, beforeEach, afterEach?

– Archibald, the structuralist dev

they're still
here and
the same.

before(), beforeEach(), after(), afterEach() work the exact same way as in jest

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)
    ]
  });
}); 

Stage 1 - Early Development

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'
    ]);
  })
});

test with module mocking

(uses --experimental-test-module-mocks)

  questions                   ??

tea sommelier questions are also welcome

drink water, have sunshine, touch grass

~ negru

thank you!

Batteries included: Testing with Node.js and no external deps

By Joel Alejandro Villarreal Bertoldi

Batteries included: Testing with Node.js and no external deps

  • 11