Skip to content

Commit

Permalink
chore: rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
poyoho committed May 9, 2022
2 parents 88a2e7f + 1ffc010 commit 556af65
Show file tree
Hide file tree
Showing 41 changed files with 256 additions and 371 deletions.
4 changes: 2 additions & 2 deletions .eslintrc.cjs
Expand Up @@ -63,7 +63,7 @@ module.exports = defineConfig({
'node/no-extraneous-import': [
'error',
{
allowModules: ['vite', 'less', 'sass']
allowModules: ['vite', 'less', 'sass', 'vitest']
}
],
'node/no-extraneous-require': [
Expand Down Expand Up @@ -103,7 +103,7 @@ module.exports = defineConfig({
}
},
{
files: ['packages/vite/types/**'],
files: ['packages/vite/types/**', '*.spec.ts'],
rules: {
'node/no-extraneous-import': 'off'
}
Expand Down
7 changes: 3 additions & 4 deletions .github/workflows/ci.yml
Expand Up @@ -47,8 +47,6 @@ jobs:

- name: Install pnpm
uses: pnpm/action-setup@v2
with:
version: 6

- name: Set node version to ${{ matrix.node_version }}
uses: actions/setup-node@v3
Expand All @@ -68,6 +66,9 @@ jobs:
- name: Build plugin-react
run: pnpm run build-plugin-react

- name: Test unit
run: pnpm run test-unit

- name: Test serve
run: pnpm run test-serve -- --runInBand

Expand All @@ -85,8 +86,6 @@ jobs:

- name: Install pnpm
uses: pnpm/action-setup@v2
with:
version: 6

- name: Set node version to 16
uses: actions/setup-node@v3
Expand Down
14 changes: 12 additions & 2 deletions CONTRIBUTING.md
Expand Up @@ -67,13 +67,15 @@ And re-run `pnpm install` to link the package.

## Running Tests

### Integration Tests

Each package under `packages/playground/` contains a `__tests__` directory. The tests are run using [Jest](https://jestjs.io/) + [Playwright](https://playwright.dev/) with custom integrations to make writing tests simple. The detailed setup is inside `jest.config.js` and `scripts/jest*` files.

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).

Each test can be run under either dev server mode or build mode.
Each integration test can be run under either dev server mode or build mode.

- `pnpm test` by default runs every test in both serve and build mode.
- `pnpm test` by default runs every integration test in both serve and build mode, and also unit tests.

- `pnpm run test-serve` runs tests only under serve mode. This is just calling `jest` so you can pass any Jest flags to this command. Since Jest will attempt to run tests in parallel, if your machine has many cores this may cause flaky test failures with multiple Playwright instances running at the same time. You can force the tests to run in series with `pnpm run test-serve -- --runInBand`.

Expand All @@ -83,6 +85,14 @@ Each test can be run under either dev server mode or build mode.

Note package matching is not available for the `pnpm test` script, which always runs all tests.

### Unit Tests

Other than tests under `packages/playground/` for integration tests, packages might contains unit tests under their `__tests__` directory. Unit tests are powered by [Vitest](https://vitest.dev/). The detailed config is inside `vitest.config.ts` files.

- `pnpm run test-unit` runs unit tests under each package.

- You can also use `pnpm run test-unit -- [match]` to run related tests.

### 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:
Expand Down
4 changes: 1 addition & 3 deletions jest.config.ts
Expand Up @@ -3,9 +3,7 @@ import type { Config } from '@jest/types'

const config: Config.InitialOptions = {
preset: 'ts-jest',
testMatch: process.env.VITE_TEST_BUILD
? ['**/playground/**/*.spec.[jt]s?(x)']
: ['**/*.spec.[jt]s?(x)'],
testMatch: ['**/playground/**/*.spec.[jt]s?(x)'],
testTimeout: process.env.CI ? 50000 : 20000,
globalSetup: './scripts/jestGlobalSetup.cjs',
globalTeardown: './scripts/jestGlobalTeardown.cjs',
Expand Down
8 changes: 5 additions & 3 deletions package.json
Expand Up @@ -16,10 +16,11 @@
"preinstall": "npx only-allow pnpm",
"format": "prettier --write .",
"lint": "eslint packages/*/{src,types}/**",
"test": "run-s test-serve test-build",
"test": "run-s test-unit test-serve test-build",
"test-serve": "jest",
"debug-serve": "cross-env VITE_DEBUG_SERVE=1 node --inspect-brk ./node_modules/.bin/jest",
"test-build": "cross-env VITE_TEST_BUILD=1 jest",
"test-unit": "vitest run",
"debug-serve": "cross-env VITE_DEBUG_SERVE=1 node --inspect-brk ./node_modules/.bin/jest",
"debug-build": "cross-env VITE_TEST_BUILD=1 VITE_PRESERVE_BUILD_ARTIFACTS=1 node --inspect-brk ./node_modules/.bin/jest",
"docs": "vitepress dev docs",
"build-docs": "vitepress build docs",
Expand Down Expand Up @@ -68,7 +69,8 @@
"ts-node": "^10.4.0",
"typescript": "~4.5.4",
"vite": "workspace:*",
"vitepress": "^0.22.3"
"vitepress": "^0.22.3",
"vitest": "^0.10.4"
},
"simple-git-hooks": {
"pre-commit": "pnpm exec lint-staged --concurrent false",
Expand Down
1 change: 1 addition & 0 deletions packages/create-vite/__tests__/cli.spec.ts
Expand Up @@ -3,6 +3,7 @@ import type { ExecaSyncReturnValue, SyncOptions } from 'execa'
import { commandSync } from 'execa'
import { mkdirpSync, readdirSync, remove, writeFileSync } from 'fs-extra'
import { join } from 'path'
import { test, expect, beforeAll, afterEach } from 'vitest'

const CLI_PATH = join(__dirname, '..')

Expand Down
7 changes: 7 additions & 0 deletions packages/playground/lib/__tests__/lib.spec.ts
Expand Up @@ -35,6 +35,13 @@ if (isBuild) {
)
expect(code).not.toMatch('__vitePreload')
})

test('@import hoist', async () => {
serverLogs.forEach((log) => {
// no warning from esbuild css minifier
expect(log).not.toMatch('All "@import" rules must come first')
})
})
} else {
test('dev', async () => {
expect(await page.textContent('.demo')).toBe('It works')
Expand Down
14 changes: 13 additions & 1 deletion packages/playground/lib/__tests__/serve.js
Expand Up @@ -9,13 +9,17 @@ const { ports } = require('../../testUtils')

const port = (exports.port = ports.lib)

global.serverLogs = []

/**
* @param {string} root
* @param {boolean} isBuildTest
*/
exports.serve = async function serve(root, isBuildTest) {
// build first

setupConsoleWarnCollector()

if (!isBuildTest) {
const { createServer } = require('vite')
process.env.VITE_INLINE = 'inline-serve'
Expand Down Expand Up @@ -55,7 +59,7 @@ exports.serve = async function serve(root, isBuildTest) {

await build({
root,
logLevel: 'silent',
logLevel: 'warn', // output esbuild warns
configFile: path.resolve(__dirname, '../vite.dyimport.config.js')
})

Expand Down Expand Up @@ -89,3 +93,11 @@ exports.serve = async function serve(root, isBuildTest) {
})
}
}

function setupConsoleWarnCollector() {
const warn = console.warn
console.warn = (...args) => {
global.serverLogs.push(args.join(' '))
return warn.call(console, ...args)
}
}
4 changes: 4 additions & 0 deletions packages/playground/lib/src/dynamic.css
@@ -0,0 +1,4 @@
@import 'https://cdn.jsdelivr.net/npm/@mdi/font@5.9.55/css/materialdesignicons.min.css';
.dynamic {
color: red;
}
3 changes: 3 additions & 0 deletions packages/playground/lib/src/index.css
@@ -0,0 +1,3 @@
.index {
color: blue;
}
5 changes: 5 additions & 0 deletions packages/playground/lib/src/main2.js
@@ -1,4 +1,9 @@
import './index.css'

export default async function message(sel) {
const message = await import('./message.js')

await import('./dynamic.css')

document.querySelector(sel).textContent = message.default
}
1 change: 0 additions & 1 deletion packages/playground/lib/vite.dyimport.config.js
Expand Up @@ -6,7 +6,6 @@ const path = require('path')
*/
module.exports = {
build: {
minify: false,
lib: {
entry: path.resolve(__dirname, 'src/main2.js'),
formats: ['es'],
Expand Down
@@ -1,5 +1,6 @@
import babelRestoreJSX from './babel-restore-jsx'
import * as babel from '@babel/core'
import { describe, it, expect } from 'vitest'

function jsx(code: string) {
return babel.transform(code, {
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-react/src/jsx-runtime/restore-jsx.spec.ts
@@ -1,5 +1,6 @@
import { restoreJSX } from './restore-jsx'
import * as babel from '@babel/core'
import { describe, it, expect } from 'vitest'

async function jsx(sourceCode: string) {
const [ast] = await restoreJSX(babel, sourceCode, 'test.js')
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-vue/src/template.ts
Expand Up @@ -139,7 +139,7 @@ export function resolveTemplateCompilerOptions(
tags: transformAssetUrls as any
}
} else {
transformAssetUrls = { ...transformAssetUrls, ...assetUrlOptions }
transformAssetUrls = { ...assetUrlOptions, ...transformAssetUrls }
}
} else {
transformAssetUrls = assetUrlOptions
Expand Down
29 changes: 29 additions & 0 deletions packages/vite/LICENSE.md
Expand Up @@ -3465,6 +3465,35 @@ Repository: chalk/strip-ansi
---------------------------------------

## strip-literal
License: MIT
By: Anthony Fu
Repository: git+https://github.com/antfu/strip-literal.git

> MIT License
>
> Copyright (c) 2022 Anthony Fu <https://github.com/antfu>
>
> Permission is hereby granted, free of charge, to any person obtaining a copy
> of this software and associated documentation files (the "Software"), to deal
> in the Software without restriction, including without limitation the rights
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
> copies of the Software, and to permit persons to whom the Software is
> furnished to do so, subject to the following conditions:
>
> The above copyright notice and this permission notice shall be included in all
> copies or substantial portions of the Software.
>
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
> SOFTWARE.
---------------------------------------

## to-regex-range
License: MIT
By: Jon Schlinkert, Rouven Weßling
Expand Down
1 change: 1 addition & 0 deletions packages/vite/package.json
Expand Up @@ -112,6 +112,7 @@
"source-map-js": "^1.0.2",
"source-map-support": "^0.5.21",
"strip-ansi": "^6.0.1",
"strip-literal": "^0.2.0",
"terser": "^5.13.1",
"tsconfck": "^1.2.2",
"tslib": "^2.4.0",
Expand Down
1 change: 1 addition & 0 deletions packages/vite/src/node/__tests__/asset.spec.ts
@@ -1,3 +1,4 @@
import { describe, test, expect } from 'vitest'
import { assetFileNamesToFileName, getAssetHash } from '../plugins/asset'

describe('getAssetHash', () => {
Expand Down
1 change: 1 addition & 0 deletions packages/vite/src/node/__tests__/build.spec.ts
@@ -1,6 +1,7 @@
import type { LibraryFormats, LibraryOptions } from '../build'
import { resolveLibFilename } from '../build'
import { resolve } from 'path'
import { describe, test, expect } from 'vitest'

type FormatsToFileNames = [LibraryFormats, string][]
const baseLibOptions: LibraryOptions = {
Expand Down

0 comments on commit 556af65

Please sign in to comment.