Skip to content

Commit

Permalink
test: migrate snapshots to snap (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
MoLow authored Mar 13, 2024
1 parent aa2a482 commit 1d3ca6a
Show file tree
Hide file tree
Showing 44 changed files with 272 additions and 310 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
},
"homepage": "https://github.com/MoLow/reporters#readme",
"devDependencies": {
"@matteo.collina/snap": "^0.2.1",
"c8": "^7.13.0",
"eslint": "^7.32.0 || ^8.2.0",
"eslint-config-airbnb-base": "^15.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"stderr": "",
"stdout": ".\n",
"exitCode": 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"stderr": "node:internal/test_runner/harness:*\n throw err;\n ^\n\nError: Bail\n *\n at bail.next (<anonymous>)\n *\n *\n *\n *\n *\n *\n *\n *\nEmitted 'error' event on Duplex instance at:\n *\n *\n *\n\nNode.js *\n",
"stdout": "X\n\u001b[31m✖ Bailing on failed test: fail\u001b[0m\n",
"exitCode": 7
}
2 changes: 2 additions & 0 deletions packages/bail/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
module.exports = async function* bail(source) {
for await (const event of source) {
if (event.type === 'test:fail') {
/* c8 ignore start */
yield `\n\u001b[31m✖ Bailing on failed test: ${event.data.name}\u001b[0m\n`;
throw new Error('Bail');
/* c8 ignore stop */
}
}
};
15 changes: 7 additions & 8 deletions packages/bail/tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,23 @@

const { test } = require('node:test');
const { spawnSync } = require('child_process');
const assert = require('assert');
const { Snap } = require('../../../tests/utils');

