Skip to content

Commit a8bb7e9

Browse files
committed
fix: add TS types for Jest custom matchers
1 parent e9e02df commit a8bb7e9

File tree

4 files changed

+31
-9
lines changed

4 files changed

+31
-9
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
coverage
33
lib
44
node_modules
5+
tsconfig.build.json
56
tsconfig.json
67
yarn-error.log

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"jest": "mmm jest",
2525
"release": "mmm release",
2626
"test": "mmm test",
27+
"test:compile": "mmm test:compile",
2728
"test:coverage": "mmm test:coverage",
2829
"test:lint": "mmm test:lint"
2930
},
@@ -41,6 +42,7 @@
4142
"@emotion/core": "^10.0.17",
4243
"@emotion/styled": "^10.0.17",
4344
"@types/fs-extra": "^8.0.0",
45+
"@types/jest-image-snapshot": "^2.8.0",
4446
"@types/pretty": "^2.0.0",
4547
"@types/puppeteer": "^1.19.1",
4648
"@types/yamljs": "^0.2.30",

src/extend-expect.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
1-
// @ts-ignore
2-
import { toMatchImageSnapshot as toMatchImageSnapshotBase } from 'jest-image-snapshot'
3-
// @ts-ignore
1+
import {
2+
MatchImageSnapshotOptions,
3+
toMatchImageSnapshot as toMatchImageSnapshotBase,
4+
} from 'jest-image-snapshot'
45
import { toMatchSnapshot } from 'jest-snapshot'
56
import { a11ySnapshot, domSnapshot, imageSnapshot } from '.'
67
import ComponentElement from './types/component-element'
78

9+
declare global {
10+
// eslint-disable-next-line @typescript-eslint/no-namespace
11+
namespace jest {
12+
interface Matchers<R> {
13+
toMatchA11ySnapshot(): R
14+
toMatchDomSnapshot(): R
15+
toMatchImageSnapshot(options: MatchImageSnapshotOptions): R
16+
}
17+
}
18+
}
19+
820
const extendExpect = () => {
9-
// @ts-ignore
1021
expect.extend({
1122
async toMatchA11ySnapshot(received: ComponentElement) {
1223
return toMatchSnapshot.call(
@@ -17,7 +28,6 @@ const extendExpect = () => {
1728
},
1829
})
1930

20-
// @ts-ignore
2131
expect.extend({
2232
async toMatchDomSnapshot(received: ComponentElement) {
2333
return toMatchSnapshot.call(
@@ -28,10 +38,12 @@ const extendExpect = () => {
2838
},
2939
})
3040

31-
// @ts-ignore
3241
expect.extend({
33-
async toMatchImageSnapshot(received: ComponentElement, options: any) {
34-
return toMatchImageSnapshotBase.call(
42+
async toMatchImageSnapshot(
43+
received: ComponentElement,
44+
options: MatchImageSnapshotOptions,
45+
) {
46+
return (toMatchImageSnapshotBase as any).call(
3547
this as any,
3648
await imageSnapshot(received),
3749
options,

yarn.lock

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1592,7 +1592,14 @@
15921592
resolved "https://registry.yarnpkg.com/@types/jest-diff/-/jest-diff-20.0.1.tgz#35cc15b9c4f30a18ef21852e255fdb02f6d59b89"
15931593
integrity sha512-yALhelO3i0hqZwhjtcr6dYyaLoCHbAMshwtj6cGxTvHZAKXHsYGdff6E8EPw3xLKY0ELUTQ69Q1rQiJENnccMA==
15941594

1595-
"@types/jest@24.0.18":
1595+
"@types/jest-image-snapshot@^2.8.0":
1596+
version "2.8.0"
1597+
resolved "https://registry.yarnpkg.com/@types/jest-image-snapshot/-/jest-image-snapshot-2.8.0.tgz#2f8ed2b23c1b9a8ead32a2f3ee0ef672e5fd01dd"
1598+
integrity sha512-CsE6PLYGa7hmM8ut1gzkW7iDxrW2xwSv3yJJf2dejwvF7cIPtzHPLpQWO29H4xnzIaLyEB09MiVnYS/LoYpfzw==
1599+
dependencies:
1600+
"@types/jest" "*"
1601+
1602+
"@types/jest@*", "@types/jest@24.0.18":
15961603
version "24.0.18"
15971604
resolved "https://registry.yarnpkg.com/@types/jest/-/jest-24.0.18.tgz#9c7858d450c59e2164a8a9df0905fc5091944498"
15981605
integrity sha512-jcDDXdjTcrQzdN06+TSVsPPqxvsZA/5QkYfIZlq1JMw7FdP5AZylbOc+6B/cuDurctRe+MziUMtQ3xQdrbjqyQ==

0 commit comments

Comments
 (0)