jest/no-jasmine-globals
Disallow Jasmine globals such as jasmine.any, spyOn, fail and pending
fixable
Jest's default test runner doesn't define Jasmine's globals. A test that came from Jasmine and still calls fail(), spyOn() or jasmine.createSpy() throws a ReferenceError when it gets there. Jest has its own API for each of them.
| Jasmine | Jest |
|---|---|
spyOn, spyOnProperty | jest.spyOn |
fail | Throw an error |
pending | test.skip |
jasmine.any, anything, arrayContaining, objectContaining, stringMatching | The same name on expect |
jasmine.createSpy | jest.fn |
jasmine.addMatchers | expect.extend |
jasmine.DEFAULT_TIMEOUT_INTERVAL = 5000 | jest.setTimeout(5000) |
Any other call to a jasmine method or assignment to a jasmine property is reported too.
--fix rewrites the five matchers, such as jasmine.any to expect.any, and jasmine.DEFAULT_TIMEOUT_INTERVAL = 5000 to jest.setTimeout(5000).
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"jest/no-jasmine-globals" = "error" # or "warn"Examples#
example.js4 problems
jasmine.DEFAULT_TIMEOUT_INTERVAL"Illegal usage of jasmine global" = 5000;
test("saves the user", () => {
spyOn"Illegal usage of global spyOn"(db, "insert");
save({ name: "Ada" });
expect(db.insert).toHaveBeenCalledWith(jasmine.anyIllegal usage of `any`(Object));
});
test("exports to PDF", () => {
pendingIllegal usage of `pending`,();
});example.jsNo problems
jest.setTimeout(5000);
test("saves the user", () => {
jest.spyOn(db, "insert");
save({ name: "Ada" });
expect(db.insert).toHaveBeenCalledWith(expect.any(Object));
});
test.skip("exports to PDF", () => {});Your own function with one of these names isn't reported:
example.jsNo problems
import { fail } from "./helpers";
test("rejects an empty name", () => {
if (validate("")) {
fail("an empty name passed");
}
});