diff --git a/core/cli/test/__snapshots__/config.test.ts.snap b/core/cli/test/__snapshots__/config.test.ts.snap index a4a7ee790..08ad1d010 100644 --- a/core/cli/test/__snapshots__/config.test.ts.snap +++ b/core/cli/test/__snapshots__/config.test.ts.snap @@ -40,7 +40,11 @@ exports[`loadConfig should load a config from a root 1`] = ` "parent": [Circular], "rcFile": { "commands": { - "test:ci": "Mocha", + "test:ci": { + "Mocha": { + "ci": true, + }, + }, "test:local": "Mocha", }, "hooks": undefined, @@ -169,7 +173,11 @@ exports[`loadConfig should load a config from a root 1`] = ` "parent": [Circular], "rcFile": { "commands": { - "test:ci": "Mocha", + "test:ci": { + "Mocha": { + "ci": true, + }, + }, "test:local": "Mocha", }, "hooks": undefined, @@ -299,7 +307,11 @@ exports[`loadConfig should load a config from a root 1`] = ` "parent": [Circular], "rcFile": { "commands": { - "test:ci": "Mocha", + "test:ci": { + "Mocha": { + "ci": true, + }, + }, "test:local": "Mocha", }, "hooks": undefined, @@ -431,7 +443,11 @@ exports[`loadConfig should load a config from a root 1`] = ` "parent": [Circular], "rcFile": { "commands": { - "test:ci": "Mocha", + "test:ci": { + "Mocha": { + "ci": true, + }, + }, "test:local": "Mocha", }, "hooks": undefined, @@ -560,7 +576,11 @@ exports[`loadConfig should load a config from a root 1`] = ` "parent": [Circular], "rcFile": { "commands": { - "test:ci": "Mocha", + "test:ci": { + "Mocha": { + "ci": true, + }, + }, "test:local": "Mocha", }, "hooks": undefined, @@ -690,7 +710,11 @@ exports[`loadConfig should load a config from a root 1`] = ` "parent": [Circular], "rcFile": { "commands": { - "test:ci": "Mocha", + "test:ci": { + "Mocha": { + "ci": true, + }, + }, "test:local": "Mocha", }, "hooks": undefined, @@ -825,7 +849,11 @@ exports[`loadConfig should load a config from a root 1`] = ` "parent": [Circular], "rcFile": { "commands": { - "test:ci": "Mocha", + "test:ci": { + "Mocha": { + "ci": true, + }, + }, "test:local": "Mocha", }, "hooks": undefined, @@ -954,7 +982,11 @@ exports[`loadConfig should load a config from a root 1`] = ` "parent": [Circular], "rcFile": { "commands": { - "test:ci": "Mocha", + "test:ci": { + "Mocha": { + "ci": true, + }, + }, "test:local": "Mocha", }, "hooks": undefined, @@ -1060,7 +1092,11 @@ exports[`loadConfig should load a config from a root 1`] = ` "parent": [Circular], "rcFile": { "commands": { - "test:ci": "Mocha", + "test:ci": { + "Mocha": { + "ci": true, + }, + }, "test:local": "Mocha", }, "hooks": undefined, @@ -1189,7 +1225,11 @@ exports[`loadConfig should load a config from a root 1`] = ` "parent": [Circular], "rcFile": { "commands": { - "test:ci": "Mocha", + "test:ci": { + "Mocha": { + "ci": true, + }, + }, "test:local": "Mocha", }, "hooks": undefined, @@ -1347,7 +1387,11 @@ exports[`loadConfig should load a config from a root 1`] = ` "parent": [Circular], "rcFile": { "commands": { - "test:ci": "Mocha", + "test:ci": { + "Mocha": { + "ci": true, + }, + }, "test:local": "Mocha", }, "hooks": undefined, @@ -1476,7 +1520,11 @@ exports[`loadConfig should load a config from a root 1`] = ` "parent": [Circular], "rcFile": { "commands": { - "test:ci": "Mocha", + "test:ci": { + "Mocha": { + "ci": true, + }, + }, "test:local": "Mocha", }, "hooks": undefined, @@ -1609,7 +1657,11 @@ exports[`loadConfig should load a config from a root 1`] = ` "parent": [Circular], "rcFile": { "commands": { - "test:ci": "Mocha", + "test:ci": { + "Mocha": { + "ci": true, + }, + }, "test:local": "Mocha", }, "hooks": undefined, @@ -1738,7 +1790,11 @@ exports[`loadConfig should load a config from a root 1`] = ` "parent": [Circular], "rcFile": { "commands": { - "test:ci": "Mocha", + "test:ci": { + "Mocha": { + "ci": true, + }, + }, "test:local": "Mocha", }, "hooks": undefined, @@ -1851,7 +1907,11 @@ exports[`loadConfig should load a config from a root 1`] = ` "parent": [Circular], "rcFile": { "commands": { - "test:ci": "Mocha", + "test:ci": { + "Mocha": { + "ci": true, + }, + }, "test:local": "Mocha", }, "hooks": undefined, @@ -2079,7 +2139,11 @@ exports[`loadConfig should load a config from a root 1`] = ` }, "rcFile": { "commands": { - "test:ci": "Mocha", + "test:ci": { + "Mocha": { + "ci": true, + }, + }, "test:local": "Mocha", }, "hooks": undefined, @@ -2145,7 +2209,11 @@ exports[`loadConfig should load a config from a root 1`] = ` "parent": [Circular], "rcFile": { "commands": { - "test:ci": "Mocha", + "test:ci": { + "Mocha": { + "ci": true, + }, + }, "test:local": "Mocha", }, "hooks": undefined, @@ -2276,7 +2344,11 @@ exports[`loadConfig should load a config from a root 1`] = ` "parent": [Circular], "rcFile": { "commands": { - "test:ci": "Mocha", + "test:ci": { + "Mocha": { + "ci": true, + }, + }, "test:local": "Mocha", }, "hooks": undefined, @@ -2433,7 +2505,11 @@ exports[`loadConfig should load a config from a root 1`] = ` "parent": [Circular], "rcFile": { "commands": { - "test:ci": "Mocha", + "test:ci": { + "Mocha": { + "ci": true, + }, + }, "test:local": "Mocha", }, "hooks": undefined, @@ -2565,7 +2641,11 @@ exports[`loadConfig should load a config from a root 1`] = ` "parent": [Circular], "rcFile": { "commands": { - "test:ci": "Mocha", + "test:ci": { + "Mocha": { + "ci": true, + }, + }, "test:local": "Mocha", }, "hooks": undefined, @@ -2697,7 +2777,11 @@ exports[`loadConfig should load a config from a root 1`] = ` "parent": [Circular], "rcFile": { "commands": { - "test:ci": "Mocha", + "test:ci": { + "Mocha": { + "ci": true, + }, + }, "test:local": "Mocha", }, "hooks": undefined, @@ -2806,7 +2890,11 @@ exports[`loadConfig should load a config from a root 1`] = ` "parent": [Circular], "rcFile": { "commands": { - "test:ci": "Mocha", + "test:ci": { + "Mocha": { + "ci": true, + }, + }, "test:local": "Mocha", }, "hooks": undefined, @@ -3029,7 +3117,11 @@ exports[`loadConfig should load a config from a root 1`] = ` }, "rcFile": { "commands": { - "test:ci": "Mocha", + "test:ci": { + "Mocha": { + "ci": true, + }, + }, "test:local": "Mocha", }, "hooks": undefined, @@ -3090,7 +3182,11 @@ exports[`loadConfig should load a config from a root 1`] = ` "parent": [Circular], "rcFile": { "commands": { - "test:ci": "Mocha", + "test:ci": { + "Mocha": { + "ci": true, + }, + }, "test:local": "Mocha", }, "hooks": undefined, @@ -3223,7 +3319,11 @@ exports[`loadConfig should load a config from a root and validate it 1`] = ` "parent": [Circular], "rcFile": { "commands": { - "test:ci": "Mocha", + "test:ci": { + "Mocha": { + "ci": true, + }, + }, "test:local": "Mocha", }, "hooks": undefined, @@ -3352,7 +3452,11 @@ exports[`loadConfig should load a config from a root and validate it 1`] = ` "parent": [Circular], "rcFile": { "commands": { - "test:ci": "Mocha", + "test:ci": { + "Mocha": { + "ci": true, + }, + }, "test:local": "Mocha", }, "hooks": undefined, @@ -3482,7 +3586,11 @@ exports[`loadConfig should load a config from a root and validate it 1`] = ` "parent": [Circular], "rcFile": { "commands": { - "test:ci": "Mocha", + "test:ci": { + "Mocha": { + "ci": true, + }, + }, "test:local": "Mocha", }, "hooks": undefined, @@ -3614,7 +3722,11 @@ exports[`loadConfig should load a config from a root and validate it 1`] = ` "parent": [Circular], "rcFile": { "commands": { - "test:ci": "Mocha", + "test:ci": { + "Mocha": { + "ci": true, + }, + }, "test:local": "Mocha", }, "hooks": undefined, @@ -3743,7 +3855,11 @@ exports[`loadConfig should load a config from a root and validate it 1`] = ` "parent": [Circular], "rcFile": { "commands": { - "test:ci": "Mocha", + "test:ci": { + "Mocha": { + "ci": true, + }, + }, "test:local": "Mocha", }, "hooks": undefined, @@ -3873,7 +3989,11 @@ exports[`loadConfig should load a config from a root and validate it 1`] = ` "parent": [Circular], "rcFile": { "commands": { - "test:ci": "Mocha", + "test:ci": { + "Mocha": { + "ci": true, + }, + }, "test:local": "Mocha", }, "hooks": undefined, @@ -4008,7 +4128,11 @@ exports[`loadConfig should load a config from a root and validate it 1`] = ` "parent": [Circular], "rcFile": { "commands": { - "test:ci": "Mocha", + "test:ci": { + "Mocha": { + "ci": true, + }, + }, "test:local": "Mocha", }, "hooks": undefined, @@ -4137,7 +4261,11 @@ exports[`loadConfig should load a config from a root and validate it 1`] = ` "parent": [Circular], "rcFile": { "commands": { - "test:ci": "Mocha", + "test:ci": { + "Mocha": { + "ci": true, + }, + }, "test:local": "Mocha", }, "hooks": undefined, @@ -4243,7 +4371,11 @@ exports[`loadConfig should load a config from a root and validate it 1`] = ` "parent": [Circular], "rcFile": { "commands": { - "test:ci": "Mocha", + "test:ci": { + "Mocha": { + "ci": true, + }, + }, "test:local": "Mocha", }, "hooks": undefined, @@ -4372,7 +4504,11 @@ exports[`loadConfig should load a config from a root and validate it 1`] = ` "parent": [Circular], "rcFile": { "commands": { - "test:ci": "Mocha", + "test:ci": { + "Mocha": { + "ci": true, + }, + }, "test:local": "Mocha", }, "hooks": undefined, @@ -4530,7 +4666,11 @@ exports[`loadConfig should load a config from a root and validate it 1`] = ` "parent": [Circular], "rcFile": { "commands": { - "test:ci": "Mocha", + "test:ci": { + "Mocha": { + "ci": true, + }, + }, "test:local": "Mocha", }, "hooks": undefined, @@ -4659,7 +4799,11 @@ exports[`loadConfig should load a config from a root and validate it 1`] = ` "parent": [Circular], "rcFile": { "commands": { - "test:ci": "Mocha", + "test:ci": { + "Mocha": { + "ci": true, + }, + }, "test:local": "Mocha", }, "hooks": undefined, @@ -4792,7 +4936,11 @@ exports[`loadConfig should load a config from a root and validate it 1`] = ` "parent": [Circular], "rcFile": { "commands": { - "test:ci": "Mocha", + "test:ci": { + "Mocha": { + "ci": true, + }, + }, "test:local": "Mocha", }, "hooks": undefined, @@ -4921,7 +5069,11 @@ exports[`loadConfig should load a config from a root and validate it 1`] = ` "parent": [Circular], "rcFile": { "commands": { - "test:ci": "Mocha", + "test:ci": { + "Mocha": { + "ci": true, + }, + }, "test:local": "Mocha", }, "hooks": undefined, @@ -5027,7 +5179,11 @@ exports[`loadConfig should load a config from a root and validate it 1`] = ` "parent": [Circular], "rcFile": { "commands": { - "test:ci": "Mocha", + "test:ci": { + "Mocha": { + "ci": true, + }, + }, "test:local": "Mocha", }, "hooks": undefined, @@ -5247,7 +5403,11 @@ exports[`loadConfig should load a config from a root and validate it 1`] = ` }, "rcFile": { "commands": { - "test:ci": "Mocha", + "test:ci": { + "Mocha": { + "ci": true, + }, + }, "test:local": "Mocha", }, "hooks": undefined, @@ -5305,7 +5465,11 @@ exports[`loadConfig should load a config from a root and validate it 1`] = ` "parent": [Circular], "rcFile": { "commands": { - "test:ci": "Mocha", + "test:ci": { + "Mocha": { + "ci": true, + }, + }, "test:local": "Mocha", }, "hooks": undefined, @@ -5428,7 +5592,11 @@ exports[`loadConfig should load a config from a root and validate it 1`] = ` "parent": [Circular], "rcFile": { "commands": { - "test:ci": "Mocha", + "test:ci": { + "Mocha": { + "ci": true, + }, + }, "test:local": "Mocha", }, "hooks": undefined, @@ -5584,7 +5752,11 @@ exports[`loadConfig should load a config from a root and validate it 1`] = ` "parent": [Circular], "rcFile": { "commands": { - "test:ci": "Mocha", + "test:ci": { + "Mocha": { + "ci": true, + }, + }, "test:local": "Mocha", }, "hooks": undefined, @@ -5716,7 +5888,11 @@ exports[`loadConfig should load a config from a root and validate it 1`] = ` "parent": [Circular], "rcFile": { "commands": { - "test:ci": "Mocha", + "test:ci": { + "Mocha": { + "ci": true, + }, + }, "test:local": "Mocha", }, "hooks": undefined, @@ -5848,7 +6024,11 @@ exports[`loadConfig should load a config from a root and validate it 1`] = ` "parent": [Circular], "rcFile": { "commands": { - "test:ci": "Mocha", + "test:ci": { + "Mocha": { + "ci": true, + }, + }, "test:local": "Mocha", }, "hooks": undefined, @@ -5957,7 +6137,11 @@ exports[`loadConfig should load a config from a root and validate it 1`] = ` "parent": [Circular], "rcFile": { "commands": { - "test:ci": "Mocha", + "test:ci": { + "Mocha": { + "ci": true, + }, + }, "test:local": "Mocha", }, "hooks": undefined, @@ -6180,7 +6364,11 @@ exports[`loadConfig should load a config from a root and validate it 1`] = ` }, "rcFile": { "commands": { - "test:ci": "Mocha", + "test:ci": { + "Mocha": { + "ci": true, + }, + }, "test:local": "Mocha", }, "hooks": undefined, @@ -6241,7 +6429,11 @@ exports[`loadConfig should load a config from a root and validate it 1`] = ` "parent": [Circular], "rcFile": { "commands": { - "test:ci": "Mocha", + "test:ci": { + "Mocha": { + "ci": true, + }, + }, "test:local": "Mocha", }, "hooks": undefined, diff --git a/package-lock.json b/package-lock.json index c1b02f73b..5d16a3ec6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15486,6 +15486,16 @@ "version": "0.7.0", "license": "MIT" }, + "node_modules/charenc": { + "version": "0.0.2", + "resolved": "https://registry.npmjs.org/charenc/-/charenc-0.0.2.tgz", + "integrity": "sha512-yrLQ/yVUFXkzg7EDQsPieE/53+0RlaWTs+wBrvW36cyilJ2SaDWfl4Yj7MtLTXleV9uEKefbAGUPv2/iWSooRA==", + "license": "BSD-3-Clause", + "peer": true, + "engines": { + "node": "*" + } + }, "node_modules/check-engines": { "version": "1.6.0", "dev": true, @@ -16612,6 +16622,16 @@ "which": "^1.2.9" } }, + "node_modules/crypt": { + "version": "0.0.2", + "resolved": "https://registry.npmjs.org/crypt/-/crypt-0.0.2.tgz", + "integrity": "sha512-mCxBlsHFYh9C+HVpiEacem8FEBnMXgU9gy4zmNC+SXAZNB/1idgp/aulFJ4FgCi7GPEVbfyng092GqL2k2rmow==", + "license": "BSD-3-Clause", + "peer": true, + "engines": { + "node": "*" + } + }, "node_modules/crypto-random-string": { "version": "2.0.0", "license": "MIT", @@ -20554,6 +20574,13 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/is-buffer": { + "version": "1.1.6", + "resolved": "https://registry.npmjs.org/is-buffer/-/is-buffer-1.1.6.tgz", + "integrity": "sha512-NcdALwpXkTm5Zvvbk7owOUSvVvBKDgKP5/ewfXEznmQFfs4ZRmanOeKBTjRVjka3QFoN6XJ+9F3USqfHqTaU5w==", + "license": "MIT", + "peer": true + }, "node_modules/is-callable": { "version": "1.2.7", "license": "MIT", @@ -21851,6 +21878,32 @@ "node": "^14.15.0 || ^16.10.0 || >=18.0.0" } }, + "node_modules/jest-junit": { + "version": "16.0.0", + "resolved": "https://registry.npmjs.org/jest-junit/-/jest-junit-16.0.0.tgz", + "integrity": "sha512-A94mmw6NfJab4Fg/BlvVOUXzXgF0XIH6EmTgJ5NDPp4xoKq0Kr7sErb+4Xs9nZvu58pJojz5RFGpqnZYJTrRfQ==", + "license": "Apache-2.0", + "peer": true, + "dependencies": { + "mkdirp": "^1.0.4", + "strip-ansi": "^6.0.1", + "uuid": "^8.3.2", + "xml": "^1.0.1" + }, + "engines": { + "node": ">=10.12.0" + } + }, + "node_modules/jest-junit/node_modules/uuid": { + "version": "8.3.2", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", + "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==", + "license": "MIT", + "peer": true, + "bin": { + "uuid": "dist/bin/uuid" + } + }, "node_modules/jest-leak-detector": { "version": "29.7.0", "license": "MIT", @@ -23979,6 +24032,18 @@ "node": ">= 0.4" } }, + "node_modules/md5": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/md5/-/md5-2.3.0.tgz", + "integrity": "sha512-T1GITYmFaKuO91vxyoQMFETst+O71VUPEU3ze5GNzDm0OWdP8v1ziTaAEPUr/3kLsY3Sftgz242A1SetQiDL7g==", + "license": "BSD-3-Clause", + "peer": true, + "dependencies": { + "charenc": "0.0.2", + "crypt": "0.0.2", + "is-buffer": "~1.1.6" + } + }, "node_modules/memoizee": { "version": "0.4.15", "license": "ISC", @@ -24307,6 +24372,39 @@ "url": "https://opencollective.com/mochajs" } }, + "node_modules/mocha-junit-reporter": { + "version": "2.2.1", + "resolved": "https://registry.npmjs.org/mocha-junit-reporter/-/mocha-junit-reporter-2.2.1.tgz", + "integrity": "sha512-iDn2tlKHn8Vh8o4nCzcUVW4q7iXp7cC4EB78N0cDHIobLymyHNwe0XG8HEHHjc3hJlXm0Vy6zcrxaIhnI2fWmw==", + "license": "MIT", + "peer": true, + "dependencies": { + "debug": "^4.3.4", + "md5": "^2.3.0", + "mkdirp": "^3.0.0", + "strip-ansi": "^6.0.1", + "xml": "^1.0.1" + }, + "peerDependencies": { + "mocha": ">=2.2.5" + } + }, + "node_modules/mocha-junit-reporter/node_modules/mkdirp": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-3.0.1.tgz", + "integrity": "sha512-+NsyUUAZDmo6YVHzL/stxSu3t9YS1iljliy3BSDrXJ/dkn1KYdmtZODGGjLcc9XLgVVpH4KshHB8XmZgMhaBXg==", + "license": "MIT", + "peer": true, + "bin": { + "mkdirp": "dist/cjs/src/bin.js" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/isaacs" + } + }, "node_modules/mocha/node_modules/ansi-regex": { "version": "3.0.1", "license": "MIT", @@ -31728,6 +31826,13 @@ "node": ">=8" } }, + "node_modules/xml": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/xml/-/xml-1.0.1.tgz", + "integrity": "sha512-huCv9IH9Tcf95zuYCsQraZtWnJvBtLVE0QHMOs8bWyZAFZNDcYjsPq1nEx8jKA9y+Beo9v+7OBPRisQTjinQMw==", + "license": "MIT", + "peer": true + }, "node_modules/xml2js": { "version": "0.4.19", "license": "MIT", @@ -32463,7 +32568,8 @@ }, "peerDependencies": { "dotcom-tool-kit": "5.x", - "jest-cli": "27.x || 28.x || 29.x" + "jest-cli": "27.x || 28.x || 29.x", + "jest-junit": ">=11 <17" } }, "plugins/lint-staged": { @@ -32575,7 +32681,8 @@ }, "peerDependencies": { "dotcom-tool-kit": "5.x", - "mocha": ">=6.x <=11.x" + "mocha": ">=6.x <=11.x", + "mocha-junit-reporter": "<=3.x" } }, "plugins/monorepo": { diff --git a/plugins/jest/package.json b/plugins/jest/package.json index 2e59a0149..cfddb6321 100644 --- a/plugins/jest/package.json +++ b/plugins/jest/package.json @@ -17,7 +17,8 @@ }, "peerDependencies": { "dotcom-tool-kit": "5.x", - "jest-cli": "27.x || 28.x || 29.x" + "jest-cli": "27.x || 28.x || 29.x", + "jest-junit": ">=11 <17" }, "repository": { "type": "git", diff --git a/plugins/jest/src/tasks/jest.ts b/plugins/jest/src/tasks/jest.ts index 8369d7387..728f5bb4d 100644 --- a/plugins/jest/src/tasks/jest.ts +++ b/plugins/jest/src/tasks/jest.ts @@ -63,9 +63,14 @@ export { JestSchema as schema } export default class Jest extends Task<{ task: typeof JestSchema }> { async run({ cwd }: TaskRunContext): Promise { const args = ['--colors', this.options.configPath ? `--config=${this.options.configPath}` : ''] + const env: Record = {} if (this.options.ci) { - args.push('--ci') + args.push('--ci', '--reporters=default', '--reporters=jest-junit') + + env.JEST_JUNIT_OUTPUT_DIR = 'test-results' + env.JEST_JUNIT_ADD_FILE_ATTRIBUTE = 'true' + // only relevant if running on CircleCI, other CI environments might handle // virtualisation completely differently if (process.env.CIRCLECI) { @@ -77,7 +82,14 @@ export default class Jest extends Task<{ task: typeof JestSchema }> { } this.logger.info(`running jest ${args.join(' ')}`) - const child = fork(jestCLIPath, args, { silent: true, cwd }) + const child = fork(jestCLIPath, args, { + silent: true, + cwd, + env: { + ...env, + ...process.env + } + }) hookFork(this.logger, 'jest', child) return waitOnExit('jest', child) } diff --git a/plugins/jest/tests/jest-plugin.test.ts b/plugins/jest/tests/jest-plugin.test.ts index a6b14cbea..c5204050a 100644 --- a/plugins/jest/tests/jest-plugin.test.ts +++ b/plugins/jest/tests/jest-plugin.test.ts @@ -25,7 +25,7 @@ describe('jest plugin', () => { expect(fork).toBeCalledWith( expect.any(String), expect.not.arrayContaining([expect.stringContaining('--config')]), - { silent: true, cwd: '' } + expect.objectContaining({ silent: true, cwd: '' }) ) }) @@ -36,10 +36,10 @@ describe('jest plugin', () => { expect(fork).toBeCalledWith( expect.any(String), expect.arrayContaining(['--config=./src/jest.config.js']), - { + expect.objectContaining({ silent: true, cwd: '' - } + }) ) }) @@ -47,9 +47,29 @@ describe('jest plugin', () => { const jest = new Jest(logger, 'Jest', {}, { ci: true }) await jest.run({ command: 'test:local', cwd: '' }) - expect(fork).toBeCalledWith(expect.any(String), expect.arrayContaining(['--ci']), { - silent: true, - cwd: '' - }) + expect(fork).toBeCalledWith( + expect.any(String), + expect.arrayContaining(['--ci']), + expect.objectContaining({ + silent: true, + cwd: '' + }) + ) + }) + + it('should call jest cli with reporter options if ci is passed in', async () => { + const jest = new Jest(logger, 'Jest', {}, { ci: true }) + await jest.run({ command: 'test:local', cwd: '' }) + + expect(fork).toBeCalledWith( + expect.any(String), + expect.arrayContaining(['--reporters=default', '--reporters=jest-junit']), + expect.objectContaining({ + env: expect.objectContaining({ + JEST_JUNIT_OUTPUT_DIR: 'test-results', + JEST_JUNIT_ADD_FILE_ATTRIBUTE: 'true' + }) + }) + ) }) }) diff --git a/plugins/mocha/.toolkitrc.yml b/plugins/mocha/.toolkitrc.yml index 0c788aeb6..b0fe35de7 100644 --- a/plugins/mocha/.toolkitrc.yml +++ b/plugins/mocha/.toolkitrc.yml @@ -3,6 +3,8 @@ tasks: commands: 'test:local': Mocha - 'test:ci': Mocha + 'test:ci': + Mocha: + ci: true version: 2 diff --git a/plugins/mocha/package.json b/plugins/mocha/package.json index 5eeafd789..e3a142455 100644 --- a/plugins/mocha/package.json +++ b/plugins/mocha/package.json @@ -39,7 +39,8 @@ }, "peerDependencies": { "dotcom-tool-kit": "5.x", - "mocha": ">=6.x <=11.x" + "mocha": ">=6.x <=11.x", + "mocha-junit-reporter": "<=3.x" }, "engines": { "node": ">=20.x" diff --git a/plugins/mocha/readme.md b/plugins/mocha/readme.md index db12323cf..dba9c0bba 100644 --- a/plugins/mocha/readme.md +++ b/plugins/mocha/readme.md @@ -29,6 +29,7 @@ Runs `mocha` to execute tests. | :----------- | :---------------------------------------------------------------------------------------------------------------------------------------------------------------- | :------- | :--------------- | | `files` | A file path glob to Mocha tests. | `string` | `'test/**/*.js'` | | `configPath` | Path to the [Mocha config file](https://mochajs.org/#configuring-mocha-nodejs). Uses Mocha's own [config resolution](https://mochajs.org/#priorities) by default. | `string` | | +| `ci` | Set to `true` to capture JUnit test results. | `true` | | _All properties are optional._ diff --git a/plugins/mocha/src/tasks/mocha.ts b/plugins/mocha/src/tasks/mocha.ts index f15f607e3..0f6c5b2da 100644 --- a/plugins/mocha/src/tasks/mocha.ts +++ b/plugins/mocha/src/tasks/mocha.ts @@ -13,7 +13,8 @@ const MochaSchema = z .optional() .describe( "Path to the [Mocha config file](https://mochajs.org/#configuring-mocha-nodejs). Uses Mocha's own [config resolution](https://mochajs.org/#priorities) by default." - ) + ), + ci: z.literal(true).optional().describe('Set to `true` to capture JUnit test results.') }) .describe('Runs `mocha` to execute tests.') export { MochaSchema as schema } @@ -23,9 +24,15 @@ export default class Mocha extends Task<{ task: typeof MochaSchema }> { const files = await glob(this.options.files, { cwd }) const args = ['--color', ...files] + if (this.options.configPath) { args.unshift(`--config=${this.options.configPath}`) } + + if (this.options.ci) { + args.push('--reporter mocha-junit-reporter', '--reporter-options mochaFile=./test-results/junit.xml') + } + this.logger.info(`running mocha ${args.join(' ')}`) const child = fork(mochaCLIPath, args, { silent: true, cwd }) hookFork(this.logger, 'mocha', child)