From 639ed988c27f2d3b5532083d703181a337c3d11f Mon Sep 17 00:00:00 2001 From: fi3ework Date: Sun, 28 Jul 2024 23:50:17 +0800 Subject: [PATCH] test: add test setup --- .github/workflows/ci.yaml | 4 ++ package.json | 6 ++- packages/builder-rsbuild/tests/dummy.test.ts | 32 ++++++++++++ packages/builder-rsbuild/tsconfig.json | 2 +- pnpm-lock.yaml | 14 +++-- vitest-setup.ts | 55 ++++++++++++++++++++ vitest.workspace.ts | 31 +++++++++++ 7 files changed, 138 insertions(+), 6 deletions(-) create mode 100644 packages/builder-rsbuild/tests/dummy.test.ts create mode 100644 vitest-setup.ts create mode 100644 vitest.workspace.ts diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index be36b2c..27173d9 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -49,6 +49,10 @@ jobs: pnpm lint pnpm check + - name: Test + run: | + pnpm test + - name: Build sandboxes storybooks run: | pnpm run build:sandboxes diff --git a/package.json b/package.json index 78737cb..b1ada4a 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,9 @@ "build:sandboxes": "cross-env NX_DAEMON=false nx run-many -t build:storybook --projects @sandboxes/* --parallel=10", "check": "pnpm --parallel --filter \"./packages/**\" run check", "dev": "pnpm --parallel --filter \"./packages/**\" run prep --watch", - "lint": "biome check" + "lint": "biome check", + "test": "NODE_OPTIONS=--max_old_space_size=4096 vitest run", + "test:watch": "NODE_OPTIONS=--max_old_space_size=4096 vitest watch" }, "simple-git-hooks": { "pre-commit": "npx nano-staged" @@ -20,11 +22,13 @@ "devDependencies": { "@biomejs/biome": "^1.8.3", "@changesets/cli": "^2.27.1", + "@testing-library/jest-dom": "6.4.5", "cross-env": "^7.0.3", "nano-staged": "^0.8.0", "nx": "^19.4.3", "simple-git-hooks": "^2.11.1", "sort-package-json": "^2.10.0", + "ts-dedent": "^2.0.0", "typescript": "^5.3.2", "vitest": "^1.6.0" }, diff --git a/packages/builder-rsbuild/tests/dummy.test.ts b/packages/builder-rsbuild/tests/dummy.test.ts new file mode 100644 index 0000000..dd05323 --- /dev/null +++ b/packages/builder-rsbuild/tests/dummy.test.ts @@ -0,0 +1,32 @@ +import type { Presets } from 'storybook/internal/types' +import { describe, expect } from 'vitest' +import type { RsbuildBuilderOptions } from '../src/preview/iframe-rsbuild.config' + +// @ts-expect-error TODO +const dummyOptions: RsbuildBuilderOptions = { + configType: 'DEVELOPMENT', + configDir: '', + packageJson: {}, + presets: { + apply: async (key: string) => + ({ + framework: { + name: '', + }, + addons: [], + core: { + builder: {}, + }, + options: {}, + })[key], + } as Presets, + presetsList: [], + typescriptOptions: { + check: false, + skipCompiler: true, + }, +} + +describe('dummy', () => { + expect(1).toBe(1) +}) diff --git a/packages/builder-rsbuild/tsconfig.json b/packages/builder-rsbuild/tsconfig.json index 5b3f3a5..16f3e83 100644 --- a/packages/builder-rsbuild/tsconfig.json +++ b/packages/builder-rsbuild/tsconfig.json @@ -4,5 +4,5 @@ "strict": true, "skipLibCheck": true }, - "include": ["src/**/*"] + "include": ["src/**/*", "tests/**/*"] } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 6f715f7..b10c3ea 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -14,6 +14,9 @@ importers: '@changesets/cli': specifier: ^2.27.1 version: 2.27.1 + '@testing-library/jest-dom': + specifier: 6.4.5 + version: 6.4.5(vitest@1.6.0(@types/node@18.19.33)(terser@5.31.0)) cross-env: specifier: ^7.0.3 version: 7.0.3 @@ -29,6 +32,9 @@ importers: sort-package-json: specifier: ^2.10.0 version: 2.10.0 + ts-dedent: + specifier: ^2.0.0 + version: 2.2.0 typescript: specifier: ^5.3.2 version: 5.4.5 @@ -6013,8 +6019,8 @@ packages: resolution: {integrity: sha512-Dhxzh5HZuiHQhbvTW9AMetFfBHDMYpo23Uo9btPXgdYP+3T5S+p+jgNy7spra+veYhBP2dCSgxR/i2Y02h5/6w==} engines: {node: '>=0.10.0'} - vue-component-type-helpers@2.0.26: - resolution: {integrity: sha512-sO9qQ8oC520SW6kqlls0iqDak53gsTVSrYylajgjmkt1c0vcgjsGSy1KzlDrbEx8pm02IEYhlUkU5hCYf8rwtg==} + vue-component-type-helpers@2.0.29: + resolution: {integrity: sha512-58i+ZhUAUpwQ+9h5Hck0D+jr1qbYl4voRt5KffBx8qzELViQ4XdT/Tuo+mzq8u63teAG8K0lLaOiL5ofqW38rg==} vue-docgen-api@4.78.0: resolution: {integrity: sha512-RsZf+qzTttCCAN9v7AKmBykc2QWmO8csVk1c2aXeOktomSOu0NA7sgK4ObuRB5lpmtOvTnwuxssyYmxXxABr+A==} @@ -7988,7 +7994,7 @@ snapshots: ts-dedent: 2.2.0 type-fest: 2.19.0 vue: 3.4.27(typescript@5.4.5) - vue-component-type-helpers: 2.0.26 + vue-component-type-helpers: 2.0.29 '@swc/helpers@0.5.11': dependencies: @@ -12857,7 +12863,7 @@ snapshots: void-elements@3.1.0: {} - vue-component-type-helpers@2.0.26: {} + vue-component-type-helpers@2.0.29: {} vue-docgen-api@4.78.0(vue@3.4.27(typescript@5.4.5)): dependencies: diff --git a/vitest-setup.ts b/vitest-setup.ts new file mode 100644 index 0000000..3dcc6a3 --- /dev/null +++ b/vitest-setup.ts @@ -0,0 +1,55 @@ +// Code taken from https://github.com/storybookjs/storybook/blob/next/code/vitest-setup.ts. +import '@testing-library/jest-dom/vitest' +import { expect, vi } from 'vitest' + +import { dedent } from 'ts-dedent' + +const ignoreList = [ + (error: any) => error.message.includes('":nth-child" is potentially unsafe'), + (error: any) => + error.message.includes('":first-child" is potentially unsafe'), + (error: any) => + error.message.match(/Browserslist: .* is outdated. Please run:/), + (error: any) => + error.message.includes('react-async-component-lifecycle-hooks') && + error.stack.includes('addons/knobs/src/components/__tests__/Options.js'), + // React will log this error even if you catch an error with a boundary. I guess it's to + // help in development. See https://github.com/facebook/react/issues/15069 + (error: any) => + error.message.match( + /React will try to recreate this component tree from scratch using the error boundary you provided/, + ), + (error: any) => + error.message.includes( + 'Lit is in dev mode. Not recommended for production!', + ), +] + +const throwMessage = (type: any, message: any) => { + // eslint-disable-next-line local-rules/no-uncategorized-errors + const error = new Error(`${type}${message}`) + if (!ignoreList.reduce((acc, item) => acc || item(error), false)) { + throw error + } +} +const throwWarning = (message: any) => throwMessage('warn: ', message) +const throwError = (message: any) => throwMessage('error: ', message) + +vi.spyOn(console, 'warn').mockImplementation(throwWarning) +vi.spyOn(console, 'error').mockImplementation(throwError) + +expect.extend({ + toMatchPaths(regex: RegExp, paths: string[]) { + const matched = paths.map((p) => !!p.match(regex)) + + const pass = matched.every(Boolean) + const failures = paths.filter((_, i) => (pass ? matched[i] : !matched[i])) + const message = () => dedent`Expected ${regex} to ${pass ? 'not ' : ''}match all strings. + + Failures:${['', ...failures].join('\n - ')}` + return { + pass, + message, + } + }, +}) diff --git a/vitest.workspace.ts b/vitest.workspace.ts new file mode 100644 index 0000000..8d78f44 --- /dev/null +++ b/vitest.workspace.ts @@ -0,0 +1,31 @@ +import { resolve } from 'node:path' +import { defineConfig, defineWorkspace } from 'vitest/config' + +export default defineWorkspace(['packages/*/vitest.config.ts']) + +/** + * CircleCI reports the wrong number of threads to Node.js, so we need to set it manually. + * Unit tests are running with the xlarge resource class, which has 8 vCPUs. + * @see https://jahed.dev/2022/11/20/fixing-node-js-multi-threading-on-circleci/ + * @see https://vitest.dev/config/#pooloptions-threads-maxthreads + * @see https://circleci.com/docs/configuration-reference/#x86 + * @see .circleci/config.yml#L214 + */ +const threadCount = process.env.CI ? 8 : undefined + +export const vitestCommonConfig = defineConfig({ + test: { + passWithNoTests: true, + clearMocks: true, + setupFiles: [resolve(__dirname, './vitest-setup.ts')], + globals: true, + testTimeout: 10000, + environment: 'node', + poolOptions: { + threads: { + minThreads: threadCount, + maxThreads: threadCount, + }, + }, + }, +})