Commit e0d48b41 authored by Guillaume's avatar Guillaume

Improve the log format in specs

parent b09521b1
Pipeline #344 passed with stage
in 4 minutes and 53 seconds
const chalk = require('chalk'); const chalk = require("chalk");
const NodeEnvironment = require('jest-environment-node'); const NodeEnvironment = require("jest-environment-node");
const puppeteer = require('puppeteer'); const puppeteer = require("puppeteer");
const fs = require('fs'); const fs = require("fs");
const os = require('os');
const path = require('path');
const { WS_ENDPOINT_PATH, DIR, DEBUG, ORIGIN, PDF_SETTINGS } = require('./constants'); const { WS_ENDPOINT_PATH, DEBUG, ORIGIN, PDF_SETTINGS } = require("./constants");
class PuppeteerEnvironment extends NodeEnvironment { class PuppeteerEnvironment extends NodeEnvironment {
constructor(config) { constructor(config) {
...@@ -13,15 +11,15 @@ class PuppeteerEnvironment extends NodeEnvironment { ...@@ -13,15 +11,15 @@ class PuppeteerEnvironment extends NodeEnvironment {
} }
async setup() { async setup() {
DEBUG && console.log(chalk.yellow('Setup Test Environment.')); DEBUG && console.log(chalk.yellow("Setup Test Environment."));
await super.setup() await super.setup();
const wsEndpoint = fs.readFileSync(WS_ENDPOINT_PATH, 'utf8'); const wsEndpoint = fs.readFileSync(WS_ENDPOINT_PATH, "utf8");
if (!wsEndpoint) { if (!wsEndpoint) {
throw new Error('wsEndpoint not found'); throw new Error("wsEndpoint not found");
} }
this.global.browser = await puppeteer.connect({ this.global.browser = await puppeteer.connect({
browserWSEndpoint: wsEndpoint, browserWSEndpoint: wsEndpoint,
}) });
this.global.loadPage = this.loadPage.bind(this); this.global.loadPage = this.loadPage.bind(this);
...@@ -31,7 +29,7 @@ class PuppeteerEnvironment extends NodeEnvironment { ...@@ -31,7 +29,7 @@ class PuppeteerEnvironment extends NodeEnvironment {
} }
async teardown() { async teardown() {
DEBUG && console.log(chalk.yellow('Teardown Test Environment.')); DEBUG && console.log(chalk.yellow("Teardown Test Environment."));
await super.teardown(); await super.teardown();
} }
...@@ -51,27 +49,41 @@ class PuppeteerEnvironment extends NodeEnvironment { ...@@ -51,27 +49,41 @@ class PuppeteerEnvironment extends NodeEnvironment {
renderedReject = reject; renderedReject = reject;
}); });
page.on('pageerror', (error) => { page.on("pageerror", (error) => {
this.handleError(error); this.handleError(error);
renderedReject(error); renderedReject(error);
}); });
page.on('error', (error) => { page.on("error", (error) => {
this.handleError(error); this.handleError(error);
renderedReject(error); renderedReject(error);
}); });
page.on('requestfailed', (error) => { page.on("requestfailed", (error) => {
this.handleError(error); this.handleError(error);
renderedReject(error); renderedReject(error);
}); });
page.on('console', (msg) => { page.on("console", async (msg) => {
for (let i = 0; i < msg.args().length; ++i) const args = [];
console.log(`TestPage - ${i}: ${msg.args()[i]}`); for (let i = 0; i < msg.args().length; ++i) {
args.push(await msg.args()[i].jsonValue());
}
if (args.length > 0) {
// preprend the path
args[0] = `${path}: ${args[0]}`;
}
const type = msg.type();
let log;
if (type === "warning") {
log = console.warn;
} else {
log = console[msg.type()];
}
log.apply(this, args);
}); });
await page.exposeFunction('onRendered', (msg, width, height, orientation) => { await page.exposeFunction("onRendered", (msg, width, height, orientation) => {
renderedResolve(msg, width, height, orientation); renderedResolve(msg, width, height, orientation);
}); });
...@@ -84,7 +96,7 @@ class PuppeteerEnvironment extends NodeEnvironment { ...@@ -84,7 +96,7 @@ class PuppeteerEnvironment extends NodeEnvironment {
}); });
}); });
await page.goto(ORIGIN + '/specs/' + path, { waitUntil: 'networkidle2' }); await page.goto(ORIGIN + "/specs/" + path, { waitUntil: "networkidle2" });
return page; return page;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment