Skip to content

Commit

Permalink
✨ Throw when snapshot options result in no snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
wwilsman committed Mar 8, 2022
1 parent 3918f08 commit 8c50a17
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
11 changes: 5 additions & 6 deletions packages/core/src/percy.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,14 +372,13 @@ export class Percy {
if (!options.snapshots) options.sitemap = new URL('sitemap.xml', options.baseUrl).href;
}

let snapshots = options.snapshots ||
let snapshots = mapSnapshotOptions((options.snapshots ||
('sitemap' in options && await getSitemapSnapshots(options)) ||
('url' in options && [options]);

await Promise.all(mapSnapshotOptions(snapshots, options, (
snapshot => this._takeSnapshot(snapshot)
)));
('url' in options && [options])
), options);

if (!snapshots.length) throw new Error('No snapshots found');
await Promise.all(snapshots.map(s => this._takeSnapshot(s)));
await server?.close();
});
}
Expand Down
10 changes: 6 additions & 4 deletions packages/core/src/snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,18 @@ export function snapshotMatches(snapshot, include, exclude) {
}

// Accepts an array of snapshots to filter and map with matching options.
export function mapSnapshotOptions(snapshots, config, map) {
export function mapSnapshotOptions(snapshots, config) {
if (!snapshots?.length) return [];

// reduce options into a single function
let applyOptions = [].concat(config?.options || [])
.reduceRight((next, { include, exclude, ...opts }) => snap => next(
// assign additional options to included snaphots
snapshotMatches(snap, include, exclude) ? Object.assign(snap, opts) : snap
), map);
), s => s);

// reduce snapshots with overrides
return [...snapshots].reduce((acc, snapshot) => {
return snapshots.reduce((acc, snapshot) => {
// transform snapshot URL shorthand into an object
if (typeof snapshot === 'string') snapshot = { url: snapshot };

Expand Down Expand Up @@ -157,7 +159,7 @@ export async function getSitemapSnapshots(options) {
}

// parse XML content into a list of URLs
let urls = body.match(/(?<=<loc>)(.*)(?=<\/loc>)/ig);
let urls = body.match(/(?<=<loc>)(.*)(?=<\/loc>)/ig) ?? [];

// filter out duplicate URLs that differ by a trailing slash
return urls.filter((url, i) => {
Expand Down
8 changes: 8 additions & 0 deletions packages/core/test/snapshot-multiple.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,14 @@ describe('Snapshot multiple', () => {
'The sitemap must be an XML document, but the content-type was "text/html"'
);
});

it('throws when a sitemap is empty', async () => {
sitemap = [];

await expectAsync(percy.snapshot({
sitemap: 'http://localhost:8000/sitemap.xml'
})).toBeRejectedWithError('No snapshots found');
});
});

describe('server syntax', () => {
Expand Down

0 comments on commit 8c50a17

Please sign in to comment.