Skip to content

Commit

Permalink
test: improve dx for tests (vitejs#8112)
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu authored May 11, 2022
1 parent 43a58dd commit d287482
Show file tree
Hide file tree
Showing 87 changed files with 259 additions and 209 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ module.exports = defineConfig({
files: ['playground/**'],
rules: {
'node/no-extraneous-import': 'off',
'node/no-extraneous-require': 'off'
'node/no-extraneous-require': 'off',
'node/no-missing-import': 'off'
}
},
{
Expand Down
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ packages/vite/temp/
packages/plugin-react/dist/
packages/plugin-vue/dist/
packages/*/CHANGELOG.md
playground-temp/
LICENSE.md
.prettierignore
pnpm-lock.yaml
Expand Down
6 changes: 4 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,17 @@ Other than tests under `playground/` for integration tests, packages might conta

### Test Env and Helpers

Inside playground tests, a global `page` object is automatically available, which is a Playwright [`Page`](https://playwright.dev/docs/api/class-page) instance that has already navigated to the served page of the current playground. So writing a test is as simple as:
Inside playground tests, you can import the `page` object from `~utils`, which is a Playwright [`Page`](https://playwright.dev/docs/api/class-page) instance that has already navigated to the served page of the current playground. So writing a test is as simple as:

```js
import { page } from '~utils'

test('should work', async () => {
expect(await page.textContent('.foo')).toMatch('foo')
})
```

Some common test helpers, e.g. `testDir`, `isBuild` or `editFile` are available in `playground/testUtils.ts`.
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`.

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.

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"scripts": {
"preinstall": "npx only-allow pnpm",
"format": "prettier --write .",
"lint": "eslint packages/*/{src,types}/** playground/**/__tests__/**/*.*",
"lint": "eslint packages/*/{src,types}/** playground/**/__tests__/** scripts/**",
"test": "run-s test-unit test-serve test-build",
"test-serve": "vitest run -c vitest.config.e2e.ts",
"test-build": "cross-env VITE_TEST_BUILD=1 vitest run -c vitest.config.e2e.ts",
Expand Down
3 changes: 2 additions & 1 deletion playground/alias/__tests__/alias.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { editFile, getColor, untilUpdated } from '../../testUtils'
import { expect, test } from 'vitest'
import { editFile, getColor, page, untilUpdated } from '~utils'

test('fs', async () => {
expect(await page.textContent('.fs')).toMatch('[success] alias to fs path')
Expand Down
14 changes: 9 additions & 5 deletions playground/assets/__tests__/assets.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { describe, expect, test } from 'vitest'
import {
browserLogs,
editFile,
findAssetFile,
getBg,
getColor,
isBuild,
listAssets,
readManifest,
readFile,
editFile,
notifyRebuildComplete,
untilUpdated
} from '../../testUtils'
page,
readFile,
readManifest,
untilUpdated,
watcher
} from '~utils'

const assetMatch = isBuild
? /\/foo\/assets\/asset\.\w{8}\.png/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import {
browserErrors,
browserLogs,
editFile,
getColor,
isBuild,
isServe,
page,
readManifest,
untilUpdated
} from '../../testUtils'
} from '~utils'

const outerAssetMatch = isBuild
? /\/dev\/assets\/logo\.\w{8}\.png/
Expand Down
1 change: 1 addition & 0 deletions playground/cli-module/__tests__/cli-module.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { port } from './serve'
import { page } from '~utils'

test('cli should work in "type":"module" package', async () => {
// this test uses a custom serve implementation, so regular helpers for browserLogs and goto don't work
Expand Down
9 changes: 3 additions & 6 deletions playground/cli-module/__tests__/serve.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
// this is automatically detected by scripts/vitestSetup.ts and will replace
// the default e2e test serve behavior

import path from 'path'
import execa from 'execa'
import { workspaceRoot, ports } from '../../testUtils'
import kill from 'kill-port'
import { isWindows, ports, viteBinPath } from '~utils'

const isWindows = process.platform === 'win32'
export const port = ports['cli-module']
const viteBin = path.join(workspaceRoot, 'packages', 'vite', 'bin', 'vite.js')

export async function serve(root: string, isProd: boolean) {
// collect stdout and stderr streams from child processes here to avoid interfering with regular jest output
Expand Down Expand Up @@ -39,7 +36,7 @@ export async function serve(root: string, isProd: boolean) {

// only run `vite build` when needed
if (isProd) {
const buildCommand = `${viteBin} build`
const buildCommand = `${viteBinPath} build`
try {
const buildProcess = execa.command(buildCommand, {
cwd: root,
Expand All @@ -62,7 +59,7 @@ export async function serve(root: string, isProd: boolean) {
if (isProd) {
viteServerArgs.unshift('preview')
}
const serverCommand = `${viteBin} ${viteServerArgs.join(' ')}`
const serverCommand = `${viteBinPath} ${viteServerArgs.join(' ')}`
const serverProcess = execa.command(serverCommand, {
cwd: root,
stdio: 'pipe'
Expand Down
1 change: 1 addition & 0 deletions playground/cli/__tests__/cli.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { port } from './serve'
import { page } from '~utils'

test('cli should work', async () => {
// this test uses a custom serve implementation, so regular helpers for browserLogs and goto don't work
Expand Down
11 changes: 4 additions & 7 deletions playground/cli/__tests__/serve.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
// this is automatically detected by scripts/vitestSetup.ts and will replace
// the default e2e test serve behavior

import path from 'path'
import execa from 'execa'
import { workspaceRoot, ports } from '../../testUtils'
import kill from 'kill-port'
import { isWindows, ports, viteBinPath } from '~utils'

const isWindows = process.platform === 'win32'
const port = (exports.port = ports.cli)
const viteBin = path.join(workspaceRoot, 'packages', 'vite', 'bin', 'vite.js')
export const port = ports.cli

export async function serve(root: string, isProd: boolean) {
// collect stdout and stderr streams from child processes here to avoid interfering with regular jest output
Expand Down Expand Up @@ -39,7 +36,7 @@ export async function serve(root: string, isProd: boolean) {

// only run `vite build` when needed
if (isProd) {
const buildCommand = `${viteBin} build`
const buildCommand = `${viteBinPath} build`
try {
const buildProcess = execa.command(buildCommand, {
cwd: root,
Expand All @@ -62,7 +59,7 @@ export async function serve(root: string, isProd: boolean) {
if (isProd) {
viteServerArgs.unshift('preview')
}
const serverCommand = `${viteBin} ${viteServerArgs.join(' ')}`
const serverCommand = `${viteBinPath} ${viteServerArgs.join(' ')}`
const serverProcess = execa.command(serverCommand, {
cwd: root,
stdio: 'pipe'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { findAssetFile, getColor, isBuild, readManifest } from '../../testUtils'
import { findAssetFile, getColor, isBuild, readManifest } from '~utils'

test('should load both stylesheets', async () => {
expect(await getColor('h1')).toBe('red')
Expand Down
2 changes: 1 addition & 1 deletion playground/css-codesplit/__tests__/css-codesplit.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { findAssetFile, getColor, isBuild, readManifest } from '../../testUtils'
import { findAssetFile, getColor, isBuild, page, readManifest } from '~utils'

test('should load all stylesheets', async () => {
expect(await getColor('h1')).toBe('red')
Expand Down
2 changes: 1 addition & 1 deletion playground/css-sourcemap/__tests__/build.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isBuild } from '../../testUtils'
import { isBuild, serverLogs } from '~utils'

test.runIf(isBuild)('should not output sourcemap warning (#4939)', () => {
serverLogs.forEach((log) => {
Expand Down
6 changes: 4 additions & 2 deletions playground/css-sourcemap/__tests__/serve.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { URL } from 'url'
import {
extractSourcemap,
formatSourcemapForSnapshot,
isServe
} from '../../testUtils'
isServe,
page,
serverLogs
} from '~utils'

describe.runIf(isServe)('serve', () => {
const getStyleTagContentIncluding = async (content: string) => {
Expand Down
7 changes: 3 additions & 4 deletions playground/css/__tests__/css.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import fs from 'fs'
import path from 'path'
import {
editFile,
findAssetFile,
getBg,
getColor,
isBuild,
page,
removeFile,
testDir,
serverLogs,
untilUpdated
} from '../../testUtils'
} from '~utils'

// note: tests should retrieve the element at the beginning of test and reuse it
// in later assertions to ensure CSS HMR doesn't reload the page
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getColor, getBgColor, ports } from '../../testUtils'
import { createServer } from 'vite'
import path from 'path'
import { createServer } from 'vite'
import { getBgColor, getColor, page, ports } from '~utils'

// Regression test for https://github.com/vitejs/vite/issues/4000
test('postcss plugins in different dir', async () => {
Expand Down
2 changes: 1 addition & 1 deletion playground/css/postcss-caching/css.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getColor, ports } from '../../testUtils'
import { getColor, page, ports } from '~utils'
import { createServer } from 'vite'
import path from 'path'

Expand Down
2 changes: 1 addition & 1 deletion playground/data-uri/__tests__/data-uri.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isBuild, findAssetFile } from '../../testUtils'
import { findAssetFile, isBuild, page } from '~utils'

test('plain', async () => {
expect(await page.textContent('.plain')).toBe('hi')
Expand Down
5 changes: 4 additions & 1 deletion playground/define/__tests__/define.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import viteConfig from '../vite.config'
import { page } from '~utils'

test('string', async () => {
const defines = require('../vite.config.js').define
const defines = viteConfig.define

expect(await page.textContent('.exp')).toBe(
String(typeof eval(defines.__EXP__))
Expand Down
2 changes: 1 addition & 1 deletion playground/dynamic-import/__tests__/dynamic-import.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getColor, isBuild, untilUpdated } from '../../testUtils'
import { getColor, page, serverLogs, untilUpdated } from '~utils'

test('should load literal dynamic import', async () => {
await page.click('.baz')
Expand Down
2 changes: 1 addition & 1 deletion playground/env-nested/__tests__/env-nested.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isBuild } from '../../testUtils'
import { isBuild, page } from '~utils'

const mode = isBuild ? `production` : `development`

Expand Down
2 changes: 1 addition & 1 deletion playground/env/__tests__/env.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isBuild } from '../../testUtils'
import { isBuild, page } from '~utils'

const mode = isBuild ? `production` : `development`

Expand Down
2 changes: 2 additions & 0 deletions playground/extensions/__tests__/extensions.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { browserLogs, page } from '~utils'

test('should have no 404s', () => {
browserLogs.forEach((msg) => {
expect(msg).not.toMatch('404')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import {
addFile,
editFile,
untilUpdated,
isBuild,
page,
removeFile,
addFile,
isBuild
} from '../../testUtils'
untilUpdated
} from '~utils'

test.runIf(isBuild)(
'should hmr when file is deleted and restored',
Expand Down
3 changes: 1 addition & 2 deletions playground/fs-serve/__tests__/fs-serve.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { isServe } from '../../testUtils'
import { isServe, page, viteTestUrl } from '~utils'

const json = require('../safe.json')
const stringified = JSON.stringify(json)

describe.runIf(isServe)('main', () => {
beforeAll(async () => {
// viteTestUrl is globally injected in scripts/vitestSetup.ts
await page.goto(viteTestUrl + '/src/')
})

Expand Down
3 changes: 2 additions & 1 deletion playground/glob-import/__tests__/glob-import.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import {
addFile,
editFile,
isBuild,
page,
removeFile,
untilUpdated
} from '../../testUtils'
} from '~utils'

const filteredResult = {
'./alias.js': {
Expand Down
10 changes: 9 additions & 1 deletion playground/hmr/__tests__/hmr.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { isBuild, editFile, untilUpdated, getBg } from '../../testUtils'
import {
browserLogs,
editFile,
getBg,
isBuild,
page,
untilUpdated,
viteTestUrl
} from '~utils'

test('should render', async () => {
expect(await page.textContent('.app')).toBe('1')
Expand Down
3 changes: 2 additions & 1 deletion playground/hmr/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"extends": "../tsconfig.json",
"include": ["."],
"exclude": ["**/dist/**"],
"exclude": ["**/dist/**", "**/__tests__/**"],
"compilerOptions": {
"target": "es2019",
"module": "esnext",
Expand Down
Loading

0 comments on commit d287482

Please sign in to comment.