Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: improve dx for tests #8112

Merged
merged 10 commits into from
May 11, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,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
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 { editFile, getColor, page, untilUpdated } from '~utils'
import { test, expect } from 'vitest'

test('fs', async () => {
expect(await page.textContent('.fs')).toMatch('[success] alias to fs path')
Expand Down
8 changes: 6 additions & 2 deletions playground/assets/__tests__/assets.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ import {
readFile,
editFile,
notifyRebuildComplete,
untilUpdated
} from '../../testUtils'
untilUpdated,
browserLogs,
page,
watcher
} from '~utils'
import { test, expect, describe } from 'vitest'

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,3 +1,4 @@
import { page } from '~utils'
import { port } from './serve'

test('cli should work in "type":"module" package', async () => {
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, viteBinPath, ports } 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,3 +1,4 @@
import { page } from '~utils'
import { port } from './serve'

test('cli should work', async () => {
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 { ports, isWindows, viteBinPath } from '~utils'
import kill from 'kill-port'

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,4 +1,4 @@
import { getColor, getBgColor, ports } from '../../testUtils'
import { getColor, getBgColor, ports, page } from '~utils'
import { createServer } from 'vite'
import path from 'path'

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 { isBuild, findAssetFile, 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 { page } from '~utils'
import viteConfig from '../vite.config'

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
Expand Up @@ -3,8 +3,9 @@ import {
untilUpdated,
removeFile,
addFile,
isBuild
} from '../../testUtils'
isBuild,
page
} from '~utils'

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

const json = require('../safe.json')
const stringified = JSON.stringify(json)
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 {
isBuild,
editFile,
untilUpdated,
getBg,
page,
browserLogs,
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
2 changes: 1 addition & 1 deletion playground/html/__tests__/html.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getColor, isBuild, editFile, isServe } from '../../testUtils'
import { getColor, isBuild, editFile, isServe, page, viteTestUrl } from '~utils'

function testPage(isNested: boolean) {
test('pre transform', async () => {
Expand Down
2 changes: 1 addition & 1 deletion playground/js-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/js-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,
isBuild
} from '../../testUtils'
isBuild,
page,
serverLogs
} from '~utils'

if (!isBuild) {
test('js', async () => {
Expand Down
2 changes: 1 addition & 1 deletion playground/json/__tests__/json.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 json = require('../test.json')
const deepJson = require('vue/package.json')
Expand Down
5 changes: 3 additions & 2 deletions playground/legacy/__tests__/legacy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import {
isBuild,
readManifest,
untilUpdated,
getColor
} from '../../testUtils'
getColor,
page
} from '~utils'

test('should work', async () => {
expect(await page.textContent('#app')).toMatch('Hello')
Expand Down
2 changes: 1 addition & 1 deletion playground/legacy/__tests__/ssr/legacy-ssr.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'
import { port } from './serve'

const url = `http://localhost:${port}`
Expand Down
2 changes: 1 addition & 1 deletion playground/legacy/__tests__/ssr/serve.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// this is automatically detected by scripts/vitestSetup.ts and will replace
// the default e2e test serve behavior
import path from 'path'
import { ports } from '../../../testUtils'
import { ports } from '~utils'

export const port = ports['legacy/ssr']

Expand Down
9 changes: 8 additions & 1 deletion playground/lib/__tests__/lib.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { isBuild, isServe, testDir, untilUpdated } from '../../testUtils'
import {
isBuild,
isServe,
testDir,
untilUpdated,
page,
serverLogs
} from '~utils'
import path from 'path'
import fs from 'fs'

Expand Down
Loading