-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
expect
toThrow
does not match empty string
#5228
Comments
I think this behavior is directly coming from chai: vitest/packages/expect/src/jest-expect.ts Lines 530 to 532 in 7d9b1fb
Here is a reproduction: Show codeimport * as chai from 'chai';
try {
chai
.expect(() => {
throw '';
})
.throws(/^$/);
} catch (e) {
console.error(e);
}
// AssertionError: expected [Function] to throw an error
// at eval (/home/projects/vitest-dev-vitest-8a9pvm/repro-chai.mjs:21:6)
// at _0xba6ba0.run (https://vitestdevvitest8a9pvm-zty5.w-credentialless.staticblitz.com/blitz.a9c8a5a3.js:352:372622)
// at _0x3775c6._evaluate (https://vitestdevvitest8a9pvm-zty5.w-credentialless.staticblitz.com/blitz.a9c8a5a3.js:352:378123)
// at async ModuleJob.run (node:internal/modules/esm/module_job:181:2372) {
// actual: '',
// expected: null,
// showDiff: true,
// operator: 'strictEqual'
// }
try {
chai
.expect(() => {
throw 'hello';
})
.throws(/^$/);
} catch (e) {
console.error(e);
}
// AssertionError: expected [Function] to throw error matching /^$/ but got 'hello'
// at eval (/home/projects/vitest-dev-vitest-8a9pvm/repro-chai.mjs:31:6)
// at _0xba6ba0.run (https://vitestdevvitest8a9pvm-zty5.w-credentialless.staticblitz.com/blitz.a9c8a5a3.js:352:372622)
// at _0x3775c6._evaluate (https://vitestdevvitest8a9pvm-zty5.w-credentialless.staticblitz.com/blitz.a9c8a5a3.js:352:378123)
// at async ModuleJob.run (node:internal/modules/esm/module_job:181:2372) {
// actual: 'hello',
// expected: /^$/,
// showDiff: true,
// operator: 'strictEqual'
// } |
This'll probably be fixed in chaijs/chai#1609 |
@43081j what if I want to throw Just kidding 😄 |
Seriously though, couldn't the initial value be a dummy object? Like this: const noError = {};
var caughtErr = noError;
// ...
caughtErr !== noError |
we could support i feel like supporting that will only lead to confusion further down the line ("expected x not to throw but it threw |
we released chai 5.1.1 recently which fixes this, and renovatebot already bumped the version here in vitest. this issue can be closed i think |
Describe the bug
The
toThrow
method on theJestAssertion
interface does not match the empty string when using a regular expression matcher (e.g.,/^$/
). Instead, it gives the following output:Reproduction
https://stackblitz.com/edit/vitest-dev-vitest-otkry8?file=test%2Fbasic.test.ts
System Info
Used Package Manager
npm
Validations
The text was updated successfully, but these errors were encountered: