jest/prefer-expect-resolves
eslint-plugin-jest
Safe autofix
eslint.config.js
import jest from "eslint-plugin-jest";
export default [
{
plugins: { jest },
rules: {
"jest/prefer-expect-resolves": "error",
},
},
];Prefer await expect(...).resolves over expect(await ...) syntax
it('passes', async () => {
expect(await someValue()).toBe(true);
});
it('is true', async () => {
const myPromise = Promise.resolve(true);
expect(await myPromise).toBe(true);
});it('passes', async () => {
await expect(someValue()).resolves.toBe(true);
});
it('is true', async () => {
const myPromise = Promise.resolve(true);
await expect(myPromise).resolves.toBe(true);
});
it('errors', async () => {
await expect(Promise.reject(new Error('oh noes!'))).rejects.toThrowError(
'oh noes!',
);
});Examples from the ESLint documentation.
No tracked preset enables this rule, so it has no reach yet.
None of the tracked presets enable this rule. Turn it on with the config below.
jest/prefer-expect-resolves
eslint-plugin-jest
import jest from "eslint-plugin-jest";
export default [
{
plugins: { jest },
rules: {
"jest/prefer-expect-resolves": "error",
},
},
];jest/prefer-expect-resolves
jest plugin · style
{
"plugins": [
"jest"
],
"rules": {
"jest/prefer-expect-resolves": "error"
}
}Biome does not have this rule.
Enforce using expect().resolves over expect(await ...) syntax
Suggest using expect.assertions() OR expect.hasAssertions()
Prefer having the last statement in a test be an assertion
Prefer mock resolved/rejected shorthands for promises