Skip to content

Commit d821f5b

Browse files
fix: ensure journey/end is written to fd (#447)
* fix: ensure journey/end is written to fd * add comment about test
1 parent 201f7da commit d821f5b

File tree

8 files changed

+33
-70
lines changed

8 files changed

+33
-70
lines changed

__tests__/core/logger.test.ts

Lines changed: 0 additions & 46 deletions
This file was deleted.

__tests__/core/runner.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,4 +690,26 @@ describe('runner', () => {
690690
const collectOrder = events.map(event => event.type);
691691
expect(collectOrder).toEqual(realEventsOrder);
692692
});
693+
694+
/**
695+
* TODO: Move this as part of integration test
696+
* Its really hard to ensure the journey/end is called for a real world page
697+
* without actually testing on a real world webpage.
698+
*/
699+
it('run - ensure journey/end is written for real world pages', async () => {
700+
const j1 = journey('journey1', async ({ page }) => {
701+
step('load homepage', async () => {
702+
await page.goto('https://www.elastic.co');
703+
});
704+
});
705+
runner.addJourney(j1);
706+
await runner.run({
707+
reporter: 'json',
708+
network: true,
709+
trace: true,
710+
outfd: fs.openSync(dest, 'w'),
711+
});
712+
const events = readAndCloseStreamJson().map(event => event.type);
713+
expect(events[events.length - 1]).toBe('journey/end');
714+
});
693715
});

__tests__/reporters/base.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ describe('base reporter', () => {
5757
});
5858

5959
it('writes each step to the FD', async () => {
60-
const { stream } = new BaseReporter(runner, { fd: fs.openSync(dest, 'w') });
6160
runner.emit('start', { numJourneys: 1 });
6261
runner.emit('journey:start', {
6362
journey: j1,

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"playwright-chromium": "=1.14.0",
4949
"sharp": "^0.29.3",
5050
"snakecase-keys": "^3.2.1",
51-
"sonic-boom": "^2.1.0",
51+
"sonic-boom": "^2.6.0",
5252
"source-map-support": "^0.5.19",
5353
"ts-node": "^10.2.1",
5454
"typescript": "^4.3.5"

src/core/logger.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,9 @@
2323
*
2424
*/
2525

26-
import SonicBoom from 'sonic-boom';
2726
import { grey, cyan, dim, italic } from 'kleur/colors';
2827
import { now } from '../helpers';
2928

30-
const defaultFd = process.stdout.fd;
31-
let logger = new SonicBoom({ fd: defaultFd, sync: true });
32-
33-
export const setLogger = (fd: number) => {
34-
if (fd && fd !== defaultFd) {
35-
logger = new SonicBoom({ fd });
36-
}
37-
return logger;
38-
};
39-
4029
export function log(msg) {
4130
if (!process.env.DEBUG || !msg) {
4231
return;
@@ -45,5 +34,5 @@ export function log(msg) {
4534
msg = JSON.stringify(msg);
4635
}
4736
const time = dim(cyan(`at ${parseInt(String(now()))} ms `));
48-
logger.write(time + italic(grey(msg)) + '\n');
37+
console.log(time + italic(grey(msg)) + '\n');
4938
}

src/index.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
import { runner } from './core';
2727
import { RunOptions } from './core/runner';
28-
import { setLogger } from './core/logger';
2928
import sourceMapSupport from 'source-map-support';
3029

3130
export async function run(options: RunOptions) {
@@ -35,11 +34,6 @@ export async function run(options: RunOptions) {
3534
sourceMapSupport.install({
3635
environment: 'node',
3736
});
38-
/**
39-
* set up logger with appropriate file descriptor
40-
* to capture all the DEBUG logs when run through heartbeat
41-
*/
42-
setLogger(options.outfd);
4337

4438
try {
4539
return await runner.run(options);

src/reporters/base.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,12 @@ export default class BaseReporter {
6464

6565
constructor(public runner: Runner, options: ReporterOptions = {}) {
6666
this.fd = options.fd || process.stdout.fd;
67-
this.stream = new SonicBoom({ fd: this.fd, sync: true });
67+
/**
68+
* minLength is set to 1 byte to make sure we flush the
69+
* content even if its the last byte on the stream buffer
70+
* before destroying the pipe with underlying file descriptor
71+
*/
72+
this.stream = new SonicBoom({ fd: this.fd, sync: true, minLength: 1 });
6873
this._registerListeners();
6974
this.runner.on('end', () => this.stream.flush());
7075
}

0 commit comments

Comments
 (0)