Skip to content

Commit

Permalink
refactor: define dependencies correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Jun 7, 2024
1 parent 85d3d35 commit 08faac5
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 14 deletions.
3 changes: 2 additions & 1 deletion packages/browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
"periscopic": "^4.0.2",
"playwright": "^1.44.0",
"playwright-core": "^1.44.0",
"rollup-plugin-copy": "^3.5.0",
"safaridriver": "^0.1.2",
"vitest": "workspace:*",
"webdriverio": "^8.36.1"
Expand All @@ -96,4 +97,4 @@
"msw"
]
}
}
}
27 changes: 23 additions & 4 deletions packages/browser/src/client/mocker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getType } from '@vitest/utils'
import { extname, join } from 'pathe'
import type { SetupWorker } from 'msw/browser'
import { setupWorker } from 'msw/browser'
import { HttpResponse, http, passthrough } from 'msw'
import { rpc } from './rpc'
Expand All @@ -24,9 +25,13 @@ export class VitestBrowserClientMocker {
private factories: Record<string, () => any> = {}

private spyModule!: SpyModule
private mswWorker!: SetupWorker

startWorker() {
const worker = setupWorker(
private _mswStarted = false
private _mswPromise: Promise<unknown> | null = null

setupWorker() {
this.mswWorker = setupWorker(
http.get(/.+/, async ({ request }) => {
const path = cleanTimestamp(request.url.slice(location.origin.length))
if (!(path in this.mocks))
Expand Down Expand Up @@ -63,12 +68,23 @@ export class VitestBrowserClientMocker {
})
}),
)
return worker.start({
}

public async startMSW() {
if (this._mswStarted)
return
if (this._mswPromise)
return this._mswPromise
this._mswPromise = this.mswWorker.start({
serviceWorker: {
url: '/__virtual_vitest__:mocker-worker.js',
},
quiet: true,
}).then(() => {
this._mswStarted = true
this._mswPromise = null
})
await this._mswPromise
}

public setSpyModule(mod: SpyModule) {
Expand Down Expand Up @@ -168,7 +184,10 @@ export class VitestBrowserClientMocker {
public async prepare() {
if (!this.queue.size)
return
await Promise.all([...this.queue.values()])
await Promise.all([
this.startMSW(),
...this.queue.values(),
])
}

// TODO: move this logic into a util(?)
Expand Down
2 changes: 1 addition & 1 deletion packages/browser/src/client/tester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ async function prepareTestEnvironment(files: string[]) {
registerUnexpectedErrors(rpc)

try {
await mocker.startWorker()
await mocker.setupWorker()
}
catch (err) {
console.error(err)
Expand Down
4 changes: 2 additions & 2 deletions packages/vitest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,14 @@
}
},
"dependencies": {
"@ampproject/remapping": "^2.3.0",
"@vitest/expect": "workspace:*",
"@vitest/runner": "workspace:*",
"@vitest/snapshot": "workspace:*",
"@vitest/spy": "workspace:*",
"@vitest/utils": "workspace:*",
"chai": "^5.1.1",
"debug": "^4.3.4",
"execa": "^8.0.1",
"magic-string": "^0.30.10",
"pathe": "^1.1.2",
Expand All @@ -165,7 +167,6 @@
"why-is-node-running": "^2.2.2"
},
"devDependencies": {
"@ampproject/remapping": "^2.3.0",
"@antfu/install-pkg": "0.3.1",
"@edge-runtime/vm": "^3.2.0",
"@sinonjs/fake-timers": "11.1.0",
Expand All @@ -183,7 +184,6 @@
"cac": "^6.7.14",
"chai-subset": "^1.6.0",
"cli-truncate": "^4.0.0",
"debug": "^4.3.4",
"expect-type": "^0.19.0",
"fast-glob": "^3.3.2",
"find-up": "^6.3.0",
Expand Down
12 changes: 6 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 08faac5

Please sign in to comment.