@cbrl/testing 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +103 -2
- package/dist/a11y.d.mts +1 -0
- package/dist/a11y.mjs +22 -0
- package/dist/playwright.d.mts +86 -0
- package/dist/playwright.mjs +112 -0
- package/dist/vitest/jsdom.d.mts +17 -0
- package/dist/vitest/jsdom.mjs +46 -0
- package/dist/vitest/server-only-shim.d.mts +1 -0
- package/dist/vitest/server-only-shim.mjs +1 -0
- package/dist/vitest.d.mts +7 -0
- package/dist/vitest.mjs +16 -0
- package/package.json +75 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Kayron Cabral
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,104 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @cbrl/testing
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Test helpers for Vitest and Playwright.
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
pnpm add -D @cbrl/testing vitest @vitest/coverage-v8
|
|
7
|
+
# Playwright presets (optional peer):
|
|
8
|
+
pnpm add -D @playwright/test
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
| Entry | Export |
|
|
12
|
+
| --------------------------------------- | --------------------------------------------------------------------------------------------------------------- |
|
|
13
|
+
| `@cbrl/testing/vitest` | `coverageBase()`: v8 provider, `src/**` include, test files excluded, `text-summary` + `json-summary` reporters |
|
|
14
|
+
| `@cbrl/testing/vitest/server-only-shim` | Empty module to alias `server-only` to in tests |
|
|
15
|
+
| `@cbrl/testing/vitest/jsdom` | `installJsdomPolyfills()`: stubs `ResizeObserver`, `matchMedia`, `scrollIntoView` and Pointer Capture |
|
|
16
|
+
| `@cbrl/testing/a11y` | `expectNoA11yViolations(container)`: axe-core check for jsdom component tests (page-level `region` rule off) |
|
|
17
|
+
| `@cbrl/testing/playwright` | `defineE2EConfig()`, `defineSmokeConfig()` and `smokeSuite()` presets |
|
|
18
|
+
|
|
19
|
+
## Vitest
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
// vitest.config.ts
|
|
23
|
+
import { coverageBase } from '@cbrl/testing/vitest'
|
|
24
|
+
import { defineConfig } from 'vitest/config'
|
|
25
|
+
|
|
26
|
+
export default defineConfig({
|
|
27
|
+
test: {
|
|
28
|
+
coverage: {
|
|
29
|
+
...coverageBase(),
|
|
30
|
+
// Keep thresholds inline so `autoUpdate` can ratchet them in this file.
|
|
31
|
+
thresholds: { statements: 90, branches: 85, functions: 90, lines: 90, autoUpdate: true },
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
})
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Alias `server-only` in tests:
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
import { fileURLToPath } from 'node:url'
|
|
41
|
+
|
|
42
|
+
resolve: {
|
|
43
|
+
alias: {
|
|
44
|
+
'server-only': fileURLToPath(import.meta.resolve('@cbrl/testing/vitest/server-only-shim')),
|
|
45
|
+
},
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Polyfill the browser APIs Radix primitives need under jsdom:
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
// vitest.setup.ts
|
|
53
|
+
import '@testing-library/jest-dom/vitest'
|
|
54
|
+
import { installJsdomPolyfills } from '@cbrl/testing/vitest/jsdom'
|
|
55
|
+
|
|
56
|
+
installJsdomPolyfills()
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Playwright
|
|
60
|
+
|
|
61
|
+
`defineE2EConfig` boots the app and runs chromium. On CI (`CI` set) it serves a
|
|
62
|
+
production build, never reuses a running server, forbids `.only` and reports
|
|
63
|
+
with GitHub annotations; locally it runs the dev server. Spread it to add app
|
|
64
|
+
specifics:
|
|
65
|
+
|
|
66
|
+
```ts
|
|
67
|
+
// playwright.config.ts
|
|
68
|
+
import { defineE2EConfig } from '@cbrl/testing/playwright'
|
|
69
|
+
import { defineConfig } from '@playwright/test'
|
|
70
|
+
|
|
71
|
+
export default defineConfig({
|
|
72
|
+
...defineE2EConfig({
|
|
73
|
+
port: 3000,
|
|
74
|
+
testDir: './src',
|
|
75
|
+
testMatch: '**/_e2e/**/*.spec.ts',
|
|
76
|
+
webServerEnv: { ENABLE_TEST_AUTH: 'true' },
|
|
77
|
+
// devCommand: 'pnpm run dev', ciCommand: 'pnpm run build && pnpm run start -p 3000'
|
|
78
|
+
}),
|
|
79
|
+
globalSetup: './e2e/global-setup.ts',
|
|
80
|
+
})
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
Post-deploy smoke tests against a deployed URL (fails fast when the env var is
|
|
84
|
+
missing):
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
// playwright.smoke.config.ts
|
|
88
|
+
import { defineSmokeConfig } from '@cbrl/testing/playwright'
|
|
89
|
+
|
|
90
|
+
export default defineSmokeConfig() // { testDir: './_smoke', baseUrlEnv: 'SMOKE_BASE_URL' }
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
```ts
|
|
94
|
+
// _smoke/health.spec.ts
|
|
95
|
+
import { smokeSuite } from '@cbrl/testing/playwright'
|
|
96
|
+
import { expect, test } from '@playwright/test'
|
|
97
|
+
|
|
98
|
+
// GET /api/health -> 200 { status: 'ok' }, and `/` renders with a title.
|
|
99
|
+
smokeSuite({ healthPath: '/api/health' })
|
|
100
|
+
|
|
101
|
+
test('listing renders', async ({ page }) => {
|
|
102
|
+
expect((await page.goto('/listing'))?.status()).toBeLessThan(400)
|
|
103
|
+
})
|
|
104
|
+
```
|
package/dist/a11y.d.mts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function expectNoA11yViolations(container: Element): Promise<void>;
|
package/dist/a11y.mjs
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { axe, toHaveNoViolations } from "jest-axe";
|
|
2
|
+
import { expect } from "vitest";
|
|
3
|
+
// src/vitest/a11y.ts
|
|
4
|
+
/**
|
|
5
|
+
* Shared accessibility assertion for jsdom component tests, backed by axe-core
|
|
6
|
+
* (via jest-axe). Design-system components are exactly where automated a11y
|
|
7
|
+
* checks pay off — a violation here propagates to every consuming app.
|
|
8
|
+
*
|
|
9
|
+
* import { expectNoA11yViolations } from '@cbrl/testing/a11y'
|
|
10
|
+
* const { container } = render(<Button>Salvar</Button>)
|
|
11
|
+
* await expectNoA11yViolations(container)
|
|
12
|
+
*
|
|
13
|
+
* The jest-axe matcher is registered once here so callers only deal with the
|
|
14
|
+
* wrapper — no per-file `expect.extend` and no matcher type augmentation.
|
|
15
|
+
* Requires a DOM environment (`environment: 'jsdom'`).
|
|
16
|
+
*/
|
|
17
|
+
expect.extend(toHaveNoViolations);
|
|
18
|
+
async function expectNoA11yViolations(container) {
|
|
19
|
+
const results = await axe(container, { rules: { region: { enabled: false } } });
|
|
20
|
+
expect(results).toHaveNoViolations();
|
|
21
|
+
}
|
|
22
|
+
export { expectNoA11yViolations };
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { PlaywrightTestConfig } from "@playwright/test";
|
|
2
|
+
/** Options for {@link defineE2EConfig}. */
|
|
3
|
+
interface E2EConfigOptions {
|
|
4
|
+
/** Port the app server listens on. The base URL is derived from it. */
|
|
5
|
+
readonly port: number;
|
|
6
|
+
/** Directory Playwright searches for specs, relative to the config file. */
|
|
7
|
+
readonly testDir: string;
|
|
8
|
+
/** Spec glob(s) inside `testDir`. Default `'**\/*.spec.ts'`. */
|
|
9
|
+
readonly testMatch?: string | RegExp | readonly (string | RegExp)[];
|
|
10
|
+
/** Env passed to the app server (test-only flags, local service keys…). */
|
|
11
|
+
readonly webServerEnv?: Readonly<Record<string, string>>;
|
|
12
|
+
/** Server command outside CI. Default `'pnpm run dev'`. */
|
|
13
|
+
readonly devCommand?: string;
|
|
14
|
+
/**
|
|
15
|
+
* Server command on CI: a production build, so specs never race on-demand
|
|
16
|
+
* compilation. Default `` `pnpm run build && pnpm run start -p ${port}` ``.
|
|
17
|
+
*/
|
|
18
|
+
readonly ciCommand?: string;
|
|
19
|
+
/** Host the base URL points at. Default `'127.0.0.1'`. */
|
|
20
|
+
readonly host?: string;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Base Playwright config for an E2E suite that boots the app itself.
|
|
24
|
+
*
|
|
25
|
+
* On CI (`process.env.CI` set) it runs the production build, never reuses a
|
|
26
|
+
* running server, forbids `.only` and reports with GitHub annotations; locally
|
|
27
|
+
* it runs the dev server and reuses one already up. Always chromium, no
|
|
28
|
+
* retries, traces kept for failing tests. Spread the result into Playwright's
|
|
29
|
+
* `defineConfig` to add app specifics (`globalSetup`, `workers`, `shard`…).
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* import { defineE2EConfig } from '@cbrl/testing/playwright'
|
|
34
|
+
* import { defineConfig } from '@playwright/test'
|
|
35
|
+
*
|
|
36
|
+
* export default defineConfig({
|
|
37
|
+
* ...defineE2EConfig({ port: 3000, testDir: './src', testMatch: '**\/_e2e/**\/*.spec.ts' }),
|
|
38
|
+
* workers: 2,
|
|
39
|
+
* })
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
export declare function defineE2EConfig(options: E2EConfigOptions): PlaywrightTestConfig;
|
|
43
|
+
/** Options for {@link defineSmokeConfig}. */
|
|
44
|
+
interface SmokeConfigOptions {
|
|
45
|
+
/** Directory with the smoke specs. Default `'./_smoke'`. */
|
|
46
|
+
readonly testDir?: string;
|
|
47
|
+
/** Env var holding the deployed URL to smoke-test. Default `'SMOKE_BASE_URL'`. */
|
|
48
|
+
readonly baseUrlEnv?: string;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Playwright config for post-deploy smoke tests against a deployed URL. Starts
|
|
52
|
+
* no server; fails fast when the URL env var is missing. Retries twice (a
|
|
53
|
+
* fresh deployment can be cold) and records a trace on the first retry.
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```ts
|
|
57
|
+
* // playwright.smoke.config.ts
|
|
58
|
+
* import { defineSmokeConfig } from '@cbrl/testing/playwright'
|
|
59
|
+
*
|
|
60
|
+
* export default defineSmokeConfig()
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
export declare function defineSmokeConfig(options?: SmokeConfigOptions): PlaywrightTestConfig;
|
|
64
|
+
/** Options for {@link smokeSuite}. */
|
|
65
|
+
interface SmokeSuiteOptions {
|
|
66
|
+
/** Health endpoint that answers `200` with `{ status: 'ok' }`. Default `'/api/health'`. */
|
|
67
|
+
readonly healthPath?: string;
|
|
68
|
+
/** Page that must render with a non-empty title. Default `'/'`. */
|
|
69
|
+
readonly homePath?: string;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Register the baseline smoke tests in the calling spec file: the health
|
|
73
|
+
* endpoint returns `200` with `status: 'ok'`, and the home page renders
|
|
74
|
+
* (status below 400) with a non-empty `<title>`. Add app-specific smoke tests
|
|
75
|
+
* next to the call.
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```ts
|
|
79
|
+
* // _smoke/health.spec.ts
|
|
80
|
+
* import { smokeSuite } from '@cbrl/testing/playwright'
|
|
81
|
+
*
|
|
82
|
+
* smokeSuite()
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
export declare function smokeSuite(options?: SmokeSuiteOptions): void;
|
|
86
|
+
export type { E2EConfigOptions, SmokeConfigOptions, SmokeSuiteOptions };
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { requireEnv } from "@cbrl/env";
|
|
2
|
+
import { devices, expect, test } from "@playwright/test";
|
|
3
|
+
// src/playwright/config.ts
|
|
4
|
+
const chromium = {
|
|
5
|
+
name: "chromium",
|
|
6
|
+
use: { ...devices["Desktop Chrome"] }
|
|
7
|
+
};
|
|
8
|
+
const isCI = () => Boolean(process.env.CI);
|
|
9
|
+
/**
|
|
10
|
+
* Base Playwright config for an E2E suite that boots the app itself.
|
|
11
|
+
*
|
|
12
|
+
* On CI (`process.env.CI` set) it runs the production build, never reuses a
|
|
13
|
+
* running server, forbids `.only` and reports with GitHub annotations; locally
|
|
14
|
+
* it runs the dev server and reuses one already up. Always chromium, no
|
|
15
|
+
* retries, traces kept for failing tests. Spread the result into Playwright's
|
|
16
|
+
* `defineConfig` to add app specifics (`globalSetup`, `workers`, `shard`…).
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* import { defineE2EConfig } from '@cbrl/testing/playwright'
|
|
21
|
+
* import { defineConfig } from '@playwright/test'
|
|
22
|
+
*
|
|
23
|
+
* export default defineConfig({
|
|
24
|
+
* ...defineE2EConfig({ port: 3000, testDir: './src', testMatch: '**\/_e2e/**\/*.spec.ts' }),
|
|
25
|
+
* workers: 2,
|
|
26
|
+
* })
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
function defineE2EConfig(options) {
|
|
30
|
+
const { port, testDir, testMatch = "**/*.spec.ts", webServerEnv = {}, devCommand = "pnpm run dev", ciCommand = `pnpm run build && pnpm run start -p ${port}`, host = "127.0.0.1" } = options;
|
|
31
|
+
const ci = isCI();
|
|
32
|
+
const baseURL = `http://${host}:${port}`;
|
|
33
|
+
return {
|
|
34
|
+
testDir,
|
|
35
|
+
testMatch: typeof testMatch === "string" || testMatch instanceof RegExp ? testMatch : [...testMatch],
|
|
36
|
+
fullyParallel: true,
|
|
37
|
+
forbidOnly: ci,
|
|
38
|
+
retries: 0,
|
|
39
|
+
reporter: ci ? "github" : "list",
|
|
40
|
+
use: {
|
|
41
|
+
baseURL,
|
|
42
|
+
trace: "retain-on-failure"
|
|
43
|
+
},
|
|
44
|
+
projects: [chromium],
|
|
45
|
+
webServer: {
|
|
46
|
+
command: ci ? ciCommand : devCommand,
|
|
47
|
+
url: baseURL,
|
|
48
|
+
reuseExistingServer: !ci,
|
|
49
|
+
timeout: ci ? 3e5 : 12e4,
|
|
50
|
+
env: { ...webServerEnv }
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Playwright config for post-deploy smoke tests against a deployed URL. Starts
|
|
56
|
+
* no server; fails fast when the URL env var is missing. Retries twice (a
|
|
57
|
+
* fresh deployment can be cold) and records a trace on the first retry.
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```ts
|
|
61
|
+
* // playwright.smoke.config.ts
|
|
62
|
+
* import { defineSmokeConfig } from '@cbrl/testing/playwright'
|
|
63
|
+
*
|
|
64
|
+
* export default defineSmokeConfig()
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
function defineSmokeConfig(options = {}) {
|
|
68
|
+
const { testDir = "./_smoke", baseUrlEnv = "SMOKE_BASE_URL" } = options;
|
|
69
|
+
const baseURL = requireEnv(baseUrlEnv);
|
|
70
|
+
return {
|
|
71
|
+
testDir,
|
|
72
|
+
testMatch: "**/*.spec.ts",
|
|
73
|
+
fullyParallel: true,
|
|
74
|
+
forbidOnly: isCI(),
|
|
75
|
+
retries: 2,
|
|
76
|
+
reporter: "html",
|
|
77
|
+
use: {
|
|
78
|
+
baseURL,
|
|
79
|
+
trace: "on-first-retry"
|
|
80
|
+
},
|
|
81
|
+
projects: [chromium]
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
// src/playwright/smoke.ts
|
|
85
|
+
/**
|
|
86
|
+
* Register the baseline smoke tests in the calling spec file: the health
|
|
87
|
+
* endpoint returns `200` with `status: 'ok'`, and the home page renders
|
|
88
|
+
* (status below 400) with a non-empty `<title>`. Add app-specific smoke tests
|
|
89
|
+
* next to the call.
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* ```ts
|
|
93
|
+
* // _smoke/health.spec.ts
|
|
94
|
+
* import { smokeSuite } from '@cbrl/testing/playwright'
|
|
95
|
+
*
|
|
96
|
+
* smokeSuite()
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
function smokeSuite(options = {}) {
|
|
100
|
+
const { healthPath = "/api/health", homePath = "/" } = options;
|
|
101
|
+
test("health endpoint returns ok", async ({ request }) => {
|
|
102
|
+
const res = await request.get(healthPath);
|
|
103
|
+
expect(res.status()).toBe(200);
|
|
104
|
+
expect(await res.json()).toMatchObject({ status: "ok" });
|
|
105
|
+
});
|
|
106
|
+
test("home page renders", async ({ page }) => {
|
|
107
|
+
const res = await page.goto(homePath);
|
|
108
|
+
expect(res?.status()).toBeLessThan(400);
|
|
109
|
+
await expect(page).toHaveTitle(/.+/);
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
export { defineE2EConfig, defineSmokeConfig, smokeSuite };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stub the browser APIs jsdom lacks but Radix primitives, sonner and
|
|
3
|
+
* next-themes call: `ResizeObserver`, `matchMedia` (never matches),
|
|
4
|
+
* `Element#scrollIntoView` and the Pointer Capture API. Only missing APIs are
|
|
5
|
+
* defined, so a real implementation or a test's own mock wins. Call it once
|
|
6
|
+
* from the vitest `setupFiles` of a jsdom project.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* // vitest.setup.ts
|
|
11
|
+
* import '@testing-library/jest-dom/vitest'
|
|
12
|
+
* import { installJsdomPolyfills } from '@cbrl/testing/vitest/jsdom'
|
|
13
|
+
*
|
|
14
|
+
* installJsdomPolyfills()
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export declare function installJsdomPolyfills(): void;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// src/vitest/jsdom.ts
|
|
2
|
+
const noop = () => {};
|
|
3
|
+
var ResizeObserverStub = class {
|
|
4
|
+
observe() {}
|
|
5
|
+
unobserve() {}
|
|
6
|
+
disconnect() {}
|
|
7
|
+
};
|
|
8
|
+
function matchMediaStub(query) {
|
|
9
|
+
return {
|
|
10
|
+
matches: false,
|
|
11
|
+
media: query,
|
|
12
|
+
onchange: null,
|
|
13
|
+
addEventListener: noop,
|
|
14
|
+
removeEventListener: noop,
|
|
15
|
+
addListener: noop,
|
|
16
|
+
removeListener: noop,
|
|
17
|
+
dispatchEvent: () => false
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Stub the browser APIs jsdom lacks but Radix primitives, sonner and
|
|
22
|
+
* next-themes call: `ResizeObserver`, `matchMedia` (never matches),
|
|
23
|
+
* `Element#scrollIntoView` and the Pointer Capture API. Only missing APIs are
|
|
24
|
+
* defined, so a real implementation or a test's own mock wins. Call it once
|
|
25
|
+
* from the vitest `setupFiles` of a jsdom project.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```ts
|
|
29
|
+
* // vitest.setup.ts
|
|
30
|
+
* import '@testing-library/jest-dom/vitest'
|
|
31
|
+
* import { installJsdomPolyfills } from '@cbrl/testing/vitest/jsdom'
|
|
32
|
+
*
|
|
33
|
+
* installJsdomPolyfills()
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
function installJsdomPolyfills() {
|
|
37
|
+
if (typeof globalThis.ResizeObserver === "undefined") globalThis.ResizeObserver = ResizeObserverStub;
|
|
38
|
+
if (typeof globalThis.matchMedia !== "function") globalThis.matchMedia = matchMediaStub;
|
|
39
|
+
if (typeof Element === "undefined") return;
|
|
40
|
+
const proto = Element.prototype;
|
|
41
|
+
if (typeof proto.scrollIntoView !== "function") proto.scrollIntoView = noop;
|
|
42
|
+
if (typeof proto.hasPointerCapture !== "function") proto.hasPointerCapture = () => false;
|
|
43
|
+
if (typeof proto.setPointerCapture !== "function") proto.setPointerCapture = noop;
|
|
44
|
+
if (typeof proto.releasePointerCapture !== "function") proto.releasePointerCapture = noop;
|
|
45
|
+
}
|
|
46
|
+
export { installJsdomPolyfills };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/vitest.mjs
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// src/vitest/index.ts
|
|
2
|
+
function coverageBase() {
|
|
3
|
+
return {
|
|
4
|
+
provider: "v8",
|
|
5
|
+
include: ["src/**/*.{ts,tsx}"],
|
|
6
|
+
exclude: [
|
|
7
|
+
"src/**/*.{test,spec}.{ts,tsx}",
|
|
8
|
+
"src/**/*.integration.test.{ts,tsx}",
|
|
9
|
+
"src/**/*.d.ts",
|
|
10
|
+
"src/**/__mocks__/**"
|
|
11
|
+
],
|
|
12
|
+
reporter: ["text-summary", "json-summary"],
|
|
13
|
+
reportsDirectory: "./coverage"
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
export { coverageBase };
|
package/package.json
CHANGED
|
@@ -1,6 +1,78 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cbrl/testing",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Test helpers: Vitest coverage base, server-only shim, jsdom polyfills, jest-axe a11y matcher, and Playwright E2E/smoke presets.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Kayron Cabral",
|
|
7
|
+
"homepage": "https://github.com/kaycabral/toolkit/tree/main/packages/testing#readme",
|
|
8
|
+
"bugs": {
|
|
9
|
+
"url": "https://github.com/kaycabral/toolkit/issues"
|
|
10
|
+
},
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/kaycabral/toolkit.git",
|
|
14
|
+
"directory": "packages/testing"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"vitest",
|
|
18
|
+
"playwright",
|
|
19
|
+
"testing",
|
|
20
|
+
"coverage",
|
|
21
|
+
"a11y",
|
|
22
|
+
"jest-axe"
|
|
23
|
+
],
|
|
24
|
+
"type": "module",
|
|
25
|
+
"sideEffects": false,
|
|
26
|
+
"exports": {
|
|
27
|
+
"./a11y": "./dist/a11y.mjs",
|
|
28
|
+
"./playwright": "./dist/playwright.mjs",
|
|
29
|
+
"./vitest": "./dist/vitest.mjs",
|
|
30
|
+
"./vitest/jsdom": "./dist/vitest/jsdom.mjs",
|
|
31
|
+
"./vitest/server-only-shim": "./dist/vitest/server-only-shim.mjs",
|
|
32
|
+
"./package.json": "./package.json"
|
|
33
|
+
},
|
|
34
|
+
"files": [
|
|
35
|
+
"dist"
|
|
36
|
+
],
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"jest-axe": "^10.0.0",
|
|
39
|
+
"@cbrl/env": "^0.1.1"
|
|
40
|
+
},
|
|
41
|
+
"peerDependencies": {
|
|
42
|
+
"@playwright/test": ">=1.50",
|
|
43
|
+
"vitest": ">=3"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@arethetypeswrong/cli": "^0.18.5",
|
|
47
|
+
"@playwright/test": "^1.56.1",
|
|
48
|
+
"@types/jest-axe": "^3.5.9",
|
|
49
|
+
"@types/node": "^25.6.2",
|
|
50
|
+
"@vitest/coverage-v8": "^4.1.6",
|
|
51
|
+
"eslint": "^9.39.4",
|
|
52
|
+
"jsdom": "^29.1.1",
|
|
53
|
+
"publint": "^0.3.25",
|
|
54
|
+
"tsdown": "^0.23.7",
|
|
55
|
+
"typescript": "^5.9.3",
|
|
56
|
+
"vitest": "^4.1.6",
|
|
57
|
+
"@cbrl/eslint-config": "0.2.0",
|
|
58
|
+
"@cbrl/tsconfig": "0.1.0"
|
|
59
|
+
},
|
|
60
|
+
"publishConfig": {
|
|
61
|
+
"access": "public"
|
|
62
|
+
},
|
|
63
|
+
"peerDependenciesMeta": {
|
|
64
|
+
"@playwright/test": {
|
|
65
|
+
"optional": true
|
|
66
|
+
},
|
|
67
|
+
"vitest": {
|
|
68
|
+
"optional": true
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
"scripts": {
|
|
72
|
+
"build": "tsdown",
|
|
73
|
+
"typecheck": "tsc --noEmit",
|
|
74
|
+
"test": "vitest run --coverage",
|
|
75
|
+
"check:package": "node ../../scripts/check-package.mjs --types",
|
|
76
|
+
"lint": "eslint ."
|
|
77
|
+
}
|
|
6
78
|
}
|