-
Notifications
You must be signed in to change notification settings - Fork 1
/
cypress-commands.ts
47 lines (40 loc) · 1.23 KB
/
cypress-commands.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/// <reference types="cypress" />
/* @hack fix "Could not find a declaration file for module"
* as `allowJs: true` was not enough. */
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore-next-line
import serializeDOM from '@percy/dom';
declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace Cypress {
interface Chainable<Subject> {
getByDataRole<E extends HTMLElement>(
dataRole: string
): Chainable<JQuery<E>>;
snapshot(name?: string): void;
}
}
}
Cypress.Commands.add('getByDataRole', (dataRole) =>
cy.get(`[data-role="${dataRole}"]`)
);
/* Cf. https://github.com/percy/percy-cypress/blob/master/index.js */
Cypress.Commands.add('snapshot', (name = undefined) => {
name = name || (cy as any).state('runnable').fullTitle();
const fileName = name.replace(/ /g, '-').toLowerCase();
const options = {
widths: [360, 768, 1280],
};
return cy.document({ log: false }).then((dom) => {
const domSnapshot = serializeDOM({ ...options, dom });
return cy.writeFile(
`./__percy_snapshots__/${fileName}.json`,
JSON.stringify({
domSnapshot,
name,
url: dom.URL,
...options,
})
);
});
});