-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: update mock implementation to support ESM runtime, introduce "…
…vi.hoisted" (#3258)
- Loading branch information
1 parent
da2f197
commit 0c09a40
Showing
39 changed files
with
2,430 additions
and
693 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { expect, test, vi } from 'vitest' | ||
import { asyncSquare as importedAsyncSquare, square as importedSquare } from '../src/example' | ||
|
||
const mocks = vi.hoisted(() => { | ||
return { | ||
square: vi.fn(), | ||
} | ||
}) | ||
|
||
const { asyncSquare } = await vi.hoisted(async () => { | ||
return { | ||
asyncSquare: vi.fn(), | ||
} | ||
}) | ||
|
||
vi.mock('../src/example.ts', () => { | ||
return { | ||
square: mocks.square, | ||
asyncSquare, | ||
} | ||
}) | ||
|
||
test('hoisted works', () => { | ||
expect(importedSquare).toBe(mocks.square) | ||
expect(importedAsyncSquare).toBe(asyncSquare) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "esnext", | ||
"target": "esnext", | ||
"moduleResolution": "nodenext", | ||
"types": ["vitest/globals"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
function throwNotImplemented(name: string) { | ||
throw new Error(`[vitest] ${name} is not implemented in browser environment yet.`) | ||
} | ||
|
||
export class VitestBrowserClientMocker { | ||
public importActual() { | ||
throwNotImplemented('importActual') | ||
} | ||
|
||
public importMock() { | ||
throwNotImplemented('importMock') | ||
} | ||
|
||
public queueMock() { | ||
throwNotImplemented('queueMock') | ||
} | ||
|
||
public queueUnmock() { | ||
throwNotImplemented('queueUnmock') | ||
} | ||
|
||
public prepare() { | ||
// TODO: prepare | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export function importId(id: string) { | ||
const name = `/@id/${id}` | ||
return import(name) | ||
// @ts-expect-error mocking vitest apis | ||
return __vi_wrap_module__(import(name)) | ||
} |
Oops, something went wrong.