Skip to content

Commit

Permalink
fixup! fixup! wip fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
novemberborn committed Oct 22, 2023
1 parent 5f1cb3b commit 6d31fea
Show file tree
Hide file tree
Showing 15 changed files with 106 additions and 89 deletions.
17 changes: 14 additions & 3 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,12 @@ export class Assertions {
this.throwsAsync = withSkip(async (...args) => {
let [thrower, expectations, message] = args;

assertMessage(message, 't.throwsAsync()');
try {
assertMessage(message, 't.throwsAsync()');
} catch (error) {
Promise.resolve(thrower).catch(noop);
throw error;
}

if (typeof thrower !== 'function' && !isPromise(thrower)) {
throw fail(new AssertionError('`t.throwsAsync()` must be called with a function or promise', {
Expand All @@ -460,6 +465,7 @@ export class Assertions {
try {
expectations = validateExpectations('t.throwsAsync()', expectations, args.length, experiments);
} catch (error) {
Promise.resolve(thrower).catch(noop);
throw fail(error);
}

Expand All @@ -468,7 +474,7 @@ export class Assertions {
const assertionStack = getAssertionStack();
// Handle "promise like" objects by casting to a real Promise.
const intermediate = Promise.resolve(promise).then(value => {
throw fail(new AssertionError(message, {
throw failPending(new AssertionError(message, {
assertion: 't.throwsAsync()',
assertionStack,
formattedDetails: [formatWithLabel(`${wasReturned ? 'Returned promise' : 'Promise'} resolved with:`, value)],
Expand Down Expand Up @@ -548,7 +554,12 @@ export class Assertions {
});

this.notThrowsAsync = withSkip(async (nonThrower, message) => {
assertMessage(message, 't.notThrowsAsync()');
try {
assertMessage(message, 't.notThrowsAsync()');
} catch (error) {
Promise.resolve(nonThrower).catch(noop);
throw error;
}

if (typeof nonThrower !== 'function' && !isPromise(nonThrower)) {
throw fail(new AssertionError('`t.notThrowsAsync()` must be called with a function or promise', {
Expand Down
16 changes: 10 additions & 6 deletions lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ class ExecutionContext extends Assertions {
test.addPendingAssertion(promise);
},
fail(error) {
test.addFailedAssertion(error);
return test.addFailedAssertion(error);
},
failPending(error) {
test.failPendingAssertion(error);
return test.failPendingAssertion(error);
},
skip() {
test.countPassedAssertion();
Expand Down Expand Up @@ -321,7 +321,7 @@ export default class Test {
this.logs.push(text);
}

addPendingAssertion(promise) {
async addPendingAssertion(promise) {
if (this.finishing) {
this.saveFirstError(new Error('Assertion started, but test has already finished'));
}
Expand All @@ -334,10 +334,14 @@ export default class Test {
this.pendingAssertionCount++;
this.refreshTimeout();

promise.finally(() => {
try {
await promise;
} catch {
// Ignore errors.
} finally {
this.pendingAssertionCount--;
this.refreshTimeout();
});
}
}

addFailedAssertion(error) {
Expand Down Expand Up @@ -530,7 +534,7 @@ export default class Test {
const [syncOk, retval] = this.callFn();
if (!syncOk) {
if (this.testFailure !== null && retval === this.testFailure) {
return;
return this.finish();
}

if (isExternalAssertError(retval)) {
Expand Down
12 changes: 7 additions & 5 deletions test-tap/fixture/report/regular/traces-in-t-throws.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ test('notThrows', t => {
t.notThrows(() => throwError());
});

test('notThrowsAsync', t => {
t.notThrowsAsync(() => throwError());
test('notThrowsAsync', async t => {
await t.notThrowsAsync(() => throwError());
});

test('throwsAsync', t => {
t.throwsAsync(() => throwError(), {instanceOf: TypeError});
test('throwsAsync', async t => {
await t.throwsAsync(() => throwError(), {instanceOf: TypeError});
});

test('throwsAsync different error', t => t.throwsAsync(returnRejectedPromise, {instanceOf: TypeError}));
test('throwsAsync different error', async t => {
await t.throwsAsync(returnRejectedPromise, {instanceOf: TypeError});
});
30 changes: 15 additions & 15 deletions test-tap/reporters/default.regular.v16.log
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,9 @@

traces-in-t-throws.cjs:20

19: test('notThrowsAsync', t => {
 20: t.notThrowsAsync(() => throwError());
21: });
19: test('notThrowsAsync', async t => {
 20: await t.notThrowsAsync(() => throwError());
21: });

Function threw:

Expand All @@ -300,18 +300,18 @@
}

› throwError (test-tap/fixture/report/regular/traces-in-t-throws.cjs:4:8)
› test-tap/fixture/report/regular/traces-in-t-throws.cjs:20:25
› test-tap/fixture/report/regular/traces-in-t-throws.cjs:20:4
› test-tap/fixture/report/regular/traces-in-t-throws.cjs:20:31
› test-tap/fixture/report/regular/traces-in-t-throws.cjs:20:10



traces-in-t-throws › throwsAsync

traces-in-t-throws.cjs:24

23: test('throwsAsync', t => {
 24: t.throwsAsync(() => throwError(), {instanceOf: TypeError});
25: });
23: test('throwsAsync', async t => {
 24: await t.throwsAsync(() => throwError(), {instanceOf: TypeError});
25: });

Function threw synchronously. Use `t.throws()` instead:

Expand All @@ -320,18 +320,18 @@
}

› throwError (test-tap/fixture/report/regular/traces-in-t-throws.cjs:4:8)
› t.throwsAsync.instanceOf (test-tap/fixture/report/regular/traces-in-t-throws.cjs:24:22)
› test-tap/fixture/report/regular/traces-in-t-throws.cjs:24:4
› t.throwsAsync.instanceOf (test-tap/fixture/report/regular/traces-in-t-throws.cjs:24:28)
› test-tap/fixture/report/regular/traces-in-t-throws.cjs:24:10



traces-in-t-throws › throwsAsync different error

traces-in-t-throws.cjs:27
traces-in-t-throws.cjs:28

26:
 27: test('throwsAsync different error', t => t.throwsAsync(returnRejectedPromise, {instanceOf: TypeError}));
28:
27: test('throwsAsync different error', async t => {
 28: await t.throwsAsync(returnRejectedPromise, {instanceOf: TypeError});
29: });

Returned promise rejected with unexpected exception:

Expand All @@ -344,7 +344,7 @@
Function TypeError {}

› returnRejectedPromise (test-tap/fixture/report/regular/traces-in-t-throws.cjs:8:24)
› test-tap/fixture/report/regular/traces-in-t-throws.cjs:27:44
› test-tap/fixture/report/regular/traces-in-t-throws.cjs:28:10

─

Expand Down
30 changes: 15 additions & 15 deletions test-tap/reporters/default.regular.v18.log
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,9 @@

traces-in-t-throws.cjs:20

19: test('notThrowsAsync', t => {
 20: t.notThrowsAsync(() => throwError());
21: });
19: test('notThrowsAsync', async t => {
 20: await t.notThrowsAsync(() => throwError());
21: });

Function threw:

Expand All @@ -300,18 +300,18 @@
}

› throwError (test-tap/fixture/report/regular/traces-in-t-throws.cjs:4:8)
› test-tap/fixture/report/regular/traces-in-t-throws.cjs:20:25
› test-tap/fixture/report/regular/traces-in-t-throws.cjs:20:4
› test-tap/fixture/report/regular/traces-in-t-throws.cjs:20:31
› test-tap/fixture/report/regular/traces-in-t-throws.cjs:20:10



traces-in-t-throws › throwsAsync

traces-in-t-throws.cjs:24

23: test('throwsAsync', t => {
 24: t.throwsAsync(() => throwError(), {instanceOf: TypeError});
25: });
23: test('throwsAsync', async t => {
 24: await t.throwsAsync(() => throwError(), {instanceOf: TypeError});
25: });

Function threw synchronously. Use `t.throws()` instead:

Expand All @@ -320,18 +320,18 @@
}

› throwError (test-tap/fixture/report/regular/traces-in-t-throws.cjs:4:8)
› t.throwsAsync.instanceOf (test-tap/fixture/report/regular/traces-in-t-throws.cjs:24:22)
› test-tap/fixture/report/regular/traces-in-t-throws.cjs:24:4
› t.throwsAsync.instanceOf (test-tap/fixture/report/regular/traces-in-t-throws.cjs:24:28)
› test-tap/fixture/report/regular/traces-in-t-throws.cjs:24:10



traces-in-t-throws › throwsAsync different error

traces-in-t-throws.cjs:27
traces-in-t-throws.cjs:28

26:
 27: test('throwsAsync different error', t => t.throwsAsync(returnRejectedPromise, {instanceOf: TypeError}));
28:
27: test('throwsAsync different error', async t => {
 28: await t.throwsAsync(returnRejectedPromise, {instanceOf: TypeError});
29: });

Returned promise rejected with unexpected exception:

Expand All @@ -344,7 +344,7 @@
Function TypeError {}

› returnRejectedPromise (test-tap/fixture/report/regular/traces-in-t-throws.cjs:8:24)
› test-tap/fixture/report/regular/traces-in-t-throws.cjs:27:44
› test-tap/fixture/report/regular/traces-in-t-throws.cjs:28:10

─

Expand Down
30 changes: 15 additions & 15 deletions test-tap/reporters/default.regular.v20.log
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,9 @@

traces-in-t-throws.cjs:20

19: test('notThrowsAsync', t => {
 20: t.notThrowsAsync(() => throwError());
21: });
19: test('notThrowsAsync', async t => {
 20: await t.notThrowsAsync(() => throwError());
21: });

Function threw:

Expand All @@ -300,18 +300,18 @@
}

› throwError (test-tap/fixture/report/regular/traces-in-t-throws.cjs:4:8)
› test-tap/fixture/report/regular/traces-in-t-throws.cjs:20:25
› test-tap/fixture/report/regular/traces-in-t-throws.cjs:20:4
› test-tap/fixture/report/regular/traces-in-t-throws.cjs:20:31
› test-tap/fixture/report/regular/traces-in-t-throws.cjs:20:10



traces-in-t-throws › throwsAsync

traces-in-t-throws.cjs:24

23: test('throwsAsync', t => {
 24: t.throwsAsync(() => throwError(), {instanceOf: TypeError});
25: });
23: test('throwsAsync', async t => {
 24: await t.throwsAsync(() => throwError(), {instanceOf: TypeError});
25: });

Function threw synchronously. Use `t.throws()` instead:

Expand All @@ -320,18 +320,18 @@
}

› throwError (test-tap/fixture/report/regular/traces-in-t-throws.cjs:4:8)
› t.throwsAsync.instanceOf (test-tap/fixture/report/regular/traces-in-t-throws.cjs:24:22)
› test-tap/fixture/report/regular/traces-in-t-throws.cjs:24:4
› t.throwsAsync.instanceOf (test-tap/fixture/report/regular/traces-in-t-throws.cjs:24:28)
› test-tap/fixture/report/regular/traces-in-t-throws.cjs:24:10



traces-in-t-throws › throwsAsync different error

traces-in-t-throws.cjs:27
traces-in-t-throws.cjs:28

26:
 27: test('throwsAsync different error', t => t.throwsAsync(returnRejectedPromise, {instanceOf: TypeError}));
28:
27: test('throwsAsync different error', async t => {
 28: await t.throwsAsync(returnRejectedPromise, {instanceOf: TypeError});
29: });

Returned promise rejected with unexpected exception:

Expand All @@ -344,7 +344,7 @@
Function TypeError {}

› returnRejectedPromise (test-tap/fixture/report/regular/traces-in-t-throws.cjs:8:24)
› test-tap/fixture/report/regular/traces-in-t-throws.cjs:27:44
› test-tap/fixture/report/regular/traces-in-t-throws.cjs:28:10

─

Expand Down
2 changes: 1 addition & 1 deletion test-tap/reporters/tap.failfast.v16.log
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ not ok 1 - a › fails
name: AssertionError
assertion: t.fail()
message: Test failed via `t.fail()`
at: 'ExecutionContext.fail (/lib/assert.js:285:9)'
at: 'ExecutionContext.fail (/lib/assert.js:278:15)'
...
---tty-stream-chunk-separator

Expand Down
2 changes: 1 addition & 1 deletion test-tap/reporters/tap.failfast.v18.log
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ not ok 1 - a › fails
name: AssertionError
assertion: t.fail()
message: Test failed via `t.fail()`
at: 'ExecutionContext.fail (/lib/assert.js:285:9)'
at: 'ExecutionContext.fail (/lib/assert.js:278:15)'
...
---tty-stream-chunk-separator

Expand Down
2 changes: 1 addition & 1 deletion test-tap/reporters/tap.failfast.v20.log
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ not ok 1 - a › fails
name: AssertionError
assertion: t.fail()
message: Test failed via `t.fail()`
at: 'ExecutionContext.fail (/lib/assert.js:285:9)'
at: 'ExecutionContext.fail (/lib/assert.js:278:15)'
...
---tty-stream-chunk-separator

Expand Down
2 changes: 1 addition & 1 deletion test-tap/reporters/tap.failfast2.v16.log
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ not ok 1 - a › fails
name: AssertionError
assertion: t.fail()
message: Test failed via `t.fail()`
at: 'ExecutionContext.fail (/lib/assert.js:285:9)'
at: 'ExecutionContext.fail (/lib/assert.js:278:15)'
...
---tty-stream-chunk-separator
# 1 test remaining in a.cjs
Expand Down
2 changes: 1 addition & 1 deletion test-tap/reporters/tap.failfast2.v18.log
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ not ok 1 - a › fails
name: AssertionError
assertion: t.fail()
message: Test failed via `t.fail()`
at: 'ExecutionContext.fail (/lib/assert.js:285:9)'
at: 'ExecutionContext.fail (/lib/assert.js:278:15)'
...
---tty-stream-chunk-separator
# 1 test remaining in a.cjs
Expand Down
2 changes: 1 addition & 1 deletion test-tap/reporters/tap.failfast2.v20.log
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ not ok 1 - a › fails
name: AssertionError
assertion: t.fail()
message: Test failed via `t.fail()`
at: 'ExecutionContext.fail (/lib/assert.js:285:9)'
at: 'ExecutionContext.fail (/lib/assert.js:278:15)'
...
---tty-stream-chunk-separator
# 1 test remaining in a.cjs
Expand Down

0 comments on commit 6d31fea

Please sign in to comment.