|
1 | 1 | import { defineConfig, coverageConfigDefaults } from "vitest/config"; |
2 | 2 | import GithubActionsReporter from "vitest-github-actions-reporter"; |
3 | | -import type { EmptyObject } from "type-fest"; |
| 3 | + |
| 4 | +const isCI = !!process.env.CI; |
| 5 | +const isGitHubActionWorkflow = !!process.env.GITHUB_ACTIONS; |
4 | 6 |
|
5 | 7 | export default defineConfig({ |
6 | 8 | test: { |
7 | 9 | /** |
8 | 10 | * `restoreMocks` accomplishes the following: |
9 | | - * - clears all spies of `spy.mock.calls` and `spy.mock.results` (same as clearMocks:true) |
10 | | - * - removes any mocked implementations (same as mockReset:true) |
11 | | - * - restores the original implementation so fns don't return undefined like with mockReset |
| 11 | + * - Removes any mocked implementations (same as mockReset:true). |
| 12 | + * - Restores the original implementation so fns don't return undefined like with mockReset. |
| 13 | + * - Clears mock-state for spies created "manually" via `vi.spyOn` (same as clearMocks:true). |
| 14 | + * > Mock-state is NOT cleared for spies created via *automocking* (`mockReset` does this). |
| 15 | + * |
| 16 | + * `mockReset` accomplishes the following: |
| 17 | + * - Resets mock-state (call counts, arguments, etc.) for all mocks, including automocks. |
12 | 18 | */ |
13 | 19 | restoreMocks: true, |
| 20 | + mockReset: true, |
14 | 21 | unstubGlobals: true, |
15 | 22 | globals: true, |
16 | 23 | silent: true, |
17 | 24 | hideSkippedTests: true, |
18 | 25 | watch: false, |
19 | | - |
| 26 | + bail: isCI ? 1 : 0, // If in CI, bail on first test failure, else run all tests |
20 | 27 | environment: "node", |
21 | | - setupFiles: "./vitest.setup.ts", |
22 | | - |
23 | | - include: ["**/?(*.){test,spec}.?(c|m)[tj]s?(x)"], |
24 | | - |
| 28 | + setupFiles: ["./vitest.setup.ts"], |
25 | 29 | reporters: [ |
26 | 30 | "default", |
27 | 31 | // GithubActionsReporter is used to format test results for GitHub Actions |
28 | | - ...(process.env.GITHUB_ACTIONS ? [new GithubActionsReporter()] : []), |
| 32 | + ...(isGitHubActionWorkflow ? [new GithubActionsReporter()] : []), |
29 | 33 | ], |
30 | | - |
31 | 34 | coverage: { |
32 | 35 | include: ["src/**/*.{js,ts}"], |
33 | | - exclude: [ |
34 | | - ...coverageConfigDefaults.exclude, |
35 | | - "**/tests/**/*", |
36 | | - "**/__mocks__/**/*", |
37 | | - "__mocks__/**/*", |
38 | | - ], |
| 36 | + exclude: [...coverageConfigDefaults.exclude, "**/index.ts"], |
39 | 37 | reporter: [ |
40 | | - ...(coverageConfigDefaults.reporter as Array<[string, EmptyObject]>), |
41 | | - // "json-summary" is used by the vitest-coverage-report GitHub Action |
42 | | - ...(process.env.GITHUB_ACTIONS ? ["json-summary"] : []), |
| 38 | + "text", // <-- for console output (even in CI, for logging/debugging purposes) |
| 39 | + "lcov", // <-- for uploading coverage info to Codecov |
| 40 | + ...(isGitHubActionWorkflow |
| 41 | + ? ["json-summary"] // <-- for the vitest-coverage-report GitHub Action |
| 42 | + : []), |
43 | 43 | ], |
44 | 44 | }, |
45 | 45 | }, |
|
0 commit comments