buildkite-test-collector
Advanced tools
Comparing version 1.2.1 to 1.3.0
@@ -26,4 +26,4 @@ const { v4: uuidv4 } = require('uuid') | ||
onRunComplete(_test, _results) { | ||
uploadTestResults(this._testEnv, this._testResults) | ||
onRunComplete(_test, _results, _options) { | ||
uploadTestResults(this._testEnv, this._testResults, this._options) | ||
} | ||
@@ -30,0 +30,0 @@ |
@@ -10,3 +10,3 @@ { | ||
], | ||
"version": "1.2.1", | ||
"version": "1.3.0", | ||
"homepage": "https://buildkite.com/test-analytics", | ||
@@ -23,2 +23,3 @@ "bugs": "https://github.com/buildkite/test-collector-javascript/issues", | ||
"axios": "^0.26.1", | ||
"dotenv": "^16.0.2", | ||
"request-spy": "^0.0.10", | ||
@@ -25,0 +26,0 @@ "uuid": "^8.3.2" |
158
README.md
@@ -11,85 +11,127 @@ # Buildkite Collectors for JavaScript | ||
1) [Create a test suite](https://buildkite.com/docs/test-analytics), and copy the API token that it gives you. | ||
1. [Create a test suite](https://buildkite.com/docs/test-analytics), and copy the API token that it gives you. | ||
2) Add the [`buildkite-test-collector` package](https://www.npmjs.com/package/buildkite-test-collector): | ||
2. Add the [`buildkite-test-collector` package](https://www.npmjs.com/package/buildkite-test-collector): | ||
```bash | ||
# If you use npm: | ||
npm install --save-dev buildkite-test-collector | ||
```bash | ||
# If you use npm: | ||
npm install --save-dev buildkite-test-collector | ||
# or, if you use yarn: | ||
yarn add --dev buildkite-test-collector | ||
``` | ||
# or, if you use yarn: | ||
yarn add --dev buildkite-test-collector | ||
``` | ||
3) Add the buildkite test collector to your testing framework: | ||
3. Add the Buildkite test collector to your testing framework: | ||
### Jest | ||
### Jest | ||
Update your [Jest configuration](https://jestjs.io/docs/configuration):<br> | ||
Update your [Jest configuration](https://jestjs.io/docs/configuration):<br> | ||
```js | ||
// jest.config.js | ||
```js | ||
// jest.config.js | ||
// Send results to Test Analytics | ||
reporters: [ | ||
'default', | ||
'buildkite-test-collector/jest/reporter' | ||
], | ||
// Send results to Test Analytics | ||
reporters: [ | ||
'default', | ||
'buildkite-test-collector/jest/reporter' | ||
], | ||
// Enable column + line capture for Test Analytics | ||
testLocationInResults: true | ||
``` | ||
// Enable column + line capture for Test Analytics | ||
testLocationInResults: true | ||
``` | ||
### Jasmine | ||
If you would like to pass in the API token using a custom environment variable, you can do so using the report options. | ||
[Add the buildkite reporter to jasmine](https://jasmine.github.io/setup/nodejs.html#reporters):<br> | ||
```js | ||
// jest.config.js | ||
```js | ||
// SpecHelper.js | ||
var BuildkiteReporter = require('buildkite-test-collector/jasmine/reporter'); | ||
var buildkiteReporter = new BuildkiteReporter(); | ||
// Send results to Test Analytics | ||
reporters: [ | ||
"default", | ||
[ | ||
"buildkite-test-collector/jest/reporter", | ||
{ token: process.env.CUSTOM_ENV_VAR }, | ||
], | ||
]; | ||
``` | ||
jasmine.getEnv().addReporter(buildkiteReporter); | ||
``` | ||
### Jasmine | ||
### Mocha | ||
[Add the Buildkite reporter to Jasmine](https://jasmine.github.io/setup/nodejs.html#reporters):<br> | ||
[Install mocha-multi-reporters](https://github.com/stanleyhlng/mocha-multi-reporters) in your project:<br> | ||
```js | ||
// SpecHelper.js | ||
var BuildkiteReporter = require("buildkite-test-collector/jasmine/reporter"); | ||
var buildkiteReporter = new BuildkiteReporter(); | ||
``` | ||
npm install mocha-multi-reporters --save-dev | ||
``` | ||
jasmine.getEnv().addReporter(buildkiteReporter); | ||
``` | ||
and configure it to run your desired reporter and the Buildkite reporter | ||
If you would like to pass in the API token using a custom environment variable, you can do so using the report options. | ||
```js | ||
// config.json | ||
{ | ||
"reporterEnabled": "spec, buildkite-test-collector/mocha/reporter" | ||
} | ||
``` | ||
```js | ||
// SpecHelper.js | ||
var buildkiteReporter = new BuildkiteReporter(undefined, { | ||
token: process.env.CUSTOM_ENV_VAR, | ||
}); | ||
``` | ||
Now update your test script to use the buildkite reporter via mocha-multi-reporters: | ||
### Mocha | ||
```js | ||
// package.json | ||
"scripts": { | ||
"test": "mocha --reporter mocha-multi-reporters --reporter-options configFile=config.json" | ||
}, | ||
``` | ||
[Install mocha-multi-reporters](https://github.com/stanleyhlng/mocha-multi-reporters) in your project:<br> | ||
4) Run your tests locally:<br> | ||
``` | ||
npm install mocha-multi-reporters --save-dev | ||
``` | ||
```js | ||
env BUILDKITE_ANALYTICS_TOKEN=xyz npm test | ||
``` | ||
and configure it to run your desired reporter and the Buildkite reporter | ||
5) Add the `BUILDKITE_ANALYTICS_TOKEN` secret to your CI, push your changes to a branch, and open a pull request 🎉 | ||
```js | ||
// config.json | ||
{ | ||
"reporterEnabled": "spec, buildkite-test-collector/mocha/reporter" | ||
} | ||
``` | ||
```bash | ||
git checkout -b add-bk-test-analytics | ||
git commit -am "Add Buildkite Test Analytics" | ||
git push origin add-bk-test-analytics | ||
``` | ||
Now update your test script to use the buildkite reporter via mocha-multi-reporters: | ||
```js | ||
// package.json | ||
"scripts": { | ||
"test": "mocha --reporter mocha-multi-reporters --reporter-options configFile=config.json" | ||
}, | ||
``` | ||
If you would like to pass in the API token using a custom environment variable, you can do so using the report options. | ||
Since the reporter options are passed in as a json file, we ask you to put the environment variable name as a string value in the `config.json`, which will be retrieved using [dotenv](https://github.com/motdotla/dotenv) in the mocha reporter. | ||
```js | ||
// config.json | ||
{ | ||
"reporterEnabled": "spec, buildkite-test-collector/mocha/reporter", | ||
"buildkiteTestCollectorMochaReporterReporterOptions": { | ||
"token_name": "CUSTOM_ENV_VAR_NAME" | ||
} | ||
} | ||
``` | ||
4. Run your tests locally:<br> | ||
```js | ||
env BUILDKITE_ANALYTICS_TOKEN=xyz npm test | ||
``` | ||
5. Add the `BUILDKITE_ANALYTICS_TOKEN` secret to your CI, push your changes to a branch, and open a pull request 🎉 | ||
```bash | ||
git checkout -b add-bk-test-analytics | ||
git commit -am "Add Buildkite Test Analytics" | ||
git push origin add-bk-test-analytics | ||
``` | ||
## 📓 Notes | ||
This jest collector uses the [`onRunComplete`](https://jestjs.io/docs/configuration#custom-reporters) hook to report test results to the Buildkite API. This interferes with the [`--forceExit`](https://jestjs.io/docs/cli#--forceexit) CLI option and interrupts the request that sends tests results, meaning that no data will be available for your test suite. It's recommended to use [`--detectOpenHandles`](https://jestjs.io/docs/cli#--detectopenhandles) to find any hanging process and clean them up and remove the use of `--forceExit`. | ||
## 🔍 Debugging | ||
@@ -96,0 +138,0 @@ |
@@ -5,4 +5,4 @@ const Debug = require('../util/debug') | ||
const uploadTestResults = (env, results, done) => { | ||
const buildkiteAnalyticsToken = process.env.BUILDKITE_ANALYTICS_TOKEN | ||
const uploadTestResults = (env, results, options, done) => { | ||
const buildkiteAnalyticsToken = options?.token || process.env.BUILDKITE_ANALYTICS_TOKEN | ||
let data | ||
@@ -9,0 +9,0 @@ |
@@ -49,2 +49,34 @@ const uploadTestResults = require('./uploadTestResults') | ||
describe('with token "abc" defined in reporter options', () => { | ||
beforeEach(() => { | ||
process.env.BUILDKITE_ANALYTICS_KEY = 'key123'; | ||
}) | ||
it('posts a result', () => { | ||
axios.post.mockResolvedValue({ data: "Success" }) | ||
uploadTestResults(new CI().env(), ['result'], { token: 'abc'}) | ||
expect(axios.post.mock.calls[0]).toEqual([ | ||
"https://analytics-api.buildkite.com/v1/uploads", | ||
{ | ||
"data": [ "result" ], | ||
"format": "json", | ||
"run_env": { | ||
"ci": "generic", | ||
"collector": "js-buildkite-test-collector", | ||
"key": "key123", | ||
"version": version | ||
} | ||
}, | ||
{ | ||
"headers": { | ||
"Authorization": "Token token=\"abc\"", | ||
"Content-Type": "application/json", | ||
} | ||
} | ||
]) | ||
}) | ||
}) | ||
describe('with token "abc"', () => { | ||
@@ -85,3 +117,3 @@ beforeEach(() => { | ||
axios.post.mockResolvedValue({ data: "Success" }); | ||
uploadTestResults({}, Array(4999).fill('result')) | ||
@@ -94,3 +126,3 @@ | ||
axios.post.mockResolvedValue({ data: "Success" }) | ||
uploadTestResults({}, Array(12000).fill('result')) | ||
@@ -97,0 +129,0 @@ |
25515
536
181
4
51
+ Addeddotenv@^16.0.2
+ Addeddotenv@16.4.7(transitive)