Skip to content

Commit 4594c9d

Browse files
authored
feat: allow custom threshold (#142)
* feat: allow custom threshold * docs: update docs * test: add example test
1 parent 1fc9a71 commit 4594c9d

5 files changed

Lines changed: 24 additions & 5 deletions

File tree

docs/api/matchers.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ sidebar_position: 3
44

55
# Matchers
66

7-
### toMatchBaseline(name: string)
7+
### toMatchBaseline(options: {threshold?: number} = {threshold: 0.1})
88

9-
This custom matcher will try to find and compare the baseline screenshot by using the path of the latest screenshot (returned by `takeScreenshot()`). You will have to take a screenshot before using, and pass the path of that screenshot to the expect method.
9+
This custom Jest matcher will try to find and compare the baseline screenshot by using the path of the latest screenshot (returned by `takeScreenshot()`). You will have to take a screenshot before using, and pass the path of that screenshot to the expect method.
1010

1111
#### Example
1212

@@ -19,10 +19,15 @@ describe('App.tsx', () => {
1919

2020
// highlight-next-line
2121
expect(screen).toMatchBaseline();
22+
23+
// Or with a custom threshold
24+
expect(screen).toMatchBaseline({ threshold: 0.2 });
2225
});
2326
});
2427
```
2528

2629
The first time this test is run, or when run with the `--update` flag, the `.toMatchBaseline` expectation will always be successful.
2730

2831
On subsequent test runs, the screenshot captured by `takeScreenshot` (and stored in `/current`) will be compared to the baseline screenshot. **_Any_** differences will cause the expectation to fail, and the report to be generated.
32+
33+
The `threshold` option allows you to customise the threshold for [pixelmatch](https://github.com/mapbox/pixelmatch#pixelmatchimg1-img2-output-width-height-options) which Owl uses under the hood.
218 KB
Loading
304 KB
Loading

example/__tests__/App.owl.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ describe('App.tsx', () => {
6060

6161
expect(screen).toMatchBaseline();
6262
});
63+
64+
it('takes a screenshot with a custom threshold', async () => {
65+
const screen = await takeScreenshot('custom-threshold');
66+
67+
expect(screen).toMatchBaseline({threshold: 0.25});
68+
});
6369
});
6470

6571
describe('Reload example', () => {

lib/matchers.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,19 @@ declare global {
99
namespace jest {
1010
interface Matchers<R> {
1111
/** Compares the image passed to the baseline one */
12-
toMatchBaseline: () => CustomMatcherResult;
12+
toMatchBaseline: ({
13+
threshold,
14+
}?: {
15+
threshold?: number;
16+
}) => CustomMatcherResult;
1317
}
1418
}
1519
}
1620

17-
export const toMatchBaseline = (latestPath: string) => {
21+
export const toMatchBaseline = (
22+
latestPath: string,
23+
options: { threshold?: number } = { threshold: 0.1 }
24+
) => {
1825
const platform = process.env.OWL_PLATFORM as Platform;
1926
const screenshotsDir = path.join(path.dirname(latestPath), '..', '..');
2027
const baselinePath = path.join(
@@ -56,7 +63,8 @@ export const toMatchBaseline = (latestPath: string) => {
5663
latestImage.data,
5764
diffImage.data,
5865
baselineImage.width,
59-
baselineImage.height
66+
baselineImage.height,
67+
{ threshold: options?.threshold }
6068
);
6169

6270
if (diffPixelsCount === 0) {

0 commit comments

Comments
 (0)