Skip to content

Commit

Permalink
feat: add process.exit (#542)
Browse files Browse the repository at this point in the history
* feat: add process.exit

Allows ending and failing run with process.exit(0) or process.exit(233).
  • Loading branch information
hugomrdias authored May 12, 2023
1 parent b71bf98 commit 2685431
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 51 deletions.
1 change: 1 addition & 0 deletions mocks/test.mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const { good, bad } = require('./lib')
describe('Array', () => {
describe('#indexOf()', () => {
it('should return -1 when the value is not present', () => {
console.log(process)
is([1, 2, 3].indexOf(4), -1)
})

Expand Down
37 changes: 14 additions & 23 deletions mocks/tinybench2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,20 @@ import { Bench } from 'tinybench'

const bench = new Bench({ time: 100 })

bench
.add('switch 1', () => {
let a = 1
let b = 2
const c = a
a = b
b = c
})
.add('switch 2', () => {
let a = 1
let b = 10
a = b + a
b = a - b
a = b - a
})
async function run() {
bench
.add('faster task', () => {
console.log('I am faster')
})
.add('slower task', async () => {
await new Promise((r) => setTimeout(r, 500)) // we wait 1ms :)
console.log('I am slower')
})

await bench.run()
await bench.run()
}

await run()

console.table(
bench.tasks.map(({ name, result }) => ({
Expand All @@ -28,10 +25,4 @@ console.table(
}))
)

// Output:
// ┌─────────┬────────────┬────────────────────┬────────────────────┐
// │ (index) │ Task Name │ Average Time (ps) │ Variance (ps) │
// ├─────────┼────────────┼────────────────────┼────────────────────┤
// │ 0 │ 'switch 1' │ 1.8458325710527104 │ 1.2113875253341617 │
// │ 1 │ 'switch 2' │ 1.8746935152109603 │ 1.2254725890767446 │
// └─────────┴────────────┴────────────────────┴────────────────────┘
process.exit(0)
11 changes: 11 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,17 @@ Description
$ playwright-test test/** BAD
```
## Flow control
All test runners support automatic flow control, which means you don't need to call special function or trigger any event in your tests to stop the run. The `none` runner does not support flow control.
To manually stop the run you can use `process.exit`:
```js
process.exit(0) // stops the run and exits with success
process.exit(1) // stops the run and exits with failure
```
## Config
> The config file needs to be commonjs for now, so if your package is pure ESM you need to use `.cjs` extension.
Expand Down
49 changes: 22 additions & 27 deletions src/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,35 +292,30 @@ export class Runner {

// run tests
if (!this.options.debug) {
if (this.type === 'none') {
// exit
await this.stop(false)
} else {
// wait for the tests
await page.waitForFunction(
// @ts-ignore
() => self.PW_TEST.ended === true,
undefined,
{
timeout: 0,
polling: 100, // need to be polling raf doesnt work in extensions
}
)
const testsFailed = await page.evaluate('self.PW_TEST.failed')

// coverage
if (this.options.cov && page.coverage) {
await createCov(
this,
await page.coverage.stopJSCoverage(),
outName,
this.options.reportDir
)
// wait for the tests
await page.waitForFunction(
// @ts-ignore
() => self.PW_TEST.ended === true,
undefined,
{
timeout: 0,
polling: 100, // need to be polling raf doesnt work in extensions
}

// exit
await this.stop(testsFailed)
)
const testsFailed = await page.evaluate('self.PW_TEST.failed')

// coverage
if (this.options.cov && page.coverage) {
await createCov(
this,
await page.coverage.stopJSCoverage(),
outName,
this.options.reportDir
)
}

// exit
await this.stop(testsFailed)
}
} catch (/** @type {any} */ error) {
spinner.fail('Running tests failed.')
Expand Down
16 changes: 15 additions & 1 deletion src/utils/inject-process.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// @ts-nocheck
export { default as process } from 'process/browser'
// eslint-disable-next-line unicorn/prefer-module
const _process = require('process/browser')

const p = {
..._process,
exit: (code = 0) => {
if (code === 0) {
globalThis.PW_TEST.end(false)
} else {
globalThis.PW_TEST.end(true)
}
},
}

export const process = p
// https://github.com/ionic-team/rollup-plugin-node-polyfills

0 comments on commit 2685431

Please sign in to comment.