Skip to content

Commit e6afab4

Browse files
Configure Jest to generate JUnit formatted reports
Jest is able to generate JUnit formatted reports using the jest-junit (1) package. It has been defined in the main configuration file as additional reporter next to the `default` report format. See Circle CI's documentation about to how to collect test data (2) for more details about setups with Jest. References: (1) https://github.com/codecov/jest-junit (2) https://circleci.com/docs/2.0/collect-test-data/#jest GH-44
1 parent dd1a155 commit e6afab4

File tree

5 files changed

+68
-36
lines changed

5 files changed

+68
-36
lines changed

.circleci/config.yml

+10-5
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ commands:
3131
path: ./public
3232
- store_artifacts:
3333
path: ./node_modules.tgz
34+
- store_test_results:
35+
path: ./build/reports/junit
3436
- codecov/upload:
3537
file: ./build/reports/coverage/coverage-final.json
3638
flags: unit
@@ -79,8 +81,8 @@ commands:
7981
name: Lint
8082
command: npm run lint
8183
- run:
82-
name: Run tests with coverage
83-
command: npm run test:cov
84+
name: Run tests with coverage in CI mode
85+
command: npm run test:ci
8486

8587
jobs:
8688
nodejs-v8:
@@ -115,6 +117,9 @@ workflows:
115117
version: 2.1
116118
build-multi-version:
117119
jobs:
118-
- nodejs-v8
119-
- nodejs-v10
120-
- nodejs-latest
120+
- nodejs-v8:
121+
context: nord-docs-ctx
122+
- nodejs-v10:
123+
context: nord-docs-ctx
124+
- nodejs-latest:
125+
context: nord-docs-ctx

jest.config.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@
1313
* @author Sven Greb <[email protected]>
1414
* @see https://jestjs.io/docs/en/configuration
1515
* @see https://www.gatsbyjs.org/docs/testing
16+
* @see https://circleci.com/docs/2.0/collect-test-data/#jest
1617
* @since 0.1.0
1718
*/
1819

19-
const { BASE_DIR_BUILD_REPORTS_COVERAGE } = require("./src/config/internal/constants");
20+
const { BASE_DIR_BUILD_REPORTS_COVERAGE, BASE_DIR_BUILD_REPORTS_JUNIT } = require("./src/config/internal/constants");
2021

2122
module.exports = {
2223
/*
@@ -70,6 +71,20 @@ module.exports = {
7071
*/
7172
modulePaths: ["<rootDir>/test/__utils__"],
7273

74+
/*
75+
* An array of module names to specify which reporters will be used.
76+
*/
77+
reporters: [
78+
"default",
79+
[
80+
"jest-junit",
81+
{
82+
outputDirectory: `${BASE_DIR_BUILD_REPORTS_JUNIT}`,
83+
outputName: "jest.xml"
84+
}
85+
]
86+
],
87+
7388
/*
7489
* The paths to modules that run some code to configure or set up the testing environment before each test.
7590
* The `___loader` shim is a global function used by internal Gatsby APIs.

package-lock.json

+30-30
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@
3838
"lint": "npm-run-all lint:*",
3939
"lint:js": "eslint --ext .js,.jsx .",
4040
"lint:md": "remark --no-stdout . \".github/**/*.md\"",
41+
"report:js:junit": "eslint --ext .js,.jsx --format junit -o ./build/reports/junit/eslint.xml .",
4142
"serve": "gatsby serve",
4243
"test": "jest",
44+
"test:ci": "jest --ci --coverage",
4345
"test:cov": "jest --coverage",
4446
"test:watch": "jest --watch"
4547
},
@@ -67,6 +69,7 @@
6769
"identity-obj-proxy": "3.0.0",
6870
"jest": "23.6.0",
6971
"jest-dom": "2.1.1",
72+
"jest-junit": "5.2.0",
7073
"lint-staged": "8.0.5",
7174
"npm-run-all": "4.1.3",
7275
"prettier": "1.15.2",

src/config/internal/constants.js

+9
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ const BASE_DIR_BUILD_REPORTS = `${BASE_DIR_BUILD}/reports`;
7070
*/
7171
const BASE_DIR_BUILD_REPORTS_COVERAGE = `${BASE_DIR_BUILD_REPORTS}/coverage`;
7272

73+
/**
74+
* The relative path of the build base directory for JUnit reports starting from the project root.
75+
*
76+
* @constant {string}
77+
* @since 0.1.0
78+
*/
79+
const BASE_DIR_BUILD_REPORTS_JUNIT = `${BASE_DIR_BUILD_REPORTS}/junit`;
80+
7381
/**
7482
* The relative path of the config base directory starting from the project root.
7583
*
@@ -109,6 +117,7 @@ module.exports = {
109117
BASE_DIR_BUILD,
110118
BASE_DIR_BUILD_REPORTS,
111119
BASE_DIR_BUILD_REPORTS_COVERAGE,
120+
BASE_DIR_BUILD_REPORTS_JUNIT,
112121
BASE_DIR_CONFIG,
113122
BASE_DIR_CONTENT,
114123
BASE_DIR_PAGES,

0 commit comments

Comments
 (0)