Skip to content

Commit

Permalink
feat: add --expose-gc flag (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
anurag-roy authored Jan 2, 2024
1 parent e3853f8 commit 87be751
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
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')
}))
})

0 comments on commit 87be751

Please sign in to comment.