Skip to content

Commit 9608bf7

Browse files
authored
feat!: transformMode affects only test files, not regular files (#3491)
1 parent 7c8117a commit 9608bf7

File tree

33 files changed

+236
-168
lines changed

33 files changed

+236
-168
lines changed

docs/config/index.md

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,41 +1120,28 @@ Will call [`vi.unstubAllEnvs`](/api/vi#vi-unstuballenvs) before each test.
11201120

11211121
Will call [`vi.unstubAllGlobals`](/api/vi#vi-unstuballglobals) before each test.
11221122

1123-
### transformMode
1123+
### testTransformMode
11241124

1125-
- **Type:** `{ web?, ssr? }`
1125+
- **Type:** `{ web?, ssr? }`
1126+
- **Version:** Since Vitest 0.32.0
11261127

1127-
Determine the transform method of modules
1128+
Determine the transform method for all modules inported inside a test that matches the glob pattern. By default, relies on the environment. For example, tests with JSDOM environment will process all files with `ssr: false` flag and tests with Node environment process all modules with `ssr: true`.
11281129

1129-
#### transformMode.ssr
1130+
#### testTransformMode.ssr
11301131

1131-
- **Type:** `RegExp[]`
1132-
- **Default:** `[/\.([cm]?[jt]sx?|json)$/]`
1132+
- **Type:** `string[]`
1133+
- **Default:** `[]`
11331134

1134-
Use SSR transform pipeline for the specified files.<br>
1135-
Vite plugins will receive `ssr: true` flag when processing those files.
1135+
Use SSR transform pipeline for all modules inside specified tests.<br>
1136+
Vite plugins will receive `ssr: true` flag when processing those files.
11361137

1137-
#### transformMode&#46;web
1138+
#### testTransformMode&#46;web
11381139

1139-
- **Type:** `RegExp[]`
1140-
- **Default:** *modules other than those specified in `transformMode.ssr`*
1140+
- **Type:** `string[]`
1141+
- **Default:** `[]`
11411142

1142-
First do a normal transform pipeline (targeting browser), then do a SSR rewrite to run the code in Node.<br>
1143-
Vite plugins will receive `ssr: false` flag when processing those files.
1144-
1145-
When you use JSX as component models other than React (e.g. Vue JSX or SolidJS), you might want to config as following to make `.tsx` / `.jsx` transformed as client-side components:
1146-
1147-
```ts
1148-
import { defineConfig } from 'vitest/config'
1149-
1150-
export default defineConfig({
1151-
test: {
1152-
transformMode: {
1153-
web: [/\.[jt]sx$/],
1154-
},
1155-
},
1156-
})
1157-
```
1143+
First do a normal transform pipeline (targeting browser), then do a SSR rewrite to run the code in Node.<br>
1144+
Vite plugins will receive `ssr: false` flag when processing those files.
11581145

11591146
### snapshotFormat<NonProjectOption />
11601147

examples/react-storybook/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"@testing-library/react": "^12.1.5",
2929
"@types/react": "^17.0.45",
3030
"@types/react-dom": "^17.0.17",
31-
"@vitejs/plugin-react": "^1.3.2",
31+
"@vitejs/plugin-react": "^4.0.1",
3232
"@vitest/ui": "latest",
3333
"babel-loader": "^8.2.5",
3434
"jsdom": "latest",

examples/react-testing-lib-msw/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"@testing-library/user-event": "^13.5.0",
2020
"@types/react": "^17.0.45",
2121
"@types/react-dom": "^17.0.17",
22-
"@vitejs/plugin-react": "^1.3.2",
22+
"@vitejs/plugin-react": "^4.0.1",
2323
"@vitest/ui": "latest",
2424
"cross-fetch": "^3.1.5",
2525
"jsdom": "latest",

examples/solid/vite.config.mjs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ import solid from 'vite-plugin-solid'
77
export default defineConfig({
88
test: {
99
environment: 'jsdom',
10-
transformMode: {
11-
web: [/.[jt]sx?/],
12-
},
1310
threads: false,
1411
isolate: false,
1512
},

examples/vue-jsx/vite.config.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,5 @@ export default defineConfig({
77
test: {
88
globals: true,
99
environment: 'jsdom',
10-
transformMode: {
11-
web: [/.[tj]sx$/],
12-
},
1310
},
1411
})

packages/vitest/src/integrations/env/edge-runtime.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { populateGlobal } from './utils'
44

55
export default <Environment>({
66
name: 'edge-runtime',
7+
transformMode: 'ssr',
78
async setup(global) {
89
const { EdgeVM } = await importModule('@edge-runtime/vm') as typeof import('@edge-runtime/vm')
910
const vm = new EdgeVM({

packages/vitest/src/integrations/env/happy-dom.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { populateGlobal } from './utils'
44

55
export default <Environment>({
66
name: 'happy-dom',
7+
transformMode: 'web',
78
async setup(global) {
89
// happy-dom v3 introduced a breaking change to Window, but
910
// provides GlobalWindow as a way to use previous behaviour

packages/vitest/src/integrations/env/index.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import type { VitestEnvironment } from '../../types/config'
1+
import type { BuiltinEnvironment, VitestEnvironment } from '../../types/config'
2+
import type { VitestExecutor } from '../../node'
3+
import type { Environment } from '../../types'
24
import node from './node'
35
import jsdom from './jsdom'
46
import happy from './happy-dom'
@@ -19,10 +21,28 @@ export const envPackageNames: Record<Exclude<keyof typeof environments, 'node'>,
1921
'edge-runtime': '@edge-runtime/vm',
2022
}
2123

24+
function isBuiltinEnvironment(env: VitestEnvironment): env is BuiltinEnvironment {
25+
return env in environments
26+
}
27+
2228
export function getEnvPackageName(env: VitestEnvironment) {
2329
if (env === 'node')
2430
return null
2531
if (env in envPackageNames)
2632
return (envPackageNames as any)[env]
2733
return `vitest-environment-${env}`
2834
}
35+
36+
export async function loadEnvironment(name: VitestEnvironment, executor: VitestExecutor): Promise<Environment> {
37+
if (isBuiltinEnvironment(name))
38+
return environments[name]
39+
const packageId = (name[0] === '.' || name[0] === '/') ? name : `vitest-environment-${name}`
40+
const pkg = await executor.executeId(packageId)
41+
if (!pkg || !pkg.default || typeof pkg.default !== 'object' || typeof pkg.default.setup !== 'function') {
42+
throw new Error(
43+
`Environment "${name}" is not a valid environment. `
44+
+ `Path "${packageId}" should export default object with a "setup" method.`,
45+
)
46+
}
47+
return pkg.default
48+
}

packages/vitest/src/integrations/env/jsdom.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ function catchWindowErrors(window: Window) {
2828

2929
export default <Environment>({
3030
name: 'jsdom',
31+
transformMode: 'web',
3132
async setup(global, { jsdom = {} }) {
3233
const {
3334
CookieJar,

packages/vitest/src/integrations/env/node.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { Environment } from '../../types'
33

44
export default <Environment>({
55
name: 'node',
6+
transformMode: 'ssr',
67
async setup(global) {
78
global.console.Console = Console
89
return {

0 commit comments

Comments
 (0)