1
1
import { FILE_SUFFIX , LINK_PREFIX , TASK } from "./constants" ;
2
2
import type pixelmatch from "pixelmatch" ;
3
3
4
+ declare global {
5
+ // eslint-disable-next-line @typescript-eslint/no-namespace
6
+ namespace Cypress {
7
+ type MatchImageOptions = {
8
+ suffix ?: string ;
9
+ screenshotConfig ?: Partial < Cypress . ScreenshotDefaultsOptions > ;
10
+ diffConfig ?: Parameters < typeof pixelmatch > [ 5 ] ;
11
+ } ;
12
+
13
+ interface Chainable {
14
+ /**
15
+ * Command to create and compare image snapshots.
16
+ * @memberof Cypress.Chainable
17
+ * @example cy.get('.my-element').matchImage();
18
+ */
19
+ matchImage < T extends Chainable < unknown > > ( this : T , options ?: Cypress . MatchImageOptions ) : T ;
20
+ }
21
+ }
22
+ }
23
+
4
24
const nameCacheCounter : Record < string , number > = { } ;
5
25
6
26
Cypress . Commands . add (
7
27
"matchImage" ,
8
28
{ prevSubject : "optional" } ,
9
29
(
10
30
subject ?: JQuery < HTMLElement > ,
11
- options : {
12
- suffix ?: string ;
13
- screenshotConfig ?: Partial < Cypress . ScreenshotDefaultsOptions > ;
14
- diffConfig ?: Parameters < typeof pixelmatch > [ 5 ] ;
15
- } = { }
31
+ options : Cypress . MatchImageOptions = { }
16
32
) => {
17
33
let title = Cypress . currentTest . titlePath . join ( " " ) ;
18
34
if ( typeof nameCacheCounter [ title ] === "undefined" )
@@ -31,8 +47,10 @@ Cypress.Commands.add(
31
47
let imgPath : string ;
32
48
return ( subject ? cy . wrap ( subject ) : cy )
33
49
. screenshot ( screenshotPath as string , {
34
- onAfterScreenshot ( _el , props ) {
50
+ ...options . screenshotConfig ,
51
+ onAfterScreenshot ( el , props ) {
35
52
imgPath = props . path ;
53
+ options . screenshotConfig ?. onAfterScreenshot ?.( el , props ) ;
36
54
} ,
37
55
} )
38
56
. then ( ( ) => imgPath ) ;
0 commit comments