Skip to content

Commit 79ef186

Browse files
authored
chore: enable typecheck for tests and scripts (#8114)
1 parent b99cdc8 commit 79ef186

File tree

29 files changed

+48
-32
lines changed

29 files changed

+48
-32
lines changed

.eslintrc.cjs

+2-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ module.exports = defineConfig({
119119
rules: {
120120
'node/no-extraneous-import': 'off',
121121
'node/no-extraneous-require': 'off',
122-
'node/no-missing-import': 'off'
122+
'node/no-missing-import': 'off',
123+
'node/no-missing-require': 'off'
123124
}
124125
},
125126
{

.github/workflows/ci.yml

+4
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,13 @@ jobs:
102102
run: |
103103
pnpm run ci-build-vite
104104
pnpm run build-plugin-vue
105+
pnpm run build-plugin-react
105106
106107
- name: Lint
107108
run: pnpm run lint
108109

109110
- name: Check formatting
110111
run: pnpm prettier --check .
112+
113+
- name: Typecheck
114+
run: pnpm run typecheck

CONTRIBUTING.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ If you want to use break point and explore code execution you can use the ["Run
3434

3535
Some errors are masked and hidden away because of the layers of abstraction and sandboxed nature added by Vitest, Playwright, and Chromium. In order to see what's actually going wrong and the contents of the devtools console in those instances, follow this setup:
3636

37-
1. Add a `debugger` statement to the `scripts/vitestSetup.ts` -> `afterAll` hook. This will pause execution before the tests quit and the Playwright browser instance exits.
37+
1. Add a `debugger` statement to the `playground/vitestSetup.ts` -> `afterAll` hook. This will pause execution before the tests quit and the Playwright browser instance exits.
3838

3939
1. Run the tests with the `debug-serve` script command which will enable remote debugging: `pnpm run debug-serve -- --runInBand resolve`.
4040

@@ -69,7 +69,7 @@ And re-run `pnpm install` to link the package.
6969

7070
### Integration Tests
7171

72-
Each package under `playground/` contains a `__tests__` directory. The tests are run using [Vitest](https://vitest.dev/) + [Playwright](https://playwright.dev/) with custom integrations to make writing tests simple. The detailed setup is inside `vitest.config.e2e.js` and `scripts/vitest*` files.
72+
Each package under `playground/` contains a `__tests__` directory. The tests are run using [Vitest](https://vitest.dev/) + [Playwright](https://playwright.dev/) with custom integrations to make writing tests simple. The detailed setup is inside `vitest.config.e2e.js` and `playground/vitest*` files.
7373

7474
Before running the tests, make sure that [Vite has been built](#repo-setup). On Windows, you may want to [activate Developer Mode](https://docs.microsoft.com/en-us/windows/apps/get-started/enable-your-device-for-development) to solve [issues with symlink creation for non-admins](https://github.com/vitejs/vite/issues/7390). Also you may want to [set git `core.symlinks` to `true` to solve issues with symlinks in git](https://github.com/vitejs/vite/issues/5242).
7575

@@ -107,11 +107,11 @@ test('should work', async () => {
107107

108108
Some common test helpers, e.g. `testDir`, `isBuild` or `editFile` are also available in the utils. Source code is located at `playground/test-utils.ts`.
109109

110-
Note: The test build environment uses a [different default set of Vite config](https://github.com/vitejs/vite/blob/9c6501d9c363eaa3c1e7708d531fb2a92b633db6/scripts/vitestSetup.ts#L102-L122) to skip transpilation during tests to make it faster. This may produce a different result compared to the default production build.
110+
Note: The test build environment uses a [different default set of Vite config](https://github.com/vitejs/vite/blob/main/playground/vitestSetup.ts#L102-L122) to skip transpilation during tests to make it faster. This may produce a different result compared to the default production build.
111111

112112
### Extending the Test Suite
113113

114-
To add new tests, you should find a related playground to the fix or feature (or create a new one). As an example, static assets loading are tested in the [assets playground](https://github.com/vitejs/vite/tree/main/playground/assets). In this Vite App, there is a test for `?raw` imports, with [a section is defined in the `index.html` for it](https://github.com/vitejs/vite/blob/71215533ac60e8ff566dc3467feabfc2c71a01e2/playground/assets/index.html#L121):
114+
To add new tests, you should find a related playground to the fix or feature (or create a new one). As an example, static assets loading are tested in the [assets playground](https://github.com/vitejs/vite/tree/main/playground/assets). In this Vite App, there is a test for `?raw` imports, with [a section is defined in the `index.html` for it](https://github.com/vitejs/vite/blob/main/playground/assets/index.html#L121):
115115

116116
```html
117117
<h2>?raw import</h2>

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"serve-docs": "vitepress serve docs",
2828
"release": "ts-node scripts/release.ts",
2929
"ci-publish": "ts-node scripts/publishCI.ts",
30+
"typecheck": "tsc -p scripts --noEmit && tsc -p playground --noEmit",
3031
"build": "run-s build-vite build-plugin-vue build-plugin-react",
3132
"build-vite": "cd packages/vite && npm run build",
3233
"build-plugin-vue": "cd packages/plugin-vue && npm run build",

playground/cli-module/__tests__/serve.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// this is automatically detected by scripts/vitestSetup.ts and will replace
1+
// this is automatically detected by playground/vitestSetup.ts and will replace
22
// the default e2e test serve behavior
33

44
import execa from 'execa'

playground/cli/__tests__/serve.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// this is automatically detected by scripts/vitestSetup.ts and will replace
1+
// this is automatically detected by playground/vitestSetup.ts and will replace
22
// the default e2e test serve behavior
33

44
import execa from 'execa'

playground/html/__tests__/html.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ describe.runIf(isServe)('invalid', () => {
220220
})
221221

222222
test('should reload when fixed', async () => {
223-
const response = await page.goto(viteTestUrl + '/invalid.html')
223+
await page.goto(viteTestUrl + '/invalid.html')
224224
await editFile('invalid.html', (content) => {
225225
return content.replace('<div Bad', '<div> Good')
226226
})

playground/legacy/__tests__/ssr/serve.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// this is automatically detected by scripts/vitestSetup.ts and will replace
1+
// this is automatically detected by playground/vitestSetup.ts and will replace
22
// the default e2e test serve behavior
33
import path from 'path'
44
import { ports } from '~utils'

playground/lib/__tests__/serve.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// this is automatically detected by scripts/vitestSetup.ts and will replace
1+
// this is automatically detected by playground/vitestSetup.ts and will replace
22
// the default e2e test serve behavior
33

44
import path from 'path'

playground/optimize-missing-deps/__test__/serve.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// this is automatically detected by scripts/vitestSetup.ts and will replace
1+
// this is automatically detected by playground/vitestSetup.ts and will replace
22
// the default e2e test serve behavior
33

44
import path from 'path'

playground/react-emotion/__tests__/react.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ test('should update button style', async () => {
2828
})
2929
}
3030

31-
const styles = await page.evaluate(() => {
31+
await page.evaluate(() => {
3232
return document.querySelector('button').style
3333
})
3434

playground/resolve-config/__tests__/serve.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// this is automatically detected by scripts/vitestSetup.ts and will replace
1+
// this is automatically detected by playground/vitestSetup.ts and will replace
22
// the default e2e test serve behavior
33

44
import path from 'path'

playground/ssr-deps/__tests__/serve.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// this is automatically detected by scripts/vitestSetup.ts and will replace
1+
// this is automatically detected by playground/vitestSetup.ts and will replace
22
// the default e2e test serve behavior
33

44
import path from 'path'

playground/ssr-html/__tests__/serve.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// this is automatically detected by scripts/vitestSetup.ts and will replace
1+
// this is automatically detected by playground/vitestSetup.ts and will replace
22
// the default e2e test serve behavior
33

44
import path from 'path'

playground/ssr-pug/__tests__/serve.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// this is automatically detected by scripts/vitestSetup.ts and will replace
1+
// this is automatically detected by playground/vitestSetup.ts and will replace
22
// the default e2e test serve behavior
33

44
import path from 'path'

playground/ssr-react/__tests__/serve.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// this is automatically detected by scripts/vitestSetup.ts and will replace
1+
// this is automatically detected by playground/vitestSetup.ts and will replace
22
// the default e2e test serve behavior
33

44
import path from 'path'

playground/ssr-react/server.js

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ async function createServer(
6060
render = (await vite.ssrLoadModule('/src/entry-server.jsx')).render
6161
} else {
6262
template = indexProd
63+
// @ts-ignore
6364
render = require('./dist/server/entry-server.js').render
6465
}
6566

playground/ssr-vue/__tests__/serve.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// this is automatically detected by scripts/vitestSetup.ts and will replace
1+
// this is automatically detected by playground/vitestSetup.ts and will replace
22
// the default e2e test serve behavior
33

44
import path from 'path'

playground/ssr-vue/server.js

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ async function createServer(
6363
render = (await vite.ssrLoadModule('/src/entry-server.js')).render
6464
} else {
6565
template = indexProd
66+
// @ts-ignore
6667
render = require('./dist/server/entry-server.js').render
6768
}
6869

playground/ssr-webworker/__tests__/serve.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// this is automatically detected by scripts/vitestSetup.ts and will replace
1+
// this is automatically detected by playground/vitestSetup.ts and will replace
22
// the default e2e test serve behavior
33

44
import path from 'path'

playground/test-utils.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @typescript-eslint/triple-slash-reference */
12
// test utils used in e2e tests for playgrounds.
23
// `import { getColor } from '~utils'`
34

@@ -12,9 +13,9 @@ import type { Manifest } from 'vite'
1213
import { normalizePath } from 'vite'
1314
import { fromComment } from 'convert-source-map'
1415
import { expect } from 'vitest'
15-
import { page } from '../scripts/vitestSetup'
16+
import { page } from './vitestSetup'
1617

17-
export * from '../scripts/vitestSetup'
18+
export * from './vitestSetup'
1819

1920
export const workspaceRoot = path.resolve(__dirname, '../')
2021

playground/tsconfig.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
"exclude": ["**/dist/**"],
44
"compilerOptions": {
55
"target": "es2019",
6+
"module": "esnext",
67
"outDir": "dist",
8+
"baseUrl": ".",
79
"allowJs": true,
810
"esModuleInterop": true,
9-
"moduleResolution": "node",
1011
"resolveJsonModule": true,
11-
"baseUrl": ".",
12+
"moduleResolution": "node",
13+
"skipLibCheck": true,
14+
"noUnusedLocals": true,
1215
"jsx": "preserve",
1316
"types": ["vite/client", "vitest/globals", "node"],
1417
"paths": {
File renamed without changes.

scripts/vitestSetup.ts renamed to playground/vitestSetup.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import type {
1313
ViteDevServer
1414
} from 'vite'
1515
import { build, createServer, mergeConfig } from 'vite'
16-
import type { Browser, ConsoleMessage, Page } from 'playwright-chromium'
16+
import type { Browser, Page } from 'playwright-chromium'
1717
import type { RollupError, RollupWatcher, RollupWatcherEvent } from 'rollup'
1818
import type { File } from 'vitest'
1919
import { beforeAll } from 'vitest'

playground/vue-lib/__tests__/serve.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// this is automatically detected by scripts/vitestSetup.ts and will replace
1+
// this is automatically detected by playground/vitestSetup.ts and will replace
22
// the default e2e test serve behavior
33

44
export async function serve() {

playground/worker/my-shared-worker.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
let count = 0
22
const ports = new Set()
33

4+
// @ts-expect-error
45
onconnect = (event) => {
56
const port = event.ports[0]
67
ports.add(port)
78
port.postMessage(count)
89
port.onmessage = (message) => {
910
if (message.data === 'tick') {
1011
count++
11-
ports.forEach((p) => {
12+
ports.forEach((p: any) => {
1213
p.postMessage(count)
1314
})
1415
}
@@ -17,3 +18,5 @@ onconnect = (event) => {
1718

1819
// for sourcemap
1920
console.log('my-shared-worker.js')
21+
22+
export {}

playground/worker/my-worker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { msg, mode } from './modules/workerImport'
1+
import { mode, msg } from './modules/workerImport'
22
import { bundleWithPlugin } from './modules/test-plugin'
33

44
self.onmessage = (e) => {

scripts/tsconfig.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
{
22
"$schema": "https://json.schemastore.org/tsconfig",
3+
"include": ["."],
34
"compilerOptions": {
45
"module": "commonjs",
5-
"moduleResolution": "node",
66
"target": "es2019",
7+
"moduleResolution": "node",
78
"strict": true,
89
"esModuleInterop": true,
910
"skipLibCheck": true,
10-
"forceConsistentCasingInFileNames": true,
11-
"types": ["node", "vitest/globals"]
11+
"noUnusedLocals": true,
12+
"forceConsistentCasingInFileNames": true
1213
}
1314
}

vitest.config.e2e.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ export default defineConfig({
99
},
1010
test: {
1111
include: ['./playground/**/*.spec.[tj]s'],
12-
setupFiles: ['./scripts/vitestSetup.ts'],
13-
globalSetup: ['./scripts/vitestGlobalSetup.ts'],
12+
setupFiles: ['./playground/vitestSetup.ts'],
13+
globalSetup: ['./playground/vitestGlobalSetup.ts'],
1414
testTimeout: process.env.CI ? 50000 : 20000,
1515
globals: true,
1616
reporters: 'dot',

0 commit comments

Comments
 (0)