vitest/hoisted-apis-on-top

January 2, 2026 ยท View on GitHub

๐Ÿ“ Enforce hoisted APIs to be on top of the file.

โš ๏ธ This rule warns in the ๐ŸŒ all config.

๐Ÿ’ก This rule is manually fixable by editor suggestions.

Rule Details

This rule disallows using APIs which are hoisted by vitest in positions where they look like runtime code.

Examples of incorrect code for this rule:

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

    const sm = await import('some-module')
    // ...
  })
})

Examples of correct code for this rule:

vi.mock('some-module', () => {})

describe('suite', () => {
  it('test', async () => {
    const sm = await import('some-module')
    // ...
  })
})
vi.unmock(() => {})

if (condition) {
  // ...
}
vi.hoisted(() => {})

if (condition) {
  // ...
}