test('pass should not interfere with passing test', () => {
const snapshot = Snap(__filename);
test('pass should not interfere with passing test', async () => {
const child = spawnSync(process.execPath, [
'--test-reporter', 'dot', '--test-reporter-destination', 'stdout',
'--test-reporter', './index.js', '--test-reporter-destination', 'stdout', 'tests/fixtures/pass.js',
], { env: {} });
assert.strictEqual(child.stderr?.toString(), '');
assert.strictEqual(child.stdout?.toString(), '.\n');
assert.strictEqual(child.status, 0);

await snapshot(child);
});

test('fail should stop after failed test', () => {
test('fail should stop after failed test', async () => {
const child = spawnSync(process.execPath, [
'--test-reporter', 'dot', '--test-reporter-destination', 'stdout',
'--test-reporter', './index.js', '--test-reporter-destination', 'stdout',
'tests/fixtures/fail.js', 'tests/fixtures/pass.js',
], { env: {} });
assert.strictEqual(child.stdout?.toString(), 'X\n\x1B[31m✖ Bailing on failed test: fail\x1B[0m\n');
assert.strictEqual(child.status, 7);
await snapshot(child);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"stderr": "",
"stdout": "::debug::starting to run tests\n::debug::starting to run is ok\n::debug::completed running is ok\n::debug::starting to run fails\n::error title=fails,file=tests/example.js,line=9,col=11::Error [ERR_TEST_FAILURE]: this is an error%0A at async Promise.all (index 0) {%0A code: 'ERR_TEST_FAILURE',%0A failureType: 'testCodeFailure',%0A cause: Error: this is an error%0A at TestContext.<anonymous> (CWD/tests/example.js:9:11)%0A at Test.runInAsyncScope (node:async_hooks:206:9)%0A at Test.run (node:internal/test_runner/test:639:25)%0A at Suite.processPendingSubtests (node:internal/test_runner/test:382:18)%0A at Test.postRun (node:internal/test_runner/test:730:19)%0A at Test.run (node:internal/test_runner/test:688:12)%0A at async Promise.all (index 0)%0A at async Suite.run (node:internal/test_runner/test:964:7)%0A at async startSubtest (node:internal/test_runner/harness:218:3)%0A}\n::debug::starting to run is a diagnostic\n::debug::completed running is a diagnostic\n::notice file=tests/example.js,line=11,col=3::this is a diagnostic\n::debug::starting to run should fail\n::error title=should fail,file=tests/example.js,line=12,col=31::[Error [ERR_TEST_FAILURE]: The expression evaluated to a falsy value:%0A%0A assert(false)%0A] {%0A code: 'ERR_TEST_FAILURE',%0A failureType: 'testCodeFailure',%0A cause: AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value:%0A %0A assert(false)%0A %0A at TestContext.<anonymous> (CWD/tests/example.js:12:31)%0A at Test.runInAsyncScope (node:async_hooks:206:9)%0A at Test.run (node:internal/test_runner/test:639:25)%0A at Suite.processPendingSubtests (node:internal/test_runner/test:382:18)%0A at Test.postRun (node:internal/test_runner/test:730:19)%0A at Test.run (node:internal/test_runner/test:688:12)%0A at async Suite.processPendingSubtests (node:internal/test_runner/test:382:7) {%0A generatedMessage: true,%0A code: 'ERR_ASSERTION',%0A actual: false,%0A expected: true,%0A operator: '=='%0A }%0A}\n::debug::starting to run more tests\n::debug::starting to run is ok\n::debug::completed running is ok\n::debug::completed running more tests\n::debug::starting to run is skipped\n::debug::completed running is skipped\n::debug::starting to run is a todo\n::debug::completed running is a todo\n::debug::starting to run top level diagnostic\n::debug::completed running top level diagnostic\n::notice file=tests/example.js,line=21,col=1::top level diagnostic\n::group::Test results (4 passed, 2 failed)\n::notice::Total Tests: 8%0ASuites 📂: 2%0APassed ✅: 4%0AFailed ❌: 2%0ACanceled 🚫: 0%0ASkipped ⏭️: 1%0ATodo 📝: 1%0ADuration 🕐: *ms\n::endgroup::\n",
"exitCode": 1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"<h1>Test Results</h1>\n<table><tr><td>Total Tests</td><td>8</td></tr><tr><td>Suites 📂</td><td>2</td></tr><tr><td>Passed ✅</td><td>4</td></tr><tr><td>Failed ❌</td><td>2</td></tr><tr><td>Canceled 🚫</td><td>0</td></tr><tr><td>Skipped ⏭️</td><td>1</td></tr><tr><td>Todo 📝</td><td>1</td></tr><tr><td>Duration 🕐</td><td>*ms</td></tr></table>\n"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"stderr": "",
"stdout": "::debug::starting to run should fail\n::error title=should fail,file=tests/example.mjs,line=5,col=3::[Error [ERR_TEST_FAILURE]: The expression evaluated to a falsy value:%0A%0A assert(false)%0A] {%0A code: 'ERR_TEST_FAILURE',%0A failureType: 'testCodeFailure',%0A cause: AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value:%0A %0A assert(false)%0A %0A at TestContext.<anonymous> (file://CWD/tests/example.mjs:5:3)%0A at Test.runInAsyncScope (node:async_hooks:206:9)%0A at Test.run (node:internal/test_runner/test:639:25)%0A at Test.start (node:internal/test_runner/test:550:17)%0A at startSubtest (node:internal/test_runner/harness:218:17) {%0A generatedMessage: true,%0A code: 'ERR_ASSERTION',%0A actual: false,%0A expected: true,%0A operator: '=='%0A }%0A}\n::debug::starting to run should pass\n::debug::completed running should pass\n::group::Test results (1 passed, 1 failed)\n::notice::Total Tests: 2%0ASuites 📂: 0%0APassed ✅: 1%0AFailed ❌: 1%0ACanceled 🚫: 0%0ASkipped ⏭️: 0%0ATodo 📝: 0%0ADuration 🕐: *ms\n::endgroup::\n",
"exitCode": 1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"<h1>Test Results</h1>\n<table><tr><td>Total Tests</td><td>2</td></tr><tr><td>Suites 📂</td><td>0</td></tr><tr><td>Passed ✅</td><td>1</td></tr><tr><td>Failed ❌</td><td>1</td></tr><tr><td>Canceled 🚫</td><td>0</td></tr><tr><td>Skipped ⏭️</td><td>0</td></tr><tr><td>Todo 📝</td><td>0</td></tr><tr><td>Duration 🕐</td><td>*ms</td></tr></table>\n"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"stderr": "",
"stdout": "",
"exitCode": 1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"stderr": "",
"stdout": "::debug::starting to run tests\n::debug::starting to run is ok\n::debug::completed running is ok\n::debug::starting to run fails\n::error title=fails,file=tests/example.js,line=9,col=11::[Error [ERR_TEST_FAILURE]: this is an error] {%0A failureType: 'testCodeFailure',%0A cause: Error: this is an error%0A at TestContext.<anonymous> (CWD/tests/example.js:9:11)%0A at Test.runInAsyncScope (node:async_hooks:203:9)%0A at Test.run (node:internal/test_runner/test:631:25)%0A at Suite.processPendingSubtests (node:internal/test_runner/test:374:18)%0A at Test.postRun (node:internal/test_runner/test:715:19)%0A at Test.run (node:internal/test_runner/test:673:12)%0A at async Promise.all (index 0)%0A at async Suite.run (node:internal/test_runner/test:948:7)%0A at async startSubtest (node:internal/test_runner/harness:214:3),%0A code: 'ERR_TEST_FAILURE'%0A}\n::debug::starting to run is a diagnostic\n::debug::completed running is a diagnostic\n::notice file=tests/example.js,line=11,col=3::this is a diagnostic\n::debug::starting to run should fail\n::error title=should fail,file=tests/example.js,line=12,col=31::[Error [ERR_TEST_FAILURE]: The expression evaluated to a falsy value:%0A%0A assert(false)%0A] {%0A failureType: 'testCodeFailure',%0A cause: AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value:%0A %0A assert(false)%0A %0A at TestContext.<anonymous> (CWD/tests/example.js:12:31)%0A at Test.runInAsyncScope (node:async_hooks:203:9)%0A at Test.run (node:internal/test_runner/test:631:25)%0A at Suite.processPendingSubtests (node:internal/test_runner/test:374:18)%0A at Test.postRun (node:internal/test_runner/test:715:19)%0A at Test.run (node:internal/test_runner/test:673:12)%0A at async Suite.processPendingSubtests (node:internal/test_runner/test:374:7) {%0A generatedMessage: true,%0A code: 'ERR_ASSERTION',%0A actual: false,%0A expected: true,%0A operator: '=='%0A },%0A code: 'ERR_TEST_FAILURE'%0A}\n::debug::starting to run more tests\n::debug::starting to run is ok\n::debug::completed running is ok\n::debug::completed running more tests\n::debug::starting to run is skipped\n::debug::completed running is skipped\n::debug::starting to run is a todo\n::debug::completed running is a todo\n::debug::starting to run top level diagnostic\n::debug::completed running top level diagnostic\n::notice file=tests/example.js,line=21,col=1::top level diagnostic\n::group::Test results (4 passed, 2 failed)\n::notice::Total Tests: 8%0ASuites 📂: 2%0APassed ✅: 4%0AFailed ❌: 2%0ACanceled 🚫: 0%0ASkipped ⏭️: 1%0ATodo 📝: 1%0ADuration 🕐: *ms\n::endgroup::\n",
"exitCode": 1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"<h1>Test Results</h1>\n<table><tr><td>Total Tests</td><td>8</td></tr><tr><td>Suites 📂</td><td>2</td></tr><tr><td>Passed ✅</td><td>4</td></tr><tr><td>Failed ❌</td><td>2</td></tr><tr><td>Canceled 🚫</td><td>0</td></tr><tr><td>Skipped ⏭️</td><td>1</td></tr><tr><td>Todo 📝</td><td>1</td></tr><tr><td>Duration 🕐</td><td>*ms</td></tr></table>\n"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"stderr": "",
"stdout": "::debug::starting to run should fail\n::error title=should fail,file=tests/example.mjs,line=5,col=3::[Error [ERR_TEST_FAILURE]: false == true] {%0A failureType: 'testCodeFailure',%0A cause: AssertionError [ERR_ASSERTION]: false == true%0A at TestContext.<anonymous> (file://CWD/tests/example.mjs:5:3)%0A at Test.runInAsyncScope (node:async_hooks:203:9)%0A at Test.run (node:internal/test_runner/test:631:25)%0A at Test.start (node:internal/test_runner/test:542:17)%0A at startSubtest (node:internal/test_runner/harness:214:17) {%0A generatedMessage: true,%0A code: 'ERR_ASSERTION',%0A actual: false,%0A expected: true,%0A operator: '=='%0A },%0A code: 'ERR_TEST_FAILURE'%0A}\n::debug::starting to run should pass\n::debug::completed running should pass\n::group::Test results (1 passed, 1 failed)\n::notice::Total Tests: 2%0ASuites 📂: 0%0APassed ✅: 1%0AFailed ❌: 1%0ACanceled 🚫: 0%0ASkipped ⏭️: 0%0ATodo 📝: 0%0ADuration 🕐: *ms\n::endgroup::\n",
"exitCode": 1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"<h1>Test Results</h1>\n<table><tr><td>Total Tests</td><td>2</td></tr><tr><td>Suites 📂</td><td>0</td></tr><tr><td>Passed ✅</td><td>1</td></tr><tr><td>Failed ❌</td><td>1</td></tr><tr><td>Canceled 🚫</td><td>0</td></tr><tr><td>Skipped ⏭️</td><td>0</td></tr><tr><td>Todo 📝</td><td>0</td></tr><tr><td>Duration 🕐</td><td>*ms</td></tr></table>\n"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"stderr": "",
"stdout": "",
"exitCode": 1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"stderr": "",
"stdout": "::debug::starting to run tests\n::debug::starting to run is ok\n::debug::completed running is ok\n::debug::starting to run fails\n::error title=fails,file=tests/example.js,line=9,col=11::[Error [ERR_TEST_FAILURE]: this is an error] {%0A code: 'ERR_TEST_FAILURE',%0A failureType: 'testCodeFailure',%0A cause: Error: this is an error%0A at TestContext.<anonymous> (CWD/tests/example.js:9:11)%0A at Test.runInAsyncScope (node:async_hooks:206:9)%0A at Test.run (node:internal/test_runner/test:631:25)%0A at Suite.processPendingSubtests (node:internal/test_runner/test:374:18)%0A at Test.postRun (node:internal/test_runner/test:715:19)%0A at Test.run (node:internal/test_runner/test:673:12)%0A at async Promise.all (index 0)%0A at async Suite.run (node:internal/test_runner/test:948:7)%0A at async startSubtest (node:internal/test_runner/harness:216:3)%0A}\n::debug::starting to run is a diagnostic\n::debug::completed running is a diagnostic\n::notice file=tests/example.js,line=11,col=3::this is a diagnostic\n::debug::starting to run should fail\n::error title=should fail,file=tests/example.js,line=12,col=31::[Error [ERR_TEST_FAILURE]: The expression evaluated to a falsy value:%0A%0A assert(false)%0A] {%0A code: 'ERR_TEST_FAILURE',%0A failureType: 'testCodeFailure',%0A cause: AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value:%0A %0A assert(false)%0A %0A at TestContext.<anonymous> (CWD/tests/example.js:12:31)%0A at Test.runInAsyncScope (node:async_hooks:206:9)%0A at Test.run (node:internal/test_runner/test:631:25)%0A at Suite.processPendingSubtests (node:internal/test_runner/test:374:18)%0A at Test.postRun (node:internal/test_runner/test:715:19)%0A at Test.run (node:internal/test_runner/test:673:12)%0A at async Suite.processPendingSubtests (node:internal/test_runner/test:374:7) {%0A generatedMessage: true,%0A code: 'ERR_ASSERTION',%0A actual: false,%0A expected: true,%0A operator: '=='%0A }%0A}\n::debug::starting to run more tests\n::debug::starting to run is ok\n::debug::completed running is ok\n::debug::completed running more tests\n::debug::starting to run is skipped\n::debug::completed running is skipped\n::debug::starting to run is a todo\n::debug::completed running is a todo\n::debug::starting to run top level diagnostic\n::debug::completed running top level diagnostic\n::notice file=tests/example.js,line=21,col=1::top level diagnostic\n::group::Test results (4 passed, 2 failed)\n::notice::Total Tests: 8%0ASuites 📂: 2%0APassed ✅: 4%0AFailed ❌: 2%0ACanceled 🚫: 0%0ASkipped ⏭️: 1%0ATodo 📝: 1%0ADuration 🕐: *ms\n::endgroup::\n",
"exitCode": 1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"<h1>Test Results</h1>\n<table><tr><td>Total Tests</td><td>8</td></tr><tr><td>Suites 📂</td><td>2</td></tr><tr><td>Passed ✅</td><td>4</td></tr><tr><td>Failed ❌</td><td>2</td></tr><tr><td>Canceled 🚫</td><td>0</td></tr><tr><td>Skipped ⏭️</td><td>1</td></tr><tr><td>Todo 📝</td><td>1</td></tr><tr><td>Duration 🕐</td><td>*ms</td></tr></table>\n"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"stderr": "",
"stdout": "::debug::starting to run should fail\n::error title=should fail,file=tests/example.mjs,line=5,col=3::[Error [ERR_TEST_FAILURE]: The expression evaluated to a falsy value:%0A%0A assert(false)%0A] {%0A code: 'ERR_TEST_FAILURE',%0A failureType: 'testCodeFailure',%0A cause: AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value:%0A %0A assert(false)%0A %0A at TestContext.<anonymous> (file://CWD/tests/example.mjs:5:3)%0A at Test.runInAsyncScope (node:async_hooks:206:9)%0A at Test.run (node:internal/test_runner/test:631:25)%0A at Test.start (node:internal/test_runner/test:542:17)%0A at startSubtest (node:internal/test_runner/harness:216:17) {%0A generatedMessage: true,%0A code: 'ERR_ASSERTION',%0A actual: false,%0A expected: true,%0A operator: '=='%0A }%0A}\n::debug::starting to run should pass\n::debug::completed running should pass\n::group::Test results (1 passed, 1 failed)\n::notice::Total Tests: 2%0ASuites 📂: 0%0APassed ✅: 1%0AFailed ❌: 1%0ACanceled 🚫: 0%0ASkipped ⏭️: 0%0ATodo 📝: 0%0ADuration 🕐: *ms\n::endgroup::\n",
"exitCode": 1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"<h1>Test Results</h1>\n<table><tr><td>Total Tests</td><td>2</td></tr><tr><td>Suites 📂</td><td>0</td></tr><tr><td>Passed ✅</td><td>1</td></tr><tr><td>Failed ❌</td><td>1</td></tr><tr><td>Canceled 🚫</td><td>0</td></tr><tr><td>Skipped ⏭️</td><td>0</td></tr><tr><td>Todo 📝</td><td>0</td></tr><tr><td>Duration 🕐</td><td>*ms</td></tr></table>\n"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"stderr": "",
"stdout": "",
"exitCode": 1
}
27 changes: 8 additions & 19 deletions packages/github/tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,36 @@ const { test, describe, beforeEach } = require('node:test');
const { spawnSync } = require('child_process');
const { tmpdir } = require('os');
const { join } = require('path');
const assert = require('assert');
const path = require('path');
const { readFileSync, writeFileSync } = require('fs');
const { compareLines } = require('../../../tests/utils');
const output = require('./output');
const outputESM = require('./output-esm');
const { Snap, nodeMajor } = require('../../../tests/utils');

const snapshot = Snap(`${__filename}.${nodeMajor}`);
const GITHUB_STEP_SUMMARY = join(tmpdir(), 'github-actions-test-reporter');
const nodeMajor = process.versions.node.split('.')[0];

describe('github reporter', () => {
beforeEach(() => {
writeFileSync(GITHUB_STEP_SUMMARY, '');
});

test('spawn with reporter', () => {
test('spawn with reporter', async () => {
const child = spawnSync(process.execPath, ['--test-reporter', './index.js', '../../tests/example'], {
env: { GITHUB_ACTIONS: true, GITHUB_STEP_SUMMARY, GITHUB_WORKSPACE: path.resolve(__dirname, '../../../') },
});

assert.strictEqual(child.stderr?.toString(), '');
compareLines(child.stdout?.toString(), output.overrides[nodeMajor]?.stdout ?? output.stdout);
compareLines(readFileSync(GITHUB_STEP_SUMMARY).toString(), output.summary);
await snapshot(child, readFileSync(GITHUB_STEP_SUMMARY).toString('utf-8'));
});

test('spawn with reporter - esm', () => {
test('spawn with reporter - esm', async () => {
const child = spawnSync(process.execPath, ['--test-reporter', './index.js', '../../tests/example.mjs'], {
env: { GITHUB_ACTIONS: true, GITHUB_STEP_SUMMARY, GITHUB_WORKSPACE: path.resolve(__dirname, '../../../') },
});

assert.strictEqual(child.stderr?.toString(), '');
compareLines(
child.stdout?.toString(),
outputESM.overrides[nodeMajor]?.stdout ?? outputESM.stdout,
);
compareLines(readFileSync(GITHUB_STEP_SUMMARY).toString(), outputESM.summary);
await snapshot(child, readFileSync(GITHUB_STEP_SUMMARY).toString('utf-8'));
});

test('should noop if not in github actions', () => {
test('should noop if not in github actions', async () => {
const silentChild = spawnSync(process.execPath, ['--test-reporter', './index.js', '../../tests/example'], { env: { } });
assert.strictEqual(silentChild.stderr?.toString(), '');
assert.strictEqual(silentChild.stdout?.toString(), '');
await snapshot(silentChild);
});
});
Loading

0 comments on commit 1d3ca6a

Please sign in to comment.