Commit 1ae78a48 authored by Fred Chasen's avatar Fred Chasen

Merge branch 'issue-157-log-specs' into 'master'

Improve the log format in specs

Closes #157

See merge request tools/pagedjs!80
parents f692426f e0d48b41
const chalk = require('chalk');
const NodeEnvironment = require('jest-environment-node');
const puppeteer = require('puppeteer');
const fs = require('fs');
const os = require('os');
const path = require('path');
const chalk = require("chalk");
const NodeEnvironment = require("jest-environment-node");
const puppeteer = require("puppeteer");
const fs = require("fs");
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 {
constructor(config) {
......@@ -13,15 +11,15 @@ class PuppeteerEnvironment extends NodeEnvironment {
}
async setup() {
DEBUG && console.log(chalk.yellow('Setup Test Environment.'));
await super.setup()
const wsEndpoint = fs.readFileSync(WS_ENDPOINT_PATH, 'utf8');
DEBUG && console.log(chalk.yellow("Setup Test Environment."));
await super.setup();
const wsEndpoint = fs.readFileSync(WS_ENDPOINT_PATH, "utf8");
if (!wsEndpoint) {
throw new Error('wsEndpoint not found');
throw new Error("wsEndpoint not found");
}
this.global.browser = await puppeteer.connect({
browserWSEndpoint: wsEndpoint,
})
});
this.global.loadPage = this.loadPage.bind(this);
......@@ -31,7 +29,7 @@ class PuppeteerEnvironment extends NodeEnvironment {
}
async teardown() {
DEBUG && console.log(chalk.yellow('Teardown Test Environment.'));
DEBUG && console.log(chalk.yellow("Teardown Test Environment."));
await super.teardown();
}
......@@ -51,27 +49,41 @@ class PuppeteerEnvironment extends NodeEnvironment {
renderedReject = reject;
});
page.on('pageerror', (error) => {
page.on("pageerror", (error) => {
this.handleError(error);
renderedReject(error);
});
page.on('error', (error) => {
page.on("error", (error) => {
this.handleError(error);
renderedReject(error);
});
page.on('requestfailed', (error) => {
page.on("requestfailed", (error) => {
this.handleError(error);
renderedReject(error);
});
page.on('console', (msg) => {
for (let i = 0; i < msg.args().length; ++i)
console.log(`TestPage - ${i}: ${msg.args()[i]}`);
page.on("console", async (msg) => {
const args = [];
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);
});
......@@ -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;
}
......
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