纸飞机的信笺
博客Awesome开源Demos制品库样板与配置

系统

command常用命令行
vscodeVSCode

通用

licenseLICENSE 许可
editorconfigEditorConfig
gitGit
typescriptTypeScript

Agent

claudeClaude Code
codexCodex

开发配置

nextjsNext.js
viteVite
vitestVitest
postcssPostCSS
rollupRollup
eslintESLint
prettierPrettier
tailwindcssTailwindCSS

包管理

npmnpm
nodejspackage.json

代码质量

huskyHusky
commitizenCommitizen
commitlintCommitLint
lintstagedLintStaged

传统

webpackWebpack
babelBabel
craCreateReactApp

杂项

monorepoMonorepo

vitestVitest

基础配置模拟 HTTP 服务器Node.js 服务端浏览器

Vitest 支持浏览器测试。不过需要配置,默认情况测试是跑在 Node.js 的。

依赖项:

npm add -D @vitest/browser-playwright

配置如下:

import { playwright } from '@vitest/browser-playwright'
import { defineConfig } from 'vitest/config'
 
export default defineConfig({
  test: {
    name: '浏览器测试',
    browser: {
      provider: playwright(),
      instances: [{ browser: 'chromium' }],
    },
  },
})

这里使用 playwright 运行 Chromium 浏览器。

如果要将测试运行在 CI/CD 环境,需要开启浏览器的无头模式:

import { playwright } from '@vitest/browser-playwright'
import { defineConfig } from 'vitest/config'
 
export default defineConfig({
  test: {
    name: '浏览器测试',
    browser: {
      provider: playwright(),
      instances: [{ browser: 'chromium' }],
      // ↓ 新增
      headless: true,
      ui: false,
      // ↓ 失败时是否截图,默认开启,可以关掉
      screenshotFailures: false,
    },
  },
})

如果是 npm 包的测试,可能要同时在 Node.js 和浏览器中运行测试,此时需要作出区分。

配置如下:

import { playwright } from '@vitest/browser-playwright'
import { defineConfig } from 'vitest/config'
 
export default defineConfig({
  test: {
    projects: [
      {
        extends: true,
        test: {
          include: ['tests/**/*.test.ts'],
          exclude: ['tests/**/*.browser.test.ts'],
          name: 'Node.js 测试',
          environment: 'node',
        },
      },
      {
        extends: true,
        test: {
          include: ['tests/**/*.test.ts'],
          exclude: ['tests/**/*.node.test.ts'],
          name: '浏览器测试',
          browser: {
            provider: playwright(),
            instances: [{ browser: 'chromium' }],
            headless: true,
            ui: false,
            screenshotFailures: false,
          },
        },
      },
    ],
  },
})

这是一段根据文件名区分的测试配置,对于默认的测试文件,会在浏览器和 Node.js 上都运行测试; 而 *.node.test.ts 结尾的文件则只会在 Node.js 上运行测试,*.browser.test.ts 结尾的文件只会在浏览器上运行测试。