Skip to content

Commit

Permalink
test: migrate pseudo_tty tests to use assertSnapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
MoLow committed Apr 30, 2023
1 parent 8da7ab3 commit dc38eb4
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 65 deletions.
37 changes: 34 additions & 3 deletions test/common/assertSnapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ const common = require('.');
const path = require('node:path');
const fs = require('node:fs/promises');
const assert = require('node:assert/strict');
const pty = require('../../tools/node_modules/node-pty');


const stackFramesRegexp = /(\s+)((.+?)\s+\()?(?:\(?(.+?):(\d+)(?::(\d+))?)\)?(\s+\{)?(\n|$)/g;
// eslint-disable-next-line no-control-regex
const stackFramesRegexp = /(\s+)((.+?)\s+\()?(?:\(?(.+?):(\d+)(?::(\d+))?)\)?(\s+\{)?(\u001b\[\d+m)?(\n|$)/g;
const windowNewlineRegexp = /\r/g;

function replaceStackTrace(str, replacement = '$1*$7\n') {
Expand Down Expand Up @@ -39,6 +41,33 @@ async function assertSnapshot(actual, filename = process.argv[1]) {
}
}

function spawnPTYPromisifyied(...args) {
const stderr = '';
let stdout = '';

const child = pty.spawn(...args);
child.onData((data) => { stdout += data; });

return new Promise((resolve, reject) => {
child.on('close', (code, signal) => {
resolve({
code,
signal,
stderr,
stdout,
});
});
child.on('error', (code, signal) => {
reject({
code,
signal,
stderr,
stdout,
});
});
});
}

/**
* Spawn a process and assert its output against a snapshot.
* if you want to automatically update the snapshot, run tests with NODE_REGENERATE_SNAPSHOTS=1
Expand All @@ -53,9 +82,11 @@ async function assertSnapshot(actual, filename = process.argv[1]) {
* @param {function(string): string} [transform]
* @returns {Promise<void>}
*/
async function spawnAndAssert(filename, transform = (x) => x) {
async function spawnAndAssert(filename, transform = (x) => x, options = {}) {
const flags = common.parseTestFlags(filename);
const { stdout, stderr } = await common.spawnPromisified(process.execPath, [...flags, filename]);
const { stdout, stderr } = await (options.tty ?
spawnPTYPromisifyied(process.execPath, [...flags, filename]) :
common.spawnPromisified(process.execPath, [...flags, filename]));
await assertSnapshot(transform(`${stdout}${stderr}`), filename);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ process.env.FORCE_COLOR = '1';
delete process.env.NODE_DISABLE_COLORS;
delete process.env.NO_COLOR;

require('../common');
require('../../../common');
const test = require('node:test');

test('should pass', () => {});
Expand Down
57 changes: 57 additions & 0 deletions test/fixtures/test-runner/output/default_output.snapshot
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
✔ should pass (*ms)
✖ should fail (*ms)
Error: fail
*
*
*
*
*
*
*

﹣ should skip (*ms) # SKIP
▶ parent
✖ should fail (*ms)
Error: fail
*
*
*
*
*

✖ should pass but parent fail (*ms)
'test did not finish before its parent and was cancelled'

▶ parent (*ms)

ℹ tests 6
ℹ suites 0
ℹ pass 1
ℹ fail 3
ℹ cancelled 1
ℹ skipped 1
ℹ todo 0
ℹ duration_ms *

✖ failing tests:

✖ should fail (*ms)
Error: fail
*
*
*
*
*
*
*

✖ should fail (*ms)
Error: fail
*
*
*
*
*

✖ should pass but parent fail (*ms)
'test did not finish before its parent and was cancelled'
10 changes: 6 additions & 4 deletions test/parallel/test-runner-output.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ function replaceSpecDuration(str) {
return str
.replaceAll(/\(0(\r?\n)ms\)/g, '(ZEROms)')
.replaceAll(/[0-9.]+ms/g, '*ms')
.replaceAll(/duration_ms [0-9.]+/g, 'duration_ms *');
.replaceAll(/duration_ms [0-9.]+/g, 'duration_ms *')
.replace(new RegExp(`(\\u001b\\[\\d+m)\\(${process.cwd()}/?(\\u001b\\[\\d+m)(.*)(\\u001b\\[\\d+m)\\)`, 'g'), '$3');
}
const defaultTransform = snapshot
.transform(snapshot.replaceWindowsLineEndings, snapshot.replaceStackTrace, replaceTestDuration);
const specTransform = snapshot
.transform(snapshot.replaceWindowsLineEndings, snapshot.replaceStackTrace, replaceSpecDuration);
.transform(replaceSpecDuration, snapshot.replaceWindowsLineEndings, snapshot.replaceStackTrace);


const tests = [
Expand All @@ -40,10 +41,11 @@ const tests = [
{ name: 'test-runner/output/name_pattern.js' },
{ name: 'test-runner/output/name_pattern_with_only.js' },
{ name: 'test-runner/output/unresolved_promise.js' },
].map(({ name, transform }) => ({
{ name: 'test-runner/output/default_output.js', transform: specTransform, tty: true },
].map(({ name, tty, transform }) => ({
name,
fn: common.mustCall(async () => {
await snapshot.spawnAndAssert(fixtures.path(name), transform ?? defaultTransform);
await snapshot.spawnAndAssert(fixtures.path(name), transform ?? defaultTransform, { tty });
}),
}));

Expand Down
57 changes: 0 additions & 57 deletions test/pseudo-tty/test_runner_default_reporter.out

This file was deleted.

0 comments on commit dc38eb4

Please sign in to comment.