Skip to content

Commit

Permalink
Fix external assertions tests for Node.js 21
Browse files Browse the repository at this point in the history
The assertion message is different, which requires more creativity with the snapshots.
  • Loading branch information
novemberborn committed Dec 3, 2023
1 parent adbfcde commit 0492d32
Show file tree
Hide file tree
Showing 3 changed files with 221 additions and 10 deletions.
193 changes: 191 additions & 2 deletions test/external-assertions/snapshots/test.js.md
Expand Up @@ -4,7 +4,7 @@ The actual snapshot is saved in `test.js.snap`.

Generated by [AVA](https://avajs.dev).

## node assertion
## node assertion (node.js v18)

> Snapshot 1
Expand Down Expand Up @@ -46,7 +46,196 @@ Generated by [AVA](https://avajs.dev).
2 tests failed`

## expect error
## expect error (node.js v18)

> Snapshot 1
`␊
✘ [fail]: test Assertion failed␊
✘ [fail]: test async Assertion failed␊
─␊
test␊
Assertion failed: ␊
expect(received).toBeTruthy()␊
Received: false␊
Error: expect(received).toBeTruthy()␊
Received: false␊
at ---␊
at ---␊
at ---␊
at ---␊
at ---␊
at ---␊
at ---␊
at ---␊
at ---␊
test async␊
Assertion failed: ␊
expect(received).toBeTruthy()␊
Received: false␊
Error: expect(received).toBeTruthy()␊
Received: false␊
at ---␊
at ---␊
─␊
2 tests failed`

## node assertion (node.js v20)

> Snapshot 1
`␊
✘ [fail]: test Assertion failed␊
✘ [fail]: test async Assertion failed␊
─␊
test␊
Assertion failed: ␊
false == true␊
AssertionError [ERR_ASSERTION]: false == true␊
at ---␊
at ---␊
at ---␊
at ---␊
at ---␊
at ---␊
at ---␊
at ---␊
at ---␊
test async␊
Assertion failed: ␊
false == true␊
AssertionError [ERR_ASSERTION]: false == true␊
at ---␊
at ---␊
─␊
2 tests failed`

## expect error (node.js v20)

> Snapshot 1
`␊
✘ [fail]: test Assertion failed␊
✘ [fail]: test async Assertion failed␊
─␊
test␊
Assertion failed: ␊
expect(received).toBeTruthy()␊
Received: false␊
Error: expect(received).toBeTruthy()␊
Received: false␊
at ---␊
at ---␊
at ---␊
at ---␊
at ---␊
at ---␊
at ---␊
at ---␊
at ---␊
test async␊
Assertion failed: ␊
expect(received).toBeTruthy()␊
Received: false␊
Error: expect(received).toBeTruthy()␊
Received: false␊
at ---␊
at ---␊
─␊
2 tests failed`

## node assertion (node.js v21)

> Snapshot 1
`␊
✘ [fail]: test Assertion failed␊
✘ [fail]: test async Assertion failed␊
─␊
test␊
Assertion failed: ␊
The expression evaluated to a falsy value:␊
assert(false)␊
AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value:␊
assert(false)␊
at ---␊
at ---␊
at ---␊
at ---␊
at ---␊
at ---␊
at ---␊
at ---␊
at ---␊
test async␊
Assertion failed: ␊
false == true␊
AssertionError [ERR_ASSERTION]: false == true␊
at ---␊
at ---␊
─␊
2 tests failed`

## expect error (node.js v21)

> Snapshot 1
Expand Down
Binary file modified test/external-assertions/snapshots/test.js.snap
Binary file not shown.
38 changes: 30 additions & 8 deletions test/external-assertions/test.js
@@ -1,13 +1,35 @@
import process from 'node:process';

import test from '@ava/test';

import {fixture} from '../helpers/exec.js';

test('node assertion ', async t => {
const result = await t.throwsAsync(fixture(['assert-failure.js']));
t.snapshot(result.stdout.replaceAll('\r', '').replaceAll(/\/{3}/g, '//').replaceAll(/at.*\n/g, 'at ---\n'));
});
const snapshotStdout = (t, stdout) => {
const normalized = stdout
.replaceAll('\r', '')
.replaceAll(/\/{3}/g, '//')
.replaceAll(/(\b)at.*\n/g, '$1at ---\n');

t.log(process.versions.node.split('.')[0]);
t.snapshot(normalized);
};

const major = process.versions.node.split('.')[0];

for (const version of ['18', '20', '21']) {
// Tests need to be declared for all versions, so that snapshots can be
// updated by running `npx test-ava -u test/external-assertions/test.js` for
// each supported version. However only the tests for the current version
// can run, so skip the others.
const declare = version === major ? test : test.skip;

declare(`node assertion (node.js v${version})`, async t => {
const result = await t.throwsAsync(fixture(['assert-failure.js']));
snapshotStdout(t, result.stdout);
});

test('expect error ', async t => {
const result = await t.throwsAsync(fixture(['expect-failure.js']));
t.snapshot(result.stdout.replaceAll('\r', '').replaceAll(/\/{3}/g, '//').replaceAll(/at.*\n/g, 'at ---\n'));
});
declare(`expect error (node.js v${version})`, async t => {
const result = await t.throwsAsync(fixture(['expect-failure.js']));
snapshotStdout(t, result.stdout);
});
}

0 comments on commit 0492d32

Please sign in to comment.