Skip to content

Commit

Permalink
test: add test setup
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework committed Jul 28, 2024
1 parent 63c89bc commit 639ed98
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 6 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ jobs:
pnpm lint
pnpm check
- name: Test
run: |
pnpm test
- name: Build sandboxes storybooks
run: |
pnpm run build:sandboxes
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
},
Expand Down
32 changes: 32 additions & 0 deletions packages/builder-rsbuild/tests/dummy.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})
2 changes: 1 addition & 1 deletion packages/builder-rsbuild/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
"strict": true,
"skipLibCheck": true
},
"include": ["src/**/*"]
"include": ["src/**/*", "tests/**/*"]
}
14 changes: 10 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 55 additions & 0 deletions vitest-setup.ts
Original file line number Diff line number Diff line change
@@ -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,
}
},
})
31 changes: 31 additions & 0 deletions vitest.workspace.ts
Original file line number Diff line number Diff line change
@@ -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,
},
},
},
})

0 comments on commit 639ed98

Please sign in to comment.