[go: nahoru, domu]

Skip to content

Commit

Permalink
Test: Convert config-notrycatch fixture to TAP-based
Browse files Browse the repository at this point in the history
The TAP snapshot is normalized to remove local file path and OS
variances, thus allowing a strict validation to take place.

While at it, also set a timeout so that this test keeps working
the same way once a default timeout is enabled.
  • Loading branch information
Krinkle committed Jun 25, 2024
1 parent 03b764d commit 44a6ba1
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 39 deletions.
22 changes: 0 additions & 22 deletions test/cli/cli-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,26 +202,4 @@ not ok 1 global failure
assert.true(execution.stderr.includes("Error: Cannot find module 'does-not-exist-at-all'"));
assert.equal(execution.stdout, '');
});

QUnit.test('config.notrycatch with rejected test', async assert => {
const command = ['qunit', 'config-notrycatch-test-rejection.js'];
const execution = await execute(command);

assert.pushResult({
// only in stdout due to using `console.log` in manual `unhandledRejection` handler
result: execution.stdout.includes('Unhandled Rejection: bad things happen sometimes'),
actual: execution.stdout + '\n' + execution.stderr
});
});

QUnit.test('config.notrycatch with rejected hook', async assert => {
const command = ['qunit', 'config-notrycatch-hook-rejection.js'];
const execution = await execute(command);

assert.pushResult({
// only in stdout due to using `console.log` in manual `unhandledRejection` handler
result: execution.stdout.includes('Unhandled Rejection: bad things happen sometimes'),
actual: execution.stdout + '\n' + execution.stderr
});
});
});
7 changes: 4 additions & 3 deletions test/cli/fixtures/config-notrycatch-hook-rejection.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
'use strict';

process.on('unhandledRejection', (reason) => {
console.log('Unhandled Rejection:', reason);
console.log('FOUND Unhandled Rejection:', reason);
});

QUnit.config.testTimeout = 1000;
QUnit.config.notrycatch = true;

QUnit.module('notrycatch', function (hooks) {
QUnit.module('example', function (hooks) {
hooks.beforeEach(() => {
return Promise.reject('bad things happen sometimes');
return Promise.reject('bad things happen');
});

QUnit.test('passing test', assert => {
Expand Down
28 changes: 28 additions & 0 deletions test/cli/fixtures/config-notrycatch-hook-rejection.tap.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# command: ["qunit", "config-notrycatch-hook-rejection.js"]

TAP version 13
FOUND Unhandled Rejection: bad things happen
not ok 1 example > passing test
---
message: global failure: bad things happen
severity: failed
actual : undefined
expected: undefined
stack: |
at internal
...
---
message: Test took longer than 1000ms; test timed out.
severity: failed
actual : null
expected: undefined
stack: |
at internal
...
1..1
# pass 0
# skip 0
# todo 0
# fail 1

# exit code: 1
16 changes: 5 additions & 11 deletions test/cli/fixtures/config-notrycatch-test-rejection.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
'use strict';

process.on('unhandledRejection', (reason) => {
console.log('Unhandled Rejection:', reason);
console.log('FOUND Unhandled Rejection:', reason);
});

QUnit.module('notrycatch', function (hooks) {
hooks.beforeEach(function () {
this.originalNotrycatch = QUnit.config.notrycatch;
QUnit.config.notrycatch = true;
});

hooks.afterEach(function () {
QUnit.config.notrycatch = this.originalNotrycatch;
});
QUnit.config.testTimeout = 1000;
QUnit.config.notrycatch = true;

QUnit.module('example', function () {
QUnit.test('returns a rejected promise', function () {
return Promise.reject('bad things happen sometimes');
return Promise.reject('bad things happen');
});
});
28 changes: 28 additions & 0 deletions test/cli/fixtures/config-notrycatch-test-rejection.tap.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# command: ["qunit", "config-notrycatch-test-rejection.js"]

TAP version 13
FOUND Unhandled Rejection: bad things happen
not ok 1 example > returns a rejected promise
---
message: global failure: bad things happen
severity: failed
actual : undefined
expected: undefined
stack: |
at internal
...
---
message: Test took longer than 1000ms; test timed out.
severity: failed
actual : null
expected: undefined
stack: |
at internal
...
1..1
# pass 0
# skip 0
# todo 0
# fail 1

# exit code: 1
7 changes: 4 additions & 3 deletions test/cli/helpers/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ function normalize (actual) {
.replace(/^(\s+at ).*\/qunit\/bin\/qunit\.js.*$/gm, '$1internal')
.replace(/^(\s+at ).*\/qunit\/src\/cli\/.*$/gm, '$1internal')

// Strip frames from indirect nyc dependencies that are specific
// to code coverage jobs:
// Convert "at load (/qunit/node_modules/append-transform/index.js:6" to "at internal"
// Strip frames from nyc dependencies that nyc injects during coverage job:
// Convert "at load (/qunit/node_modules/append-transform/index.js:6"
// Convert "at process.processEmit (/qunit/node_modules/signal-exit/index.js:199:34)"
.replace(/ {2}at .+\/.*node_modules\/append-transform\/.*\)/g, ' at internal')
.replace(/ {2}at .+\/.*node_modules\/signal-exit\/.*\)/g, ' at internal')
// Consolidate subsequent qunit.js frames
.replace(/^(\s+at qunit\.js$)(\n\s+at qunit\.js$)+/gm, '$1')
// Consolidate subsequent internal frames
Expand Down

0 comments on commit 44a6ba1

Please sign in to comment.