|
| 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 | +}) |
0 commit comments