Skip to content

Commit cf2f402

Browse files
authored
chore: introduce test debugging for cli tests (#5319)
* chore: introduce test debugging for cli tests This is mostly useful for dev server tests * chore: update contributors field * chore: threads Co-authored-by: danez <[email protected]>
1 parent 9a7c666 commit cf2f402

File tree

4 files changed

+37
-3
lines changed

4 files changed

+37
-3
lines changed

CONTRIBUTING.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ A good place to start is reading the base command README and looking at the comm
9494
9595
### Testing
9696

97-
This repo uses [ava](https://github.com/avajs/ava) for testing. Unit tests are in the `tests/unit` folder and
97+
This repo uses [vitest](https://github.com/vitest-dev/vitest) for testing. Unit tests are in the `tests/unit` folder and
9898
integration tests are in the `tests/integration` folder. We use this convention since we split tests across multiple CI
9999
machines to speed up CI time. You can read about it more [here](https://github.com/netlify/cli/issues/4178).
100100

@@ -105,6 +105,32 @@ We also test for a few other things:
105105
- Test coverage
106106
- Must work with Windows + Unix environments.
107107

108+
#### Debugging tests
109+
110+
To run a single test file you can do:
111+
112+
```
113+
npm exec vitest -- run tests/unit/tests/unit/lib/account.test.mjs
114+
```
115+
116+
To run a single test you can either use `test.only` inside the test file and ran the above command or run this:
117+
118+
```
119+
npm exec vitest -- run tests/unit/tests/unit/lib/account.test.mjs -t 'test name'
120+
```
121+
122+
Some of the tests actually start the CLI in a subprocess and therefore sometimes underlying errors are not visible in
123+
the tests when they fail. By default the output of the subprocess is not forwarded to the main process to keep the cli
124+
output clean. To debug test failures like this you can set the environment variable `DEBUG_TESTS=true` and the
125+
subprocess will pipe it's output to the main process for you to see.
126+
127+
When `DEBUG_TESTS` is set the vitest reporter will be set to `tap` so the test output won't interfere with the debug
128+
output.
129+
130+
```
131+
DEBUG_TESTS=true npm exec vitest -- run tests/unit/tests/unit/lib/account.test.mjs -t 'test name'
132+
```
133+
108134
### Lint docs per Netlify style guide
109135

110136
1. [Install vale](https://docs.errata.ai/vale/install)

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,9 @@
150150
"cesare soldini <[email protected]>",
151151
"chris (fool) mccraw (http://twitter.com/fool)",
152152
"dustincrogers",
153+
"eddie",
153154
"ehmicky (https://twitter.com/ehmicky)",
154155
"internal tools netlibot",
155-
"just toby",
156156
"kvn-shn",
157157
"netlibot (https://www.netlify.com)",
158158
"nikoladev",
@@ -211,7 +211,7 @@
211211
"test:init:eleventy-deps": "npm ci --prefix tests/integration/eleventy-site --no-audit",
212212
"test:init:hugo-deps": "npm ci --prefix tests/integration/hugo-site --no-audit",
213213
"test:dev:ava": "ava --verbose",
214-
"test:dev:vitest": "vitest run",
214+
"test:dev:vitest": "vitest run tests/unit/ && vitest run tests/integration",
215215
"test:ci:ava:integration": "c8 -r json ava --concurrency 1 --no-worker-threads tests/integration/",
216216
"test:ci:vitest:unit": "vitest run --coverage tests/unit/",
217217
"test:ci:vitest:integration": "vitest run --coverage --no-threads tests/integration/",

tests/integration/utils/dev-server.cjs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ const startServer = async ({
4444
getExecaOptions({ cwd, env }),
4545
)
4646

47+
if (process.env.DEBUG_TESTS) {
48+
ps.stderr.pipe(process.stderr)
49+
ps.stdout.pipe(process.stdout)
50+
}
51+
4752
const promptHistory = []
4853

4954
if (prompt) {

vitest.config.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
/// <reference types="vitest" />
2+
import process from 'process'
3+
24
import { defineConfig } from 'vite'
35

46
export default defineConfig({
@@ -9,6 +11,7 @@ export default defineConfig({
911
external: ['**/fixtures/**', '**/node_modules/**'],
1012
interopDefault: false,
1113
},
14+
reporters: [process.env.DEBUG_TESTS ? 'tap' : 'default'],
1215
coverage: {
1316
provider: 'c8',
1417
reporter: ['text', 'lcov'],

0 commit comments

Comments
 (0)