Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow throws to accept a fn/Promise that throws anything #3245

Merged
merged 8 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .xo-config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,13 @@ module.exports = {
'unicorn/error-message': 'off',
},
},
{
files: 'test-tap/assert.js',
rules: {
'prefer-promise-reject-errors': 'off',
'no-throw-literal': 'off',
},
},

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to do these inline in the file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like you have take care of this. Thanks you!

],
};
14 changes: 12 additions & 2 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,22 @@ function validateExpectations(assertion, expectations, numberArgs) { // eslint-d
});
}

if (Object.hasOwn(expectations, 'any') && typeof expectations.any !== 'boolean') {
throw new AssertionError({
assertion,
message: `The \`any\` property of the second argument to \`t.${assertion}()\` must be a bool`,
values: [formatWithLabel('Called with:', expectations)],
});
}

for (const key of Object.keys(expectations)) {
switch (key) {
case 'instanceOf':
case 'is':
case 'message':
case 'name':
case 'code': {
case 'code':
case 'any': {
continue;
}

Expand All @@ -164,7 +173,8 @@ function validateExpectations(assertion, expectations, numberArgs) { // eslint-d
// Note: this function *must* throw exceptions, since it can be used
// as part of a pending assertion for promises.
function assertExpectations({assertion, actual, expectations, message, prefix, savedError}) {
if (!isNativeError(actual)) {
const allowThrowAnything = Object.hasOwn(expectations, 'any') && expectations.any;
if (!isNativeError(actual) && !allowThrowAnything) {
throw new AssertionError({
assertion,
message,
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions test-tap/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,11 @@ test('.throws()', gather(t => {
throw new Error('foo');
}));

// Passes when string is thrown, only when any is set to true.
passes(t, () => assertions.throws(() => {
throw 'foo';
}, {any: true}));

// Passes because the correct error is thrown.
passes(t, () => {
const error = new Error('foo');
Expand Down Expand Up @@ -1040,9 +1045,19 @@ test('.throwsAsync()', gather(t => {
values: [{label: 'Returned promise resolved with:', formatted: /'foo'/}],
});

// Fails because the function returned a promise that rejected, but not with an error.
throwsAsyncFails(t, () => assertions.throwsAsync(() => Promise.reject('foo')), {
assertion: 'throwsAsync',
message: '',
values: [{label: 'Returned promise rejected with exception that is not an error:', formatted: /'foo'/}],
});

// Passes because the promise was rejected with an error.
throwsAsyncPasses(t, () => assertions.throwsAsync(Promise.reject(new Error())));

// Passes because the promise was rejected with an with an non-error exception, & set `any` to true in expectation.
throwsAsyncPasses(t, () => assertions.throwsAsync(Promise.reject('foo'), {any: true}));

// Passes because the function returned a promise rejected with an error.
throwsAsyncPasses(t, () => assertions.throwsAsync(() => Promise.reject(new Error())));

Expand Down