Skip to content

Commit

Permalink
fix: add TS types for Jest custom matchers
Browse files Browse the repository at this point in the history
  • Loading branch information
mmmurray committed Sep 15, 2019
1 parent e9e02df commit a8bb7e9
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
coverage
lib
node_modules
tsconfig.build.json
tsconfig.json
yarn-error.log
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"jest": "mmm jest",
"release": "mmm release",
"test": "mmm test",
"test:compile": "mmm test:compile",
"test:coverage": "mmm test:coverage",
"test:lint": "mmm test:lint"
},
Expand All @@ -41,6 +42,7 @@
"@emotion/core": "^10.0.17",
"@emotion/styled": "^10.0.17",
"@types/fs-extra": "^8.0.0",
"@types/jest-image-snapshot": "^2.8.0",
"@types/pretty": "^2.0.0",
"@types/puppeteer": "^1.19.1",
"@types/yamljs": "^0.2.30",
Expand Down
28 changes: 20 additions & 8 deletions src/extend-expect.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
// @ts-ignore
import { toMatchImageSnapshot as toMatchImageSnapshotBase } from 'jest-image-snapshot'
// @ts-ignore
import {
MatchImageSnapshotOptions,
toMatchImageSnapshot as toMatchImageSnapshotBase,
} from 'jest-image-snapshot'
import { toMatchSnapshot } from 'jest-snapshot'
import { a11ySnapshot, domSnapshot, imageSnapshot } from '.'
import ComponentElement from './types/component-element'

declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace jest {
interface Matchers<R> {
toMatchA11ySnapshot(): R
toMatchDomSnapshot(): R
toMatchImageSnapshot(options: MatchImageSnapshotOptions): R
}
}
}

const extendExpect = () => {
// @ts-ignore
expect.extend({
async toMatchA11ySnapshot(received: ComponentElement) {
return toMatchSnapshot.call(
Expand All @@ -17,7 +28,6 @@ const extendExpect = () => {
},
})

// @ts-ignore
expect.extend({
async toMatchDomSnapshot(received: ComponentElement) {
return toMatchSnapshot.call(
Expand All @@ -28,10 +38,12 @@ const extendExpect = () => {
},
})

// @ts-ignore
expect.extend({
async toMatchImageSnapshot(received: ComponentElement, options: any) {
return toMatchImageSnapshotBase.call(
async toMatchImageSnapshot(
received: ComponentElement,
options: MatchImageSnapshotOptions,
) {
return (toMatchImageSnapshotBase as any).call(
this as any,
await imageSnapshot(received),
options,
Expand Down
9 changes: 8 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1592,7 +1592,14 @@
resolved "https://registry.yarnpkg.com/@types/jest-diff/-/jest-diff-20.0.1.tgz#35cc15b9c4f30a18ef21852e255fdb02f6d59b89"
integrity sha512-yALhelO3i0hqZwhjtcr6dYyaLoCHbAMshwtj6cGxTvHZAKXHsYGdff6E8EPw3xLKY0ELUTQ69Q1rQiJENnccMA==

"@types/jest@24.0.18":
"@types/jest-image-snapshot@^2.8.0":
version "2.8.0"
resolved "https://registry.yarnpkg.com/@types/jest-image-snapshot/-/jest-image-snapshot-2.8.0.tgz#2f8ed2b23c1b9a8ead32a2f3ee0ef672e5fd01dd"
integrity sha512-CsE6PLYGa7hmM8ut1gzkW7iDxrW2xwSv3yJJf2dejwvF7cIPtzHPLpQWO29H4xnzIaLyEB09MiVnYS/LoYpfzw==
dependencies:
"@types/jest" "*"

"@types/jest@*", "@types/jest@24.0.18":
version "24.0.18"
resolved "https://registry.yarnpkg.com/@types/jest/-/jest-24.0.18.tgz#9c7858d450c59e2164a8a9df0905fc5091944498"
integrity sha512-jcDDXdjTcrQzdN06+TSVsPPqxvsZA/5QkYfIZlq1JMw7FdP5AZylbOc+6B/cuDurctRe+MziUMtQ3xQdrbjqyQ==
Expand Down

0 comments on commit a8bb7e9

Please sign in to comment.