Skip to content

Commit

Permalink
chore: add workaround for .mjs extension
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Mar 27, 2023
1 parent c66bef0 commit 5ab4065
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
2 changes: 2 additions & 0 deletions packages/browser/src/client/main.ts
Expand Up @@ -57,6 +57,8 @@ ws.addEventListener('open', async () => {
const { getSafeTimers } = await importId('vitest/utils') as typeof import('vitest/utils')
const safeRpc = createSafeRpc(client, getSafeTimers)

// @ts-expect-error untyped global for internal use
globalThis.__vitest_browser__ = true
// @ts-expect-error mocking vitest apis
globalThis.__vitest_worker__ = {
config,
Expand Down
6 changes: 0 additions & 6 deletions packages/browser/src/node/index.ts
Expand Up @@ -18,12 +18,6 @@ export default (base = '/'): Plugin[] => {
{
enforce: 'pre',
name: 'vitest:browser',
async resolveId(id, importer, options) {
if (id.includes('?vitest='))
// Work-around for vite:resolve. The "import('./file.mjs?vitest=123')" fails to load "./file.mts" and throws error.
// This is fixed in Vite 4.3.0
return this.resolve(id.split('?vitest=')[0], importer, options)
},
async config(viteConfig) {
// Enables using ignore hint for coverage providers with @preserve keyword
viteConfig.esbuild ||= {}
Expand Down
10 changes: 9 additions & 1 deletion test/coverage-test/test/coverage.test.ts
@@ -1,8 +1,16 @@
import { expect, test } from 'vitest'
import { pythagoras } from '../src/index.mjs'
import { implicitElse } from '../src/implicitElse'
import { useImportEnv } from '../src/importEnv'

const { pythagoras } = await (() => {
if ('__vitest_browser__' in globalThis)
// TODO: remove workaround after vite 4.3.2
// @ts-expect-error extension is not specified
return import('../src/index')
const dynamicImport = '../src/index.mjs'
return import(dynamicImport)
})()

test('Math.sqrt()', async () => {
expect(pythagoras(3, 4)).toBe(5)
})
Expand Down

0 comments on commit 5ab4065

Please sign in to comment.