-
-
Notifications
You must be signed in to change notification settings - Fork 99
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
Add assert.rejects and assert.not.rejects methods for promises #132
base: master
Are you sure you want to change the base?
Changes from all commits
bdd8b6f
f007c7e
7a03d8c
bbd9ba6
74b0d02
009b09c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -366,6 +366,7 @@ throws('should be a function', () => { | |||
throws('should throw if function does not throw Error :: generic', () => { | ||||
try { | ||||
$.throws(() => 123); | ||||
assert.unreachable('Function threw when it shouldn’t have'); | ||||
} catch (err) { | ||||
assert.is(err.message, 'Expected function to throw'); | ||||
isError(err, '', false, true, 'throws', false); // no details (true vs false) | ||||
|
@@ -375,6 +376,7 @@ throws('should throw if function does not throw Error :: generic', () => { | |||
throws('should throw if function does not throw matching Error :: RegExp', () => { | ||||
try { | ||||
$.throws(() => { throw new Error('hello') }, /world/); | ||||
assert.unreachable('Function threw correct pattern when it should have thrown incorrect one'); | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added this in because I was confused when I changed the test to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (I think this is – and the one above; same issue – are the only two I’m confused on. Accepted all other changes and happy to go with what you think is best for on these.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The assertion helper is saying: given X function, an error (a) should be thrown and (b) the thrown error should match {this condition}. The condition can be defined via a function, a RegExp, or a string. So this test has X function, which happens to In the original test, this fails (obv because When you change the X to be: $.throws(() => { throw new Error('world') }, /world/); This doesnt fail the assertion, because Testing negated fail conditions can be a trip, haha There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I kinda feel like it could be helpful to make that work via $.throws(() => throw new Error('world') }).matches(new Error('world')); which maybe that doesn't make sense.. but it'd be nice to be able to chain There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks but the current API is much preferred and has prior art from many existing test runners and/or assertion libraries. It would also add a bunch of additional changes for the same (but more verbose) result. |
||||
} catch (err) { | ||||
assert.is(err.message, 'Expected function to throw exception matching `/world/` pattern'); | ||||
isError(err, '', false, true, 'throws', false); // no details | ||||
|
@@ -418,6 +420,91 @@ throws.run(); | |||
|
||||
// --- | ||||
|
||||
const rejects = suite('rejects'); | ||||
|
||||
rejects('should be a function', () => { | ||||
assert.type($.rejects, 'function'); | ||||
}); | ||||
|
||||
rejects('should throw if function does not reject Error :: generic', async () => { | ||||
try { | ||||
await $.rejects(() => { | ||||
return new Promise((resolve, reject) => { | ||||
setTimeout(() => resolve(), 1); | ||||
}); | ||||
}); | ||||
assert.unreachable('Promise rejected when it shouldn’t have'); | ||||
} catch (err) { | ||||
assert.is(err.message, 'Expected promise to reject'); | ||||
isError(err, '', false, true, 'rejects', false); // no details (true vs false) | ||||
} | ||||
}); | ||||
|
||||
rejects('should throw if function does not reject matching Error :: RegExp', async () => { | ||||
try { | ||||
await $.rejects(() => { | ||||
return new Promise((resolve, reject) => { | ||||
setTimeout(() => reject('hello'), 1) | ||||
}); | ||||
}, /world/); | ||||
assert.unreachable('Promise rejected with correct pattern when it should have with incorrect one'); | ||||
} catch (err) { | ||||
assert.is(err.message, 'Expected promise to reject matching exception `/world/` pattern'); | ||||
isError(err, '', false, true, 'rejects', false); // no details | ||||
} | ||||
}); | ||||
|
||||
rejects('should throw if function does not reject matching Error :: Function', async () => { | ||||
try { | ||||
await $.rejects( | ||||
() => { | ||||
return new Promise((resolve, reject) => { | ||||
reject(new Error()) | ||||
}) | ||||
}, | ||||
(err) => err.message.includes('foobar') | ||||
); | ||||
assert.unreachable('Promise should have rejected without matching exception'); | ||||
} catch (err) { | ||||
assert.is(err.message, 'Expected promise to reject matching exception'); | ||||
isError(err, '', false, true, 'rejects', false); // no details | ||||
} | ||||
}); | ||||
|
||||
rejects('should not reject if promise does reject Error :: generic', async () => { | ||||
await assert.not.rejects( | ||||
async () => await $.rejects(() => { | ||||
return new Promise ((resolve, reject) => setTimeout(() => reject(), 1)); | ||||
}) | ||||
, 'should not reject if function does reject Error :: generic'); | ||||
}); | ||||
|
||||
rejects('should not reject if promise does reject matching Error :: RegExp', async () => { | ||||
await assert.not.rejects( | ||||
async () => await $.rejects( | ||||
() => { | ||||
return new Promise((resolve, reject) => setTimeout(() => reject(new Error('hello')), 1)); | ||||
}, | ||||
/hello/ | ||||
) | ||||
); | ||||
}); | ||||
|
||||
rejects('should not reject if function does reject matching Error :: Function', async () => { | ||||
await assert.not.rejects( | ||||
async () => await $.rejects( | ||||
() => { | ||||
return new Promise((resolve, reject) => setTimeout(() => reject(new Error('foobar')), 1)) | ||||
}, | ||||
(err) => err.message.includes('foobar') | ||||
) | ||||
); | ||||
}) | ||||
|
||||
rejects.run(); | ||||
|
||||
// --- | ||||
|
||||
const not = suite('not'); | ||||
|
||||
not('should be a function', () => { | ||||
|
@@ -793,7 +880,7 @@ notMatch.run(); | |||
const notThrows = suite('not.throws'); | ||||
|
||||
notThrows('should be a function', () => { | ||||
assert.type($.throws, 'function'); | ||||
assert.type($.not.throws, 'function'); | ||||
lukeed marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
}); | ||||
|
||||
notThrows('should not throw if function does not throw Error :: generic', () => { | ||||
|
@@ -848,3 +935,85 @@ notThrows('should throw if function does throw matching Error :: Function', () = | |||
}); | ||||
|
||||
notThrows.run(); | ||||
|
||||
// --- | ||||
|
||||
const notRejects = suite('not.throws'); | ||||
|
||||
notRejects('should be a function', () => { | ||||
assert.type($.not.rejects, 'function'); | ||||
}); | ||||
|
||||
notRejects('should not reject if function does not reject Error :: generic', async () => { | ||||
await assert.not.rejects( | ||||
async () => await $.not.rejects(() => new Promise((resolve, reject) => setTimeout(() => resolve(123), 1))) | ||||
); | ||||
}); | ||||
|
||||
notRejects('should not reject if function does not reject matching Error :: RegExp', async () => { | ||||
await assert.not.rejects( | ||||
async () => { | ||||
await $.not.rejects(() => { | ||||
return new Promise((resolve, reject) => { | ||||
setTimeout(() => reject(new Error('hello')), 1); | ||||
}) | ||||
}, /world/); | ||||
} | ||||
); | ||||
}); | ||||
|
||||
notRejects('should not reject if function does not reject matching Error :: Function', async () => { | ||||
await assert.not.rejects(async () => { | ||||
await $.not.rejects(() => { | ||||
return new Promise ((resolve, reject) => { | ||||
setTimeout(() => reject(new Error('hello')), 1); | ||||
}) | ||||
}, (err) => err.message.includes('world')); | ||||
}); | ||||
}); | ||||
|
||||
notRejects('should reject if function does reject Error :: generic', async () => { | ||||
try { | ||||
await $.not.rejects(() => { | ||||
return new Promise((resolve, reject) => { | ||||
setTimeout(() => reject(new Error()), 1); | ||||
}); | ||||
}); | ||||
assert.unreachable('Function resolved when we were expecting it to reject'); | ||||
} catch (err) { | ||||
assert.is(err.message, 'Expected promise not to reject'); | ||||
isError(err, '', true, false, 'not.rejects', false); // no details | ||||
} | ||||
}); | ||||
|
||||
notRejects('should reject if function does reject matching Error :: RegExp', async () => { | ||||
try { | ||||
await $.not.rejects(() => { | ||||
return new Promise((resolve, reject) => { | ||||
setTimeout(() => reject(new Error('hello')), 1) | ||||
}) | ||||
}, /hello/); | ||||
} catch (err) { | ||||
assert.is(err.message, 'Expected function not to reject promise exception matching `/hello/` pattern'); | ||||
isError(err, '', true, false, 'not.rejects', false); // no details | ||||
} | ||||
}); | ||||
|
||||
notRejects('should reject if function does reject matching Error :: Function', async () => { | ||||
try { | ||||
await $.not.rejects( | ||||
() => { | ||||
return new Promise((resolve, reject) => { | ||||
setTimeout(() => reject(new Error()), 1) | ||||
}) | ||||
}, | ||||
(err) => err instanceof Error | ||||
); | ||||
assert.unreachable('Expected the function to reject with Error instance but it rejected with other value') | ||||
} catch (err) { | ||||
assert.is(err.message, 'Expected promise not to reject matching exception'); | ||||
isError(err, '', true, false, 'not.rejects', false); // no details | ||||
} | ||||
}); | ||||
|
||||
notRejects.run(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.