Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement module mocking in browser mode #5765

Merged
merged 33 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
87736f3
feat: implement module mocking in the browser
sheremet-va May 23, 2024
e90b2f4
chore: cleanup
sheremet-va May 23, 2024
f6e724a
fix: correctly invalidate mocked module
sheremet-va May 23, 2024
74d14ad
feat: support __mocks__ folder
sheremet-va May 23, 2024
ffaf094
feat: support automocking
sheremet-va May 23, 2024
0c4de84
fix: remove slowHijackESM
sheremet-va May 23, 2024
f5c00fe
fix: add uniqe name to the error
sheremet-va May 23, 2024
38e9028
test: update module browser tests
sheremet-va May 24, 2024
4b0969f
chore: todo
sheremet-va May 24, 2024
b944a4b
chore: expose browser name
sheremet-va May 24, 2024
01aae91
chore: fix the testResults count
sheremet-va May 24, 2024
a727812
chore: fix mocking in --no-isolate mode
sheremet-va May 24, 2024
ab8d756
test: add tests for mocking
sheremet-va May 24, 2024
1634d0d
test: add test for nested dependencies
sheremet-va May 24, 2024
47ef4a0
chore: don't bundle tinyspy and tinybecnh with std-env
sheremet-va May 24, 2024
6b932d3
chore: cleanup
sheremet-va May 24, 2024
83ffe11
chore: print better debug error
sheremet-va May 24, 2024
3fe6f59
chore: remove exportAll util
sheremet-va May 24, 2024
97b7ecf
chore: remove scripts from the resolved config
sheremet-va May 24, 2024
d0896e6
fix: do not hoist vi.mock in vitest package
sheremet-va May 24, 2024
5e904df
chore: only ignore the Vitest dist
sheremet-va May 24, 2024
d9fcdc5
chore: fix typing issue
sheremet-va May 24, 2024
95b1430
feat: split the ws rpc for the browser mode
sheremet-va May 25, 2024
d7beeda
chore: fix types
sheremet-va May 25, 2024
ddca7a1
feat: support all export syntaxes for automocking
sheremet-va May 25, 2024
9677d06
chore: remove Symbol from automocker
sheremet-va May 28, 2024
5b06fb5
chore: remove await
sheremet-va May 28, 2024
a77531e
feat: support vi.importActual
sheremet-va May 28, 2024
958e066
chore: remove browser mock section
sheremet-va May 28, 2024
2bb081b
chore: move test
sheremet-va May 31, 2024
cbc9567
feat: support vi.importMock
sheremet-va May 31, 2024
ab8af86
test: add tets for doMock
sheremet-va May 31, 2024
927449e
fix: correctly parse url on Windows
sheremet-va May 31, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore: cleanup
  • Loading branch information
sheremet-va committed May 31, 2024
commit e90b2f4be8f591f371d45b3ef97e66c61f8115e0
9 changes: 5 additions & 4 deletions packages/browser/src/node/plugins/mocker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { Plugin } from 'vitest/config'
import type { WorkspaceProject } from 'vitest/node'
import type { Reporter } from 'vitest/reporters'
import type { WebSocketRPC } from 'vitest'

export default (project: WorkspaceProject): Plugin => {
Expand All @@ -10,11 +9,11 @@ export default (project: WorkspaceProject): Plugin => {
async load(id) {
const queued = project.browserMocks.queued
if (queued.has(id)) {
const reporter = project.ctx.reporters.find(r => 'wss' in r && 'clients' in r && (r.clients as any).size) as Reporter & {
const reporter = project.ctx.reporters.find(r => 'wss' in r && 'clients' in r && (r.clients as any).size) as {
clients: Map<any, WebSocketRPC>
}

if (!reporter || !reporter.clients.size)
if (!reporter)
throw new Error('WebSocketReporter not found')

const exports = await startMocking(reporter.clients, id)
Expand All @@ -32,7 +31,9 @@ export default (project: WorkspaceProject): Plugin => {

async function startMocking(clients: Map<any, WebSocketRPC>, id: string) {
const errors: unknown[] = []
for (const client of clients.values()) {
// start from the end since it's more likely that the last iframe is the one
// who subscribed with a running test
for (const client of [...clients.values()].reverse()) {
try {
const context = await client.getTestContext()
if (!context)
Expand Down
4 changes: 2 additions & 2 deletions test/browser/specs/runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ describe('running browser tests', async () => {
console.error(stderr)
})

expect(browserResultJson.testResults).toHaveLength(16)
expect(passedTests).toHaveLength(14)
expect(browserResultJson.testResults).toHaveLength(17)
expect(passedTests).toHaveLength(15)
expect(failedTests).toHaveLength(2)

expect(stderr).not.toContain('has been externalized for browser compatibility')
Expand Down
2 changes: 1 addition & 1 deletion test/core/test/injector-esm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ test('dynamic import', async () => {
)
expect(result).toMatchInlineSnapshot(`
"const __vi_inject__ = { [Symbol.toStringTag]: "Module" };
const i = () => __vitest_browser_runner__.wrapModule(import('./foo'))
const i = () => __vitest_browser_runner__.wrapModule(() => import('./foo'))
export { __vi_inject__ }"
`)
})
Expand Down