@wtf/project-vitest
v3.0.1
Published
The shared Vitest configs for a pnpm workspace: tests from source or, with TEST_FROM=dist, from the build, with coverage for either (./config); the environment for a process a test starts (./process); the configs for browser tests (./browser); and the con
Readme
@wtf/project-vitest
The shared Vitest configs for a pnpm workspace. They run every package's tests from the root, from
source with nothing built first, or from the build (TEST_FROM=dist), with coverage for either.
Vitest ^5.0.5 is a peer dependency. @vitest/coverage-v8 and @vitest/browser-playwright, at
the same version, and playwright are optional peers, for coverage and for browser tests. A repo
names each at its root by the one exact version, 5.0.5, since the configs run in the Vitest that
runs them. Vite comes from @wtf/project-vite, a dependency.
| Subpath | What you get |
| ------------- | ------------------------------------------------------------------------------------------------------------------------------------ |
| ./config | rootConfig(), packageConfig(), integrationConfig(), integration, fromDist(env), GUARD_TIMEOUT, the type VitestPlatform |
| ./process | childEnv(env): the environment for a process a test starts, without the resolve hook |
| ./browser | browserConfig() and browserRootConfig(): browser tests, in Chromium |
| ./storybook | storybookConfig(): for a package whose stories are its tests, each in Chromium |
| ./schema | vitestSchema: the Standard Schema of the options of rootConfig(), which gives their JSON Schema |
// vitest.config.ts, at the root
import { nodeStorage } from '@wtf/platform-fs-node/storage'
import { nodePlatform } from '@wtf/platform-node/platform'
import { rootConfig } from '@wtf/project-vitest/config'
const platform = nodePlatform({ fetch, storage: nodeStorage() })
export default rootConfig({ platform })// packages/core/vitest.config.ts
import { nodeStorage } from '@wtf/platform-fs-node/storage'
import { nodePlatform } from '@wtf/platform-node/platform'
import { packageConfig } from '@wtf/project-vitest/config'
export default packageConfig({ platform: nodePlatform({ fetch, storage: nodeStorage() }) })Each config file is the entry point: it builds Node's platform and gives it to the config, which
reads nothing of the process itself. The config reads CI and TEST_FROM of the platform's env,
the workspace of its dirs.cwd, by its navigator.storage and path; the root's reads the
arguments of the process of its argv too, whose --project filters its coverage reads.
packageConfig({ platform, ...test }, config) takes the platform and the test options, and a Vite
config to merge, such as { resolve: { dedupe: ['react'] } }.
The guards, in the root's root project, get 30 seconds per test, GUARD_TIMEOUT, the same as a
package's tests, instead of Vitest's 5. Pass another with
rootConfig({ platform, testTimeout: 60_000 }).
When tests run from source, Vite resolves each workspace package through an alias from each export
to its @wtf/source, and each test worker registers @wtf/project-resolve's resolve hook, in
Node's environment or in jsdom's. See the tests guide.
A package whose stories are its tests runs them with storybookConfig({ platform, storybook }) from
./storybook. It is a browserConfig() named storybook, whose tests are the stories that
@storybook/addon-vitest's plugin, storybookTest(), finds in the package's .storybook. The repo
installs the addon at its Storybook's version; nothing here depends on Storybook.
From the wtf configuration
The configs read no file of their own. Keep the options of rootConfig() in the vitest section
of the repo's wtf configuration, load it with
@wtf/project-config, and spread the section in, checked by
vitestSchema; what you write in code after it wins:
// vitest.config.ts, at the root
import { nodeStorage } from '@wtf/platform-fs-node/storage'
import { nodePlatform } from '@wtf/platform-node/platform'
import { loadConfig } from '@wtf/project-config/load'
import { sectionOf } from '@wtf/project-config/section'
import { rootConfig } from '@wtf/project-vitest/config'
import { vitestSchema } from '@wtf/project-vitest/schema'
const platform = nodePlatform({ fetch, storage: nodeStorage() })
const config = await loadConfig({ platform })
export default rootConfig({
platform,
...(await sectionOf(config, 'vitest', vitestSchema))
}){ "vitest": { "skip": ["packages/site"], "testTimeout": 60000 } }A key that starts with // is a comment, as "//": "why": vitestSchema takes it, in the section
and in an object of options in it, and leaves it out of the options it gives.
integrationConfig() and browserRootConfig() take the same options but testTimeout.
