Skip to content

Commit 5c20acf

Browse files
authored
Merge pull request #111 from mitcsutt/vitest-reporter
Added vitest reporter Resolves #82
2 parents fe02eb1 + d6f0802 commit 5c20acf

10 files changed

Lines changed: 3255 additions & 76 deletions

File tree

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,44 @@ Official [Buildkite Test Engine](https://buildkite.com/platform/test-engine) col
5454
];
5555
```
5656
57+
### Vitest
58+
59+
Update your [Vitest configuration](https://vitest.dev/config/):<br>
60+
61+
```js
62+
// vitest.config.js OR vite.config.js
63+
64+
65+
test: {
66+
// Send results to Test Engine
67+
reporters: [
68+
'default',
69+
'buildkite-test-collector/vitest/reporter'
70+
],
71+
72+
// Enable column + line capture for Test Engine
73+
includeTaskLocation: true,
74+
}
75+
```
76+
77+
If you would like to pass in the API token using a custom environment variable, you can do so using the report options.
78+
79+
```js
80+
// vitest.config.js OR vite.config.js
81+
82+
83+
test: {
84+
// Send results to Test Engine
85+
reporters: [
86+
'default',
87+
[
88+
"buildkite-test-collector/vitest/reporter",
89+
{ token: process.env.CUSTOM_ENV_VAR },
90+
],
91+
],
92+
}
93+
```
94+
5795
### Jasmine
5896
5997
[Add the Buildkite reporter to Jasmine](https://jasmine.github.io/setup/nodejs.html#reporters):<br>

e2e/vitest.test.js

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
// Does an end-to-end test of the vitest example, using the debug output from the
2+
// reporter, and verifying the JSON
3+
require('dotenv').config();
4+
const { exec } = require('child_process');
5+
const path = require('path');
6+
7+
describe('examples/vitest', () => {
8+
const cwd = path.join(__dirname, "../examples/vitest")
9+
const env = {
10+
...process.env,
11+
BUILDKITE_ANALYTICS_TOKEN: "xyz",
12+
BUILDKITE_ANALYTICS_VITEST_TOKEN: "abc",
13+
BUILDKITE_ANALYTICS_DEBUG_ENABLED: "true"
14+
}
15+
16+
describe('when token is defined through reporter options', () => {
17+
test('it uses the correct token', (done) => {
18+
exec('vitest test --config token.config.js', { cwd, env: { ...env, BUILDKITE_ANALYTICS_TOKEN: undefined } }, (error, stdout, stderr) => {
19+
expect(stdout).toMatch(/.*Test Engine Sending: ({.*})/m);
20+
21+
const jsonMatch = stdout.match(/.*Test Engine Sending: ({.*})/m)
22+
const json = JSON.parse(jsonMatch[1])["headers"]
23+
24+
expect(json).toHaveProperty("Authorization", 'Token token="abc"')
25+
26+
done()
27+
})
28+
}, 10000) // 10s timeout
29+
})
30+
31+
describe('when token is defined through BUILDKITE_ANALYTICS_TOKEN', () => {
32+
test('it uses the correct token', (done) => {
33+
exec('npm test', { cwd, env }, (error, stdout, stderr) => {
34+
expect(stdout).toMatch(/.*Test Engine Sending: ({.*})/m);
35+
36+
const jsonMatch = stdout.match(/.*Test Engine Sending: ({.*})/m)
37+
const json = JSON.parse(jsonMatch[1])["headers"]
38+
39+
expect(json).toHaveProperty("Authorization", 'Token token="xyz"')
40+
41+
done()
42+
})
43+
}, 10000) // 10s timeout
44+
})
45+
46+
test('it posts the correct JSON', (done) => {
47+
exec('npm test', { cwd, env }, (error, stdout, stderr) => {
48+
expect(stdout).toMatch(/.*Test Engine Sending: ({.*})/m);
49+
50+
const jsonMatch = stdout.match(/.*Test Engine Sending: ({.*})/m)
51+
const json = JSON.parse(jsonMatch[1])["data"]
52+
53+
// Uncomment to view the JSON
54+
// console.log(json)
55+
56+
expect(json).toHaveProperty("format", "json")
57+
58+
expect(json).toHaveProperty("run_env.ci")
59+
expect(json).toHaveProperty("run_env.debug", 'true')
60+
expect(json).toHaveProperty("run_env.key")
61+
expect(json).toHaveProperty("run_env.version")
62+
expect(json).toHaveProperty("run_env.collector", "js-buildkite-test-collector")
63+
64+
expect(json).toHaveProperty("tags", { "hello": "vitest" }) // examples/vitest/vitest.config.js
65+
66+
expect(json).toHaveProperty("data[0].scope", '')
67+
expect(json).toHaveProperty("data[0].name", '1 + 2 to equal 3')
68+
expect(json).toHaveProperty("data[0].location", "example.test.js:4")
69+
expect(json).toHaveProperty("data[0].file_name", "example.test.js")
70+
expect(json).toHaveProperty("data[0].result", 'passed')
71+
72+
expect(json).toHaveProperty("data[1].scope", "sum")
73+
expect(json).toHaveProperty("data[1].name", "40 + 1 equal 42")
74+
expect(json).toHaveProperty("data[1].location", "example.test.js:10")
75+
expect(json).toHaveProperty("data[1].file_name", "example.test.js")
76+
expect(json).toHaveProperty("data[1].result", "failed")
77+
expect(json).toHaveProperty("data[1].failure_reason")
78+
expect(json.data[1].failure_reason).toEqual('AssertionError: expected 41 to be 42 // Object.is equality')
79+
80+
expect(json).toHaveProperty("data[1].failure_expanded")
81+
expect(json.data[1].failure_expanded).toEqual(expect.arrayContaining([
82+
expect.objectContaining({
83+
expanded: expect.arrayContaining([expect.stringContaining("vitest/example.test.js\:11\:20")])
84+
})
85+
]))
86+
87+
expect(json).toHaveProperty("data[0].history")
88+
89+
const firstHistory = json.data[0].history
90+
expect(firstHistory).toHaveProperty("section", "top")
91+
expect(firstHistory).toHaveProperty("start_at", expect.any(Number))
92+
expect(firstHistory.start_at).toBeGreaterThanOrEqual(0)
93+
expect(firstHistory).toHaveProperty("end_at", expect.any(Number))
94+
expect(firstHistory.end_at).toBeGreaterThan(firstHistory.start_at)
95+
expect(firstHistory).toHaveProperty("duration", expect.any(Number))
96+
expect(firstHistory.duration).toBeGreaterThan(0)
97+
98+
const secondHistory = json.data[1].history
99+
expect(secondHistory).toHaveProperty("section", "top")
100+
expect(secondHistory).toHaveProperty("start_at", expect.any(Number))
101+
expect(secondHistory.start_at).toBeGreaterThanOrEqual(0)
102+
expect(secondHistory).toHaveProperty("end_at", expect.any(Number))
103+
expect(secondHistory.end_at).toBeGreaterThan(secondHistory.start_at)
104+
expect(secondHistory).toHaveProperty("duration", expect.any(Number))
105+
expect(secondHistory.duration).toBeGreaterThan(0)
106+
107+
done()
108+
})
109+
}, 10000) // 10s timeout
110+
111+
test('it supports test location prefixes for monorepos', (done) => {
112+
exec('npm test', { cwd, env: { ...env, BUILDKITE_ANALYTICS_LOCATION_PREFIX: "some-sub-dir/" } }, (error, stdout, stderr) => {
113+
expect(stdout).toMatch(/.*Test Engine Sending: ({.*})/m);
114+
115+
const jsonMatch = stdout.match(/.*Test Engine Sending: ({.*})/m)
116+
const json = JSON.parse(jsonMatch[1])["data"]
117+
118+
// Uncomment to view the JSON
119+
// console.log(json)
120+
121+
expect(json).toHaveProperty("run_env.location_prefix", "some-sub-dir/")
122+
123+
expect(json).toHaveProperty("data[0].location", "some-sub-dir/example.test.js:4")
124+
expect(json).toHaveProperty("data[1].location", "some-sub-dir/example.test.js:10")
125+
126+
done()
127+
})
128+
}, 10000) // 10s timeout
129+
130+
describe('when test is pass but upload fails', () => {
131+
beforeAll(() => {
132+
// This will cause the upload to fail
133+
env.BUILDKITE_ANALYTICS_BASE_URL = "http://"
134+
})
135+
136+
test('it should not throw an error', (done) => {
137+
exec('npm test passed.test.js', { cwd, env }, (error, stdout, stderr) => {
138+
//console.log(stdout)
139+
expect(error).toBeNull()
140+
141+
done()
142+
})
143+
})
144+
})
145+
})

examples/vitest/example.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { test, describe, expect } from 'vitest';
2+
3+
// No scope
4+
test('1 + 2 to equal 3', () => {
5+
expect(1 + 2).toBe(3);
6+
});
7+
8+
// In a scope
9+
describe('sum', () => {
10+
test('40 + 1 equal 42', () => {
11+
expect(40 + 1).toBe(42);
12+
});
13+
})

examples/vitest/package.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "buildkite-test-collector-vitest-example",
3+
"type": "module",
4+
"scripts": {
5+
"test": "vitest run"
6+
},
7+
"devDependencies": {
8+
"buildkite-test-collector": "file:../..",
9+
"vitest": "^3.0.0"
10+
}
11+
}

examples/vitest/passed.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { it, describe, expect } from 'vitest';
2+
3+
describe('passed', () => {
4+
it('is true', () => {
5+
expect(true).toBeTruthy()
6+
});
7+
})

examples/vitest/token.config.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const config = {
2+
// Send results to Test Engine
3+
test: {
4+
reporters: [
5+
'default',
6+
['buildkite-test-collector/vitest/reporter', {
7+
token: process.env.BUILDKITE_ANALYTICS_VITEST_TOKEN,
8+
}]
9+
],
10+
includeTaskLocation: true,
11+
}
12+
};
13+
14+
export default config;

examples/vitest/vitest.config.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const config = {
2+
// Send results to Test Engine
3+
test: {
4+
reporters: [
5+
'default',
6+
['buildkite-test-collector/vitest/reporter', {
7+
tags: { hello: "vitest" }
8+
}]
9+
],
10+
includeTaskLocation: true,
11+
}
12+
};
13+
14+
export default config;

0 commit comments

Comments
 (0)