Commit 7233c8be authored by Fred Chasen's avatar Fred Chasen

Add PDF Snapshot

parent dcf4ca9b
......@@ -2,4 +2,5 @@ module.exports = {
globalSetup: './jest/setup.js',
globalTeardown: './jest/teardown.js',
testEnvironment: './jest/puppeteer_environment.js',
setupTestFrameworkScriptFile: './jest/setup_tests.js'
}
......@@ -8,11 +8,18 @@ const PORT = 9999;
const ORIGIN = `http://localhost:${PORT}`;
const CI = process.env.CI === 'true';
const PDF_SETTINGS = {
printBackground: true,
displayHeaderFooter: false,
margin: { top: 0, right: 0, bottom: 0, left: 0 }
};
module.exports = {
DIR,
WS_ENDPOINT_PATH,
DEBUG,
PORT,
ORIGIN,
CI
CI,
PDF_SETTINGS
}
const { toMatchImageSnapshot } = require('jest-image-snapshot');
const path = require('path');
const gs = require('ghostscript4js')
const fs = require('fs');
const rimraf = require('rimraf');
const { DEBUG } = require('./constants');
function toMatchPDFSnapshot(received, page=1) {
let pdfImage;
let dirname = path.dirname(this.testPath);
let pdfPath = path.join(dirname, './output.pdf');
let imagePath = path.join(dirname, './output.png');
fs.writeFileSync(pdfPath, received);
try {
// create image
gs.executeSync(`-dFirstPage=${page} -dLastPage=${page} -sDEVICE=pngalpha -o ${imagePath} -sDEVICE=pngalpha -r144 ${pdfPath}`)
// load image
pdfImage = fs.readFileSync(imagePath);
// remove output
if (!DEBUG) {
rimraf.sync(imagePath);
rimraf.sync(pdfPath);
}
} catch (err) {
throw err
}
return toMatchImageSnapshot.apply(this, [pdfImage])
}
module.exports = toMatchPDFSnapshot;
......@@ -5,7 +5,7 @@ const fs = require('fs');
const os = require('os');
const path = require('path');
const { WS_ENDPOINT_PATH, DIR, DEBUG, ORIGIN } = require('./constants');
const { WS_ENDPOINT_PATH, DIR, DEBUG, ORIGIN, PDF_SETTINGS } = require('./constants');
class PuppeteerEnvironment extends NodeEnvironment {
constructor(config) {
......@@ -22,8 +22,12 @@ class PuppeteerEnvironment extends NodeEnvironment {
this.global.browser = await puppeteer.connect({
browserWSEndpoint: wsEndpoint,
})
this.global.origin = ORIGIN;
this.global.loadPage = this.loadPage.bind(this);
this.global.ORIGIN = ORIGIN;
this.global.DEBUG = DEBUG;
this.global.PDF_SETTINGS = PDF_SETTINGS;
}
async teardown() {
......@@ -34,6 +38,30 @@ class PuppeteerEnvironment extends NodeEnvironment {
runScript(script) {
return super.runScript(script);
}
handleError(error) {
console.error(error);
}
async loadPage(path) {
let page = await this.global.browser.newPage();
page.addListener('pageerror', this.handleError);
page.addListener('error', this.handleError);
let renderedResolve, renderedReject;
page.rendered = new Promise(function(resolve, reject) {
renderedResolve = resolve;
renderedReject = reject;
});
await page.exposeFunction('onPagesRendered', (msg, width, height, orientation) => {
renderedResolve(msg, width, height, orientation);
});
await page.goto(ORIGIN + '/tests/specs/' + path, { waitUntil: 'networkidle2' });
return page;
}
}
module.exports = PuppeteerEnvironment;
const { toMatchImageSnapshot } = require('jest-image-snapshot');
const toMatchPDFSnapshot = require('./pdf_snapshot.js');
expect.extend({ toMatchImageSnapshot, toMatchPDFSnapshot });
......@@ -23,7 +23,7 @@
<body>
<section class="body-rw Chapter-rw" epub:type="bodymatter chapter">
<header>
<h1><span class="audio" id="c001s0000">Chapter 2. Loomings.</span></h1></header>
<h1><span class="audio" id="c001s0000">Chapter 1. Loomings.</span></h1></header>
<p><span class="audio" id="c001s0001">Call me Ishmael.</span> <span class="audio" id="c001s0002">Some years ago—never mind how long precisely—having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world.</span> <span class="audio" id="c001s0003">It is a way I have of driving off the spleen and regulating the circulation.</span> <span class="audio" id="c001s0004">Whenever I find myself growing grim about the mouth; whenever it is a damp, drizzly November in my soul; whenever I find myself involuntarily pausing before coffin warehouses, and bringing up the rear of every funeral I meet; and especially whenever my hypos get such an upper hand of me, that it requires a strong moral principle to prevent me from deliberately stepping into the street, and methodically knocking people’s hats off—then, I account it high time to get to sea as soon as I can.</span> <span class="audio" id="c001s0005">This is my substitute for pistol and ball.</span> <span class="audio" id="c001s0006">With a philosophical flourish Cato throws himself upon his sword; I quietly take to the ship.</span> <span class="audio" id="c001s0007">There is nothing surprising in this.</span> <span class="audio" id="c001s0008">If they but knew it, almost all men in their degree, some time or other, cherish very nearly the same feelings towards the ocean with me.</span></p>
......
const path = require('path');
const gs = require('ghostscript4js')
const fs = require('fs');
const { toMatchImageSnapshot } = require('jest-image-snapshot');
const rimraf = require('rimraf');
const timeout = 10000;
const handleError = (error) => {
console.error(error);
};
const TIMEOUT = 10000; // Some book might take longer than this to renderer
describe('break', async () => {
let page;
let rendered;
beforeAll(async () => {
expect.extend({ toMatchImageSnapshot });
page = await browser.newPage()
page.addListener('pageerror', handleError);
page.addListener('error', handleError);
let renderedResolve;
rendered = new Promise(function(resolve) {
renderedResolve = resolve;
});
await page.exposeFunction('onPagesRendered', (msg, width, height, orientation) => {
renderedResolve(msg, width, height, orientation);
});
await page.goto(global.origin+'/tests/specs/default/default.html', { waitUntil: 'networkidle2' });
return rendered;
}, timeout)
page = await loadPage('default/default.html')
return page.rendered;
}, TIMEOUT)
afterAll(async () => {
if (!DEBUG) {
......@@ -47,41 +25,9 @@ describe('break', async () => {
})
it('should create a pdf', async () => {
let outputPath = path.join(__dirname, './output.pdf');
let pdf = await page.pdf({
path: outputPath,
printBackground: true,
displayHeaderFooter: false,
margin: {
top: 0,
right: 0,
bottom: 0,
left: 0,
}
// format: 'A4'
}).catch((e) => {
console.error(e);
});
// Output an image from the pdf
let pdfImage;
try {
// create image
let imagePath = path.join(__dirname, './output.png');
let page = 1;
gs.executeSync(`-dFirstPage=${page} -dLastPage=${page} -sDEVICE=pngalpha -o ${imagePath} -sDEVICE=pngalpha -r144 ${outputPath}`)
// load image
pdfImage = fs.readFileSync(imagePath);
// remove output
if (!DEBUG) {
rimraf.sync(imagePath);
rimraf.sync(outputPath);
}
} catch (err) {
throw err
}
let pdf = await page.pdf(PDF_SETTINGS);
expect(pdfImage).toMatchImageSnapshot();
expect(pdf).toMatchPDFSnapshot(1);
})
}
)
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