noJasmineGlobals (JavaScript)
Summary
Section titled “Summary”- Rule available since:
v2.6.9 - Diagnostic Category:
lint/nursery/noJasmineGlobals - This rule has an unsafe fix.
- The default severity of this rule is information.
- Sources:
- Same as
jest/no-jasmine-globals
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noJasmineGlobals": "error" } } }}Description
Section titled “Description”Disallow Jasmine globals in Jest tests.
Jest can run tests with a runner called jest-jasmine2, which is built on the Jasmine
test framework. That runner makes the jasmine object and the functions spyOn(),
spyOnProperty(), fail(), and pending() available in every test file.
Jest doesn’t document these globals, and jest-circus, the default runner since
Jest 27, doesn’t provide them. Tests that use them fail with the default runner.
This rule reports:
- calls to
spyOn(),spyOnProperty(),fail(), andpending(); - every access to a property of the
jasmineobject, such asjasmine.createSpy()orjasmine["createSpy"]().
The rule ignores a name that the file declares or imports itself, such as a parameter
called fail.
Jest has its own replacement for most Jasmine globals:
| Jasmine global | Replacement in Jest |
|---|---|
spyOn() and spyOnProperty() |
jest.spyOn() |
fail() |
Throw an error |
pending() |
test.skip() |
jasmine.any(), jasmine.anything(), jasmine.arrayContaining(), jasmine.objectContaining(), and jasmine.stringMatching() |
The function with the same name on expect, such as expect.any() |
jasmine.addMatchers() |
expect.extend() |
jasmine.createSpy() |
jest.fn() |
jasmine.DEFAULT_TIMEOUT_INTERVAL = value |
jest.setTimeout(value) |
Biome only offers a fix for the jasmine functions that have the same name on
expect, and for assignments to jasmine.DEFAULT_TIMEOUT_INTERVAL. The fix is unsafe
because Biome can’t check that the file runs in Jest, where expect and jest exist.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”jasmine.DEFAULT_TIMEOUT_INTERVAL = 5000;code-block.js:1:1 lint/nursery/noJasmineGlobals FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Unexpected use of a Jasmine global.
> 1 │ jasmine.DEFAULT_TIMEOUT_INTERVAL = 5000;
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Jest only provides Jasmine globals when it runs tests with the jest-jasmine2 runner. Its default runner doesn't provide them, so this code fails there.
ℹ Use jest.setTimeout() 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.
ℹ Unsafe fix: Use jest.setTimeout() instead.
1 │ - jasmine.DEFAULT_TIMEOUT_INTERVAL·=·5000;
1 │ + jest.setTimeout(5000);
2 2 │
test("saves the user", () => { spyOn(database, "save");});code-block.js:2:5 lint/nursery/noJasmineGlobals ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Unexpected use of a Jasmine global.
1 │ test("saves the user", () => {
> 2 │ spyOn(database, "save");
│ ^^^^^
3 │ });
4 │
ℹ Jest only provides Jasmine globals when it runs tests with the jest-jasmine2 runner. Its default runner doesn't provide them, so this code fails there.
ℹ Use jest.spyOn() 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("rejects invalid input", () => { fail();});code-block.js:2:5 lint/nursery/noJasmineGlobals ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Unexpected use of a Jasmine global.
1 │ test("rejects invalid input", () => {
> 2 │ fail();
│ ^^^^
3 │ });
4 │
ℹ Jest only provides Jasmine globals when it runs tests with the jest-jasmine2 runner. Its default runner doesn't provide them, so this code fails there.
ℹ Throw an error to fail the test 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("supports dark mode", () => { pending();});code-block.js:2:5 lint/nursery/noJasmineGlobals ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Unexpected use of a Jasmine global.
1 │ test("supports dark mode", () => {
> 2 │ pending();
│ ^^^^^^^
3 │ });
4 │
ℹ Jest only provides Jasmine globals when it runs tests with the jest-jasmine2 runner. Its default runner doesn't provide them, so this code fails there.
ℹ Use test.skip() 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("returns a number", () => { expect(getTotal()).toEqual(jasmine.any(Number));});code-block.js:2:32 lint/nursery/noJasmineGlobals FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Unexpected use of a Jasmine global.
1 │ test("returns a number", () => {
> 2 │ expect(getTotal()).toEqual(jasmine.any(Number));
│ ^^^^^^^^^^^
3 │ });
4 │
ℹ Jest only provides Jasmine globals when it runs tests with the jest-jasmine2 runner. Its default runner doesn't provide them, so this code fails there.
ℹ Use expect.any() 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.
ℹ Unsafe fix: Use expect.any() instead.
1 1 │ test("returns a number", () => {
2 │ - ····expect(getTotal()).toEqual(jasmine.any(Number));
2 │ + ····expect(getTotal()).toEqual(expect.any(Number));
3 3 │ });
4 4 │
const onSave = jasmine.createSpy();code-block.js:1:16 lint/nursery/noJasmineGlobals ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Unexpected use of a Jasmine global.
> 1 │ const onSave = jasmine.createSpy();
│ ^^^^^^^^^^^^^^^^^
2 │
ℹ Jest only provides Jasmine globals when it runs tests with the jest-jasmine2 runner. Its default runner doesn't provide them, so this code fails there.
ℹ Use jest.fn() 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.
jest.setTimeout(5000);
test("saves the user", () => { jest.spyOn(database, "save");});
test("rejects invalid input", () => { throw new Error("The input was accepted");});
test.skip("supports dark mode", () => {});
test("returns a number", () => { expect(getTotal()).toEqual(expect.any(Number));});
const onSave = jest.fn();
function onError(fail) { fail();}Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.