Skip to content

noReturnInTest (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"nursery": {
"noReturnInTest": "error"
}
}
}
}

Disallow return statements in tests.

A test doesn’t need to return a value. The only returned value that test runners make use of is a promise: the runner waits for it before it finishes the test. Using await inside an async test function does the same thing, and it makes the waiting easier to see. Unlike return, await also lets the test continue with more code afterwards.

This rule reports a return statement that is directly inside the body of a test function, whether or not it returns a value. It also checks functions that are declared elsewhere in the file and passed to a test by name.

The rule doesn’t report:

  • return statements inside another statement, such as an if statement, or inside a nested function.
  • Arrow functions that consist of a single expression, such as () => expect(1).toBe(1).
  • Functions passed to Playwright’s test.step(), because test.step() gives the returned value back to the code that called it.
  • Functions passed to fast-check’s test.prop(), because the returned value decides whether the test passes.
test("adds two numbers", () => {
return expect(add(1, 2)).toBe(3);
});
code-block.js:2:5 lint/nursery/noReturnInTest ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ Unexpected return statement in a test.

1 │ test("adds two numbers", () => {
> 2 │ return expect(add(1, 2)).toBe(3);
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3 │ });
4 │

ℹ A test doesn't need to return a value. Test runners only make use of a returned promise, by waiting for it, and await makes that waiting easier to see.

ℹ Remove the return statement. If it returns a promise, mark the test function as async and use await on the promise instead.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

it("loads the user", function () {
return loadUser().then((user) => expect(user.name).toBe("Ada"));
});
code-block.js:2:5 lint/nursery/noReturnInTest ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ Unexpected return statement in a test.

1 │ it("loads the user", function () {
> 2 │ return loadUser().then((user) => expect(user.name).toBe("Ada"));
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3 │ });
4 │

ℹ A test doesn't need to return a value. Test runners only make use of a returned promise, by waiting for it, and await makes that waiting easier to see.

ℹ Remove the return statement. If it returns a promise, mark the test function as async and use await on the promise instead.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

it("loads the user", loadsTheUser);
function loadsTheUser() {
return loadUser().then((user) => expect(user.name).toBe("Ada"));
}
code-block.js:4:5 lint/nursery/noReturnInTest ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ Unexpected return statement in a test.

3 │ function loadsTheUser() {
> 4 │ return loadUser().then((user) => expect(user.name).toBe("Ada"));
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5 │ }
6 │

ℹ A test doesn't need to return a value. Test runners only make use of a returned promise, by waiting for it, and await makes that waiting easier to see.

ℹ Remove the return statement. If it returns a promise, mark the test function as async and use await on the promise instead.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

test("adds two numbers", () => {
expect(add(1, 2)).toBe(3);
});
test("subtracts two numbers", () => expect(subtract(3, 2)).toBe(1));
it("loads the user", async () => {
const user = await loadUser();
expect(user.name).toBe("Ada");
});
it("saves the user", async () => {
if (isReadOnly) {
return;
}
await saveUser();
});