jest/no-hooks 風格
作用
禁止使用 Jest 的設定與拆卸鉤子,例如 beforeAll
。
為何這不好?
Jest 提供了用於設定和拆卸任務的全域函式,這些函式會在每個測試案例和每個測試套件之前/之後被呼叫。使用這些鉤子會促使測試之間共享狀態。
此規則會針對以下函式呼叫報告
beforeAll
beforeEach
afterAll
afterEach
範例
javascript
function setupFoo(options) {
/* ... */
}
function setupBar(options) {
/* ... */
}
describe("foo", () => {
let foo;
beforeEach(() => {
foo = setupFoo();
});
afterEach(() => {
foo = null;
});
it("does something", () => {
expect(foo.doesSomething()).toBe(true);
});
describe("with bar", () => {
let bar;
beforeEach(() => {
bar = setupBar();
});
afterEach(() => {
bar = null;
});
it("does something with bar", () => {
expect(foo.doesSomething(bar)).toBe(true);
});
});
});