0
1

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でテスト対象がimportしてるモジュールをモックする方法(typescript編)

Last updated at Posted at 2025-04-26

✅ はじめに

vitestでテスト対象がimportしてるモジュールをモックする方法について、サラッと知りたい人向けの記事です。


✅ サンプルコード

typescript-vitest-mock-1.png

// foo/index.ts

// barをimport
import { bar } from '../bar'

// bar()を実行して、文字列「foo-->」の後ろに付けて返す
export const foo = (): string => {
    return 'foo-->' + bar()
}
// bar/index.ts

// 文字列「bar」を返す
export const bar = (): string => {
    return 'bar'
}

✅ モックしない場合

typescript-vitest-mock-2.png

// foo/index.test.ts

// テスト用の関数をimport
import { describe, expect, test } from 'vitest'

// テスト対象モジュールをimport
import { foo } from '.'

// テスト
describe('テスト', () => {

  test('foo', () => {
    // foo()を実行する
    const result = foo()
    
    // 結果が「foo-->bar」であること
    expect(result).toBe('foo-->bar')
  })

})

✅ vi.spyOnでモックする場合

typescript-vitest-mock-4.png

// foo/index.test.ts


// テスト用の関数をimport
import { describe, expect, test, vi } from 'vitest'

// テスト対象モジュール
import { foo } from '.'

// モック対象モジュール
import * as barModule from '../bar'

// テスト
describe('同期関数', () => {

  test('foo', () => {
    // bar()をモックする
    const barSpy = vi.spyOn(barModule, 'bar').mockReturnValue('mock');

    // foo()を実行する
    const result = foo();

    // 結果が「foo-->mock」であること
    expect(result).toBe('foo-->mock');
    expect(barSpy).toHaveBeenCalledWith(1);
    expect(barSpy).toHaveBeenCalledTimes(1);

    // モックをリセット
    barSpy.mockRestore();
  })

})

✅ vi.mockでモックする場合

typescript-vitest-mock-3.png

📝 パターン1:戻り値が固定で良い場合

// foo/index.test.ts

// テスト用の関数をimport
import { describe, expect, test, vi } from 'vitest'

// テスト対象モジュールをimport
import { foo } from '.'

// barをモックして、「mock」を返すようにする
vi.mock('../bar', () => ({
  bar: vi.fn(() => 'mock')
}))

// テスト
describe('テスト', () => {

  test('foo', () => {
    // foo()を実行する
    const result = foo()
    
    // 結果が「foo-->mock」であること
    expect(result).toBe('foo-->mock')
  })

})

📝 パターン2:戻り値をテスト内で設定する場合

// foo/index.test.ts

// テスト用の関数をimport
import { describe, expect, test, vi } from 'vitest'

// テスト対象モジュールをimport
import { foo } from '.'

// モック対象モジュールをimport
import { bar } from '../bar'

// barをモックして、Mock型で定義しておく
// (戻り値は設定しない)
vi.mock('../bar');
const barMock = bar as Mock;

// テスト
describe('テスト', () => {

  test('foo', () => {
    // モックに戻り値を設定する
    barMock.mockReturnValue('mock')
    
    // foo()を実行する
    const result = foo()
    
    // 結果が「foo-->mock」であること
    expect(result).toBe('foo-->mock')
  })

})

📝 モック関数

モック対象が同期関数の場合

// モック対象が同期関数の場合
xxxMock.mockReturnValue('mock')
//=> モックは'mock'を返す

モック対象が非同期関数の場合

// モック対象が非同期関数の場合
xxxMock.mockResolvedValue('mock')
//=> モックは'mock'を返す

モックに引数を渡したい場合

// モックに引数を渡したい場合
xxxMock.mockImplementation((i) => 'mock'.repeat(i))
// モックに引数「1」が渡された場合、モックは'mock'を返す
// モックに引数「2」が渡された場合、モックは'mockmock'を返す
0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?