Skip to content

Commit

Permalink
add types and change webp
Browse files Browse the repository at this point in the history
  • Loading branch information
vigneshshanmugam committed May 26, 2021
1 parent 4664d91 commit 69affc1
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 31 deletions.
10 changes: 3 additions & 7 deletions __tests__/core/runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ describe('runner', () => {
await page.goto(server.TEST_PAGE);
});
});
const runOptions = { metrics: true, screenshots: true };
const runOptions = { metrics: true };
const context = await Runner.createContext(runOptions);
await runner.registerJourney(j1, context);
const result = await runner.runSteps(j1, context, runOptions);
Expand All @@ -162,7 +162,6 @@ describe('runner', () => {
{
status: 'succeeded',
metrics: expect.any(Object),
screenshot: expect.any(String),
url: server.TEST_PAGE,
},
]);
Expand All @@ -174,7 +173,7 @@ describe('runner', () => {
await (page as any).clickkkkkk();
});
});
const runOptions = { screenshots: true };
const runOptions = {};
const context = await Runner.createContext(runOptions);
await runner.registerJourney(j1, context);
const result = await runner.runSteps(j1, context, runOptions);
Expand All @@ -184,7 +183,6 @@ describe('runner', () => {
status: 'failed',
url: 'about:blank',
error: expect.any(Error),
screenshot: expect.any(String),
},
]);
});
Expand Down Expand Up @@ -238,21 +236,19 @@ describe('runner', () => {
throw error;
});
});
const runOptions = { screenshots: true };
const runOptions = {};
const context = await Runner.createContext(runOptions);
await runner.registerJourney(j1, context);
const [step1, step2] = await runner.runSteps(j1, context, runOptions);
await Gatherer.stop();
expect(step1).toEqual({
status: 'succeeded',
url: server.TEST_PAGE,
screenshot: expect.any(String),
});
expect(step2).toEqual({
status: 'failed',
url: server.TEST_PAGE,
error,
screenshot: expect.any(String),
});
});

Expand Down
9 changes: 9 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"devDependencies": {
"@types/jest": "^26.0.19",
"@types/node": "^14.14.14",
"@types/sharp": "^0.28.2",
"@typescript-eslint/eslint-plugin": "^3.10.1",
"@typescript-eslint/parser": "^3.10.1",
"eslint": "^7.15.0",
Expand Down
3 changes: 2 additions & 1 deletion src/core/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ export default class Runner {
? reporter
: reporters[reporter] || reporters['default'];
new Reporter(this, { fd: outfd });
this.emit('start', { numJourneys: this.journeys.length });
/**
* Set up the directory for caching screenshots
*/
Expand Down Expand Up @@ -377,10 +378,10 @@ export default class Runner {
* Clear all cache data stored for post processing by
* the current synthetic agent run
*/
// this.emit('end', {});
rmdirSync(CACHE_PATH, { recursive: true });
this.currentJourney = null;
this.journeys = [];
this.active = false;
this.emit('end', {});
}
}
39 changes: 20 additions & 19 deletions src/reporters/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,27 +66,28 @@ export default class BaseReporter {
this.fd = options.fd || process.stdout.fd;
this.stream = new SonicBoom({ fd: this.fd, sync: true });
this._registerListeners();
this.runner.on('end', () => this.close());
this.runner.on('end', () => this.stream.flush());
}

close() {
if (this.fd <= 2) {
/**
* For stdout/stderr we destroy stream once data is written and run
* it as the last listener giving enough room for
* other reporters to write to stream
*/
setImmediate(() => this.stream.end());
} else {
/**
* If the user has passed a custom FD we don't close the FD, but we do flush it
* to give them more control. This is important because FDs should only be closed
* once, and the primary use case for custom FDs is being called by heartbeat, which
* closes the FD after the process exits
*/
this.stream.flush();
}
}
// close() {
// if (this.fd <= 2) {
// /**
// * For stdout/stderr we destroy stream once data is written and run
// * it as the last listener giving enough room for
// * other reporters to write to stream
// */
// // setImmediate(() => this.stream.end());
// this.stream.flush();
// } else {
// /**
// * If the user has passed a custom FD we don't close the FD, but we do flush it
// * to give them more control. This is important because FDs should only be closed
// * once, and the primary use case for custom FDs is being called by heartbeat, which
// * closes the FD after the process exits
// */
// this.stream.flush();
// }
// }

_registerListeners() {
const result = {
Expand Down
9 changes: 5 additions & 4 deletions src/reporters/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ async function processScreenshot(screenshot: Buffer) {
const left = col * blockWidth;
const buf = await img
.extract({ top, left, width: blockWidth, height: blockHeight })
.toFormat(sharp.format.webp)
.webp({ quality: 80 })
.toBuffer();

const hash = createHash('sha1').update(buf).digest('hex');
Expand All @@ -298,8 +298,7 @@ async function processScreenshot(screenshot: Buffer) {
return { blocks, reference, blob_mime: 'image/webp' };
}

async function gatherScreenshots() {
const screenshotsPath = join(CACHE_PATH, 'screenshots');
async function gatherScreenshots(screenshotsPath: string) {
const screenshots: Array<ScreenshotOutput> = [];
await totalist(screenshotsPath, async (_, absPath) => {
const content = readFileSync(absPath, 'utf8');
Expand Down Expand Up @@ -377,7 +376,9 @@ export default class JSONReporter extends BaseReporter {
status,
error,
}) => {
const screenshots = await gatherScreenshots();
const screenshots = await gatherScreenshots(
join(CACHE_PATH, 'screenshots')
);
if (screenshots.length > 0) {
screenshots.forEach(({ blocks, reference, step, blob_mime }) => {
for (let i = 0; i < blocks.length; i++) {
Expand Down

0 comments on commit 69affc1

Please sign in to comment.