0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

vitestでモジュールをリセットする時は動的インポートを使う

Posted at

vitestにて、beforeEachvi.resetModules()を呼び出しているのに、キャッシュがクリアされない事象が発生した。調べたところ、トップレベルでのimportは対象にならない模様。

以下、引用

import { vi } from 'vitest'

import { data } from './data.js' // Will not get reevaluated beforeEach test

beforeEach(() => {
  vi.resetModules()
})

test('change state', async () => {
  const mod = await import('./some/path.js') // Will get reevaluated
  mod.changeLocalState('new value')
  expect(mod.getLocalState()).toBe('new value')
})

※説明文にも「トップレベルのインポートは再評価できません。」とハッキリ書いてあった。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?