Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
288 changes: 240 additions & 48 deletions core/cli/test/__snapshots__/config.test.ts.snap

Large diffs are not rendered by default.

111 changes: 109 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion plugins/jest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
9 changes: 7 additions & 2 deletions plugins/jest/src/tasks/jest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,14 @@ export { JestSchema as schema }
export default class Jest extends Task<{ task: typeof JestSchema }> {
async run({ cwd }: TaskRunContext): Promise<void> {
const args = ['--colors', this.options.configPath ? `--config=${this.options.configPath}` : '']
const env: Record<string, string> = {}

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) {
Expand All @@ -77,7 +82,7 @@ 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 })
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: should env be merged with process.env?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh yep good point, i forgot fork defaulted to that

hookFork(this.logger, 'jest', child)
return waitOnExit('jest', child)
}
Expand Down
25 changes: 20 additions & 5 deletions plugins/jest/tests/jest-plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: '' })
)
})

Expand All @@ -36,20 +36,35 @@ describe('jest plugin', () => {
expect(fork).toBeCalledWith(
expect.any(String),
expect.arrayContaining(['--config=./src/jest.config.js']),
{
expect.objectContaining({
silent: true,
cwd: ''
}
})
)
})

it('should call jest cli with ci 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(['--ci']), {
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: {
JEST_JUNIT_OUTPUT_DIR: 'test-results',
JEST_JUNIT_ADD_FILE_ATTRIBUTE: 'true'
}
}))
})
})
4 changes: 3 additions & 1 deletion plugins/mocha/.toolkitrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ tasks:

commands:
'test:local': Mocha
'test:ci': Mocha
'test:ci':
Mocha:
ci: true

version: 2
3 changes: 2 additions & 1 deletion plugins/mocha/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions plugins/mocha/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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._
<!-- end autogenerated docs -->
Expand Down
9 changes: 8 additions & 1 deletion plugins/mocha/src/tasks/mocha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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)
Expand Down