Skip to content
← Back to rules

vitest/hoisted-apis-on-top 正しさ

An auto-fix is available for this rule.

何を実行するか

ホイストされた API がファイルの先頭に配置されることを強制します。

なぜ問題なのか

一部の Vitest API は変換プロセス中に自動的にホイストされます。この API をランタイムコードのように使用すると、テストの実行時に予期しない結果が生じる可能性があります。

このルールに違反する不適切なコードの例:

js
if (condition) {
  vi.mock("some-module", () => {});
}
js
if (condition) {
  vi.unmock("some-module", () => {});
}
js
if (condition) {
  vi.hoisted(() => {});
}
js
describe("suite", () => {
  it("test", async () => {
    vi.mock("some-module", () => {});

    const sm = await import("some-module");
  });
});

このルールに準拠する適切なコードの例:

js
if (condition) {
  vi.doMock("some-module", () => {});
}
js
vi.mock("some-module", () => {});
if (condition) {
}
js
vi.unmock("some-module", () => {});
if (condition) {
}
js
vi.hoisted(() => {});
if (condition) {
}
js
vi.mock("some-module", () => {});

describe("suite", () => {
  it("test", async () => {
    const sm = await import("some-module");
  });
});

使い方

設定ファイルまたは CLI でこのルールを有効化するには、次のようにします:

json
{
  "plugins": ["vitest"],
  "rules": {
    "vitest/hoisted-apis-on-top": "error"
  }
}
bash
oxlint --deny vitest/hoisted-apis-on-top --vitest-plugin

参照