Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add --expose-gc flag #4

Merged
merged 5 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ Note the use of `incremental: true`, which speed up compilation massively.
* `--timeout` or `-t`, timeouts the tests after a given time; default is 30000 ms
* `--coverage-exclude` or `-X`, a list of comma-separated patterns to exclude from the coverage report. All tests files are ignored by default.
* `--ignore` or `-i`, ignore a glob pattern, and not look for tests there
* `--expose-gc`, exposes the gc() function to tests
* `--pattern` or `-p`, run tests matching the given glob pattern

## License
Expand Down
18 changes: 17 additions & 1 deletion borp.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import posix from 'node:path/posix'
import runWithTypeScript from './lib/run.js'
import { Report } from 'c8'
import os from 'node:os'
import { execa } from 'execa'

let reporter
/* c8 ignore next 4 */
Expand All @@ -30,17 +31,32 @@ const args = parseArgs({
timeout: { type: 'string', short: 't', default: '30000' },
'coverage-exclude': { type: 'string', short: 'X', multiple: true },
ignore: { type: 'string', short: 'i', multiple: true },
'expose-gc': { type: 'boolean' },
help: { type: 'boolean', short: 'h' }
},
allowPositionals: true
})

/* c8 ignore next 4 */
/* c8 ignore next 5 */
if (args.values.help) {
console.log(await readFile(new URL('./README.md', import.meta.url), 'utf8'))
process.exit(0)
}

if (args.values['expose-gc'] && typeof global.gc !== 'function') {
try {
await execa('node', ['--expose-gc', ...process.argv.slice(1)], {
stdio: 'inherit',
env: {
...process.env
}
})
process.exit(0)
} catch (error) {
process.exit(1)
}
}

if (args.values.concurrency) {
args.values.concurrency = parseInt(args.values.concurrency)
}
Expand Down
8 changes: 8 additions & 0 deletions fixtures/gc/gc.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { doesNotThrow } from 'node:assert'
import { test } from 'node:test'

test('this needs gc', () => {
doesNotThrow(() => {
global.gc()
})
})
19 changes: 19 additions & 0 deletions test/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,22 @@ test('failing test set correct status code', async () => {
cwd: join(import.meta.url, '..', 'fixtures', 'fails')
}))
})

test('--expose-gc flag enables garbage collection in tests', async () => {
await execa('node', [
borp,
'--expose-gc'
], {
cwd: join(import.meta.url, '..', 'fixtures', 'gc')
})
})

test('failing test with --expose-gc flag sets correct status code', async () => {
// execa rejects if status code is not 0
await rejects(execa('node', [
borp,
'--expose-gc'
], {
cwd: join(import.meta.url, '..', 'fixtures', 'fails')
}))
})
Loading