Skip to content

Commit

Permalink
Make the reference image assertions more flexible, fix error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
papandreou committed Mar 28, 2021
1 parent 281671c commit c2daad8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 19 deletions.
35 changes: 20 additions & 15 deletions test/expect.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const expect = require('unexpected')
.clone()
.use(require('unexpected-sinon'))
.use(require('unexpected-resemble'))
.use(require('unexpected-check'))
.use(require('magicpen-prism'));
const subsetFonts = require('../lib/subsetFonts');
const pathModule = require('path');
const AssetGraph = require('assetgraph');
const sinon = require('sinon');

let browser;
async function getBrowser() {
Expand All @@ -19,7 +21,7 @@ async function getBrowser() {
return browser;
}

async function screenshot(browser, assetGraph, bannedUrls) {
async function screenshot(browser, assetGraph, fileName, bannedUrls) {
const page = await browser.newPage();
await page.setRequestInterception(true);
const loadedUrls = [];
Expand All @@ -46,7 +48,7 @@ async function screenshot(browser, assetGraph, bannedUrls) {
}
request.continue();
});
await page.goto('https://example.com/');
await page.goto(`https://example.com/${fileName}`);
if (bannedUrls) {
const loadedBannedUrls = loadedUrls.filter((url) =>
bannedUrls.includes(url)
Expand All @@ -66,24 +68,26 @@ async function screenshot(browser, assetGraph, bannedUrls) {

expect.addAssertion(
'<string> to render the same after subsetting <object?>',
(expect, path, ...rest) => {
async (expect, fileName, options = {}) => {
const assetGraph = new AssetGraph({
root: pathModule.resolve(
__dirname,
'..',
'testdata',
'referenceImages',
path
),
root: pathModule.dirname(fileName),
});
return expect(assetGraph, 'to render the same after subsetting', ...rest);
const warnSpy = sinon.spy();
assetGraph.on('warn', warnSpy);
await expect(
assetGraph,
'to render the same after subsetting',
options,
pathModule.basename(fileName)
);
expect(warnSpy, 'was not called');
}
);

expect.addAssertion(
'<object> to render the same after subsetting <object?>',
async (expect, assetGraph, options = {}) => {
const [htmlAsset] = await assetGraph.loadAssets('index.html');
'<object> to render the same after subsetting <object> <string?>',
async (expect, assetGraph, options, fileName = 'index.html') => {
const [htmlAsset] = await assetGraph.loadAssets(fileName);
const originalText = htmlAsset.text;
expect.subjectOutput = (output) => {
output.code(originalText, 'html');
Expand All @@ -96,12 +100,13 @@ expect.addAssertion(
.map((asset) =>
asset.url.replace(assetGraph.root, 'https://example.com/')
);
const screenshotBefore = await screenshot(browser, assetGraph);
const screenshotBefore = await screenshot(browser, assetGraph, fileName);
const { fontInfo } = await subsetFonts(assetGraph, options);
if (fontInfo.length > 0) {
const screenshotAfter = await screenshot(
browser,
assetGraph,
fileName,
fontsBefore
);
await expect(screenshotAfter, 'to resemble', screenshotBefore, {
Expand Down
20 changes: 16 additions & 4 deletions test/referenceImages.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
const expect = require('./expect');
const combos = require('combos');
const pathModule = require('path');

function getPathToTestCase(name) {
return pathModule.resolve(
__dirname,
'..',
'testdata',
'referenceImages',
name,
'index.html'
);
}

describe('reference images', function () {
for (const options of combos({
Expand All @@ -14,31 +26,31 @@ describe('reference images', function () {
.join(', ')}`, function () {
it('should render a simple test case without ligatures', async function () {
await expect(
'withoutLigatures',
getPathToTestCase('withoutLigatures'),
'to render the same after subsetting',
options
);
});

it('should render ligatures correctly', async function () {
await expect(
'ligatures',
getPathToTestCase('ligatures'),
'to render the same after subsetting',
options
);
});

it('should render missing glyphs', async function () {
await expect(
'missingGlyphs',
getPathToTestCase('missingGlyphs'),
'to render the same after subsetting',
options
);
});

it('should render unused variants', async function () {
await expect(
'unusedVariants',
getPathToTestCase('unusedVariants'),
'to render the same after subsetting',
options
);
Expand Down

0 comments on commit c2daad8

Please sign in to comment.