Skip to content

Commit 8d18a91

Browse files
authored
refactor!: remove jest condition (#14544)
1 parent e0eb304 commit 8d18a91

File tree

3 files changed

+4
-29
lines changed

3 files changed

+4
-29
lines changed

packages/vite/src/node/ssr/ssrModuleLoader.ts

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@ import path from 'node:path'
22
import { pathToFileURL } from 'node:url'
33
import colors from 'picocolors'
44
import type { ViteDevServer } from '../server'
5-
import {
6-
dynamicImport,
7-
isBuiltin,
8-
unwrapId,
9-
usingDynamicImport,
10-
} from '../utils'
5+
import { dynamicImport, isBuiltin, unwrapId } from '../utils'
116
import { transformRequest } from '../server/transformRequest'
127
import type { InternalResolveOptionsWithOverrideConditions } from '../plugins/resolve'
138
import { tryNodeResolve } from '../plugins/resolve'
@@ -278,12 +273,7 @@ async function nodeImport(
278273
const resolved = tryNodeResolve(
279274
id,
280275
importer,
281-
// Non-external modules can import ESM-only modules, but only outside
282-
// of test runs, because we use Node `require` in Jest to avoid segfault.
283-
// @ts-expect-error jest only exists when running Jest
284-
typeof jest === 'undefined'
285-
? { ...resolveOptions, tryEsmOnly: true }
286-
: resolveOptions,
276+
{ ...resolveOptions, tryEsmOnly: true },
287277
false,
288278
undefined,
289279
true,
@@ -295,10 +285,7 @@ async function nodeImport(
295285
err.code = 'ERR_MODULE_NOT_FOUND'
296286
throw err
297287
}
298-
url = resolved.id
299-
if (usingDynamicImport) {
300-
url = pathToFileURL(url).toString()
301-
}
288+
url = pathToFileURL(resolved.id).toString()
302289
}
303290

304291
const mod = await dynamicImport(url)

packages/vite/src/node/utils.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -955,21 +955,12 @@ export const multilineCommentsRE = /\/\*[^*]*\*+(?:[^/*][^*]*\*+)*\//g
955955
export const singlelineCommentsRE = /\/\/.*/g
956956
export const requestQuerySplitRE = /\?(?!.*[/|}])/
957957

958-
// @ts-expect-error jest only exists when running Jest
959-
export const usingDynamicImport = typeof jest === 'undefined'
960-
961958
/**
962959
* Dynamically import files. It will make sure it's not being compiled away by TS/Rollup.
963960
*
964-
* As a temporary workaround for Jest's lack of stable ESM support, we fallback to require
965-
* if we're in a Jest environment.
966-
* See https://github.com/vitejs/vite/pull/5197#issuecomment-938054077
967-
*
968961
* @param file File path to import.
969962
*/
970-
export const dynamicImport = usingDynamicImport
971-
? new Function('file', 'return import(file)')
972-
: _require
963+
export const dynamicImport = new Function('file', 'return import(file)')
973964

974965
export function parseRequest(id: string): Record<string, string> | null {
975966
const [_, search] = id.split(requestQuerySplitRE, 2)

playground/ssr-deps/__tests__/ssr-deps.spec.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ const url = `http://localhost:${port}`
66

77
/**
88
* test for #5809
9-
*
10-
* NOTE: This test will always succeed now, unless the temporary workaround for Jest can be removed
11-
* See https://github.com/vitejs/vite/pull/5197#issuecomment-938054077
129
*/
1310
test('msg should be encrypted', async () => {
1411
await page.goto(url)

0 commit comments

Comments
 (0)