mirror of
https://github.com/vitest-dev/vitest.git
synced 2025-12-08 18:26:03 +00:00
48 lines
923 B
TypeScript
48 lines
923 B
TypeScript
import axios from 'axios'
|
|
import * as example from '../src/example'
|
|
import * as moduleA from '../src/moduleA'
|
|
|
|
vi
|
|
.mock('../src/example', () => ({
|
|
mocked: true,
|
|
}))
|
|
|
|
// doesn't think comments are mocks
|
|
// vi.mock('../src/example', () => ({
|
|
// mocked: false,
|
|
// }))
|
|
|
|
vi.mock('../src/moduleA', async () => {
|
|
const actual = await vi.importActual<any>('../src/moduleA')
|
|
return {
|
|
B: 'B',
|
|
...actual,
|
|
}
|
|
})
|
|
|
|
vi.mock('axios', () => {
|
|
return {
|
|
default: {
|
|
get: vi.fn(),
|
|
},
|
|
}
|
|
})
|
|
|
|
describe('mocking with factory', () => {
|
|
test('successfuly mocked', () => {
|
|
expect((example as any).mocked).toBe(true)
|
|
expect(example.boolean).toBeUndefined()
|
|
})
|
|
|
|
test('successfuly with actual', () => {
|
|
expect(moduleA.A).toBe('A')
|
|
expect((moduleA as any).B).toBe('B')
|
|
})
|
|
|
|
test('mocks node_modules', () => {
|
|
axios.get('./path')
|
|
|
|
expect(axios.get).toHaveBeenCalledTimes(1)
|
|
})
|
|
})
|