You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import { it, expect } from 'vitest';
//gets the value after 100ms asynchroniously
async function getAsyncResult() {
return new Promise((resolve) => {
setTimeout(() => resolve('expected result'), 100);
});
}
// this matcher function has 'async' keyword and returns promise
async function toBeExpected_Original(text) {
const { isNot } = this;
const value = await getAsyncResult();
const result = value === text;
return {
pass: isNot ? !result : result,
message: () => `Input is ${result ? '' : 'not '}'expected result'`,
};
}
// this matcher is the same function with toBeExpected_NonAsync but has 'async' keyword
async function toBeExpected_Async(text) {
// ... process text before using the original matcher for some reason...
// reuse the original matcher
return toBeExpected_Original.call(this, text);
}
// this matcher function has no 'async' keyword but returns the promise from original matcher.
function toBeExpected_NonAsync(text) {
// ... process text before using the original matcher for some reason...
// reuse the original matcher
return toBeExpected_Original.call(this, text);
}
const extensionMatchers = {
toBeExpected_Original,
toBeExpected_Async,
toBeExpected_NonAsync,
};
expect.extend(extensionMatchers);
// OK
it('async matcher works as expected.', async () => {
await expect('expected result').toBeExpected_Original();
});
// OK
it('async matcher that uses existing matcher works as expected.', async () => {
await expect('expected result').toBeExpected_Async();
});
// NOT OK
it('sync but promise returning matcher fails.', async () => {
await expect('expected result').toBeExpected_NonAsync();
});
Describe the bug
This breaks jest compatibility and prevents migrating some matcher extension libraries to vitest. Detailed example is below.
Reproduction
Vitest StackBlitz link: https://stackblitz.com/edit/vitest-dev-vitest-hwzkyd?file=test%2Fasync.test.js&initialPath=__vitest__
Jest StackBlitz link: https://stackblitz.com/edit/jest-example-kymfnr?file=async.test.js&terminal=test&view=editor
System Info
Used Package Manager
npm
Validations
The text was updated successfully, but these errors were encountered: