Skip to content

Commit

Permalink
chore: Integrating upstream build suite changes
Browse files Browse the repository at this point in the history
  • Loading branch information
rschristian committed Jan 31, 2022
1 parent e9f938e commit c1d17d5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
1 change: 1 addition & 0 deletions packages/cli/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ declare global {
namespace jest {
interface Matchers<R> {
toBeCloseInSize(receivedSize: number, expectedSize: number): R;
toFindMatchingKey(receivedKeys: string[]): R;
}
}
}
11 changes: 6 additions & 5 deletions packages/cli/tests/build.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ const { existsSync } = require('fs');
const { readFile } = require('fs').promises;
const looksLike = require('html-looks-like');
const { create, build } = require('./lib/cli');
const { snapshot, findMatchingKey } = require('./lib/utils');
const { snapshot } = require('./lib/utils');
const { subject } = require('./lib/output');
const images = require('./images/build');
const { promisify } = require('util');
const glob = promisify(require('glob').glob);
const minimatch = require('minimatch');

const prerenderUrlFiles = [
'prerender-urls.json',
Expand Down Expand Up @@ -39,11 +40,11 @@ function testMatch(received, expected) {
let receivedKeys = Object.keys(received);
let expectedKeys = Object.keys(expected);
expect(receivedKeys).toHaveLength(expectedKeys.length);
for (let k in expected) {
let receivedKey = findMatchingKey(k, receivedKeys);
expect(receivedKey).toBeTruthy();
for (let key in expected) {
const receivedKey = receivedKeys.find(k => minimatch(k, key));
expect(key).toFindMatchingKey(receivedKey);

expect(receivedKey).toBeCloseInSize(received[receivedKey], expected[k]);
expect(receivedKey).toBeCloseInSize(received[receivedKey], expected[key]);
}
}

Expand Down
18 changes: 12 additions & 6 deletions packages/cli/tests/lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable no-console */
const { relative, resolve } = require('path');
const { stat, readFile, writeFile } = require('fs').promises;
const minimatch = require('minimatch');
const pRetry = require('p-retry');
const { promisify } = require('util');
const glob = promisify(require('glob').glob);
Expand Down Expand Up @@ -41,10 +40,6 @@ async function snapshot(dir) {
return out;
}

function findMatchingKey(key, arr) {
return arr.find(k => minimatch(k, key));
}

async function log(fn, msg) {
logger('info', `${msg} - started...`);
try {
Expand All @@ -67,6 +62,18 @@ function waitUntil(action, errorMessage) {
const sleep = promisify(setTimeout);

expect.extend({
toFindMatchingKey(key, matchingKey) {
if (matchingKey) {
return {
message: () => `expected '${key}'' not to exist in received keys`,
pass: true,
};
}
return {
message: () => `expected '${key}' to exist in received keys`,
pass: false,
};
},
toBeCloseInSize(key, receivedSize, expectedSize) {
const expectedMin = expectedSize * 0.95;
const expectedMax = expectedSize * 1.05;
Expand Down Expand Up @@ -102,5 +109,4 @@ module.exports = {
log,
waitUntil,
sleep,
findMatchingKey,
};

0 comments on commit c1d17d5

Please sign in to comment.