Skip to content

Commit 3086c9c

Browse files
chore: add lint rule to block toMatchSnapshot usage (#28091)
Issue number: N/A --------- <!-- Please do not submit updates to dependencies unless it fixes an issue. --> <!-- Please try to limit your pull request to one type (bugfix, feature, etc). Submit multiple pull requests if needed. --> ## What is the current behavior? <!-- Please describe the current behavior that you are modifying. --> In addition to migrating away from `toMatchSnapshot` as found in PRs such as #28083, I think it would be valuable to have a lint rule to prevent developers from accidentally using this API in the future. ## What is the new behavior? <!-- Please describe the behavior or changes that are being added by this PR. --> - Adds a lint rule which errors if `toMatchSnapshot` is being used in `*.e2e.ts` files. ## Does this introduce a breaking change? - [ ] Yes - [x] No <!-- If this introduces a breaking change, please describe the impact and migration path for existing applications below. --> ## Other information <!-- Any other information that is important to this PR such as screenshots of how the component looks before and after the change. --> ~Note: This PR's lint step will continue to fail until the remaining `toHaveScreenshot` migration PRs have been merged. Do note merge this PR until that has been completed.~ All set! --------- Co-authored-by: Brandy Carney <[email protected]>
1 parent 63cb968 commit 3086c9c

File tree

4 files changed

+33
-5
lines changed

4 files changed

+33
-5
lines changed

core/.eslintrc.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ module.exports = {
4646
{
4747
"files": ["*.e2e.ts"],
4848
"rules": {
49-
"custom-rules/await-playwright-promise-assertion": "error"
49+
"custom-rules/await-playwright-promise-assertion": "error",
50+
"custom-rules/no-playwright-to-match-snapshot-assertion": "error"
5051
}
5152
}
5253
]

core/custom-rules/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module.exports = {
22
rules: {
33
'no-component-on-ready-method': require('./no-component-on-ready-method.js'),
4-
'await-playwright-promise-assertion': require('./await-playwright-promise-assertion.js')
4+
'await-playwright-promise-assertion': require('./await-playwright-promise-assertion.js'),
5+
'no-playwright-to-match-snapshot-assertion': require('./no-playwright-to-match-snapshot-assertion.js')
56
}
67
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module.exports = {
2+
meta: {
3+
messages: {
4+
noPlaywrightToMatchSnapshotAssertion: '"toHaveScreenshot" assertions should be used in favor of "toMatchSnapshot". "toHaveScreenshot" brings file size reductions and anti-flake behaviors such as disabling animations by default.',
5+
},
6+
},
7+
create(context) {
8+
return {
9+
ExpressionStatement(node) {
10+
if (node.expression.callee === undefined) {
11+
return;
12+
}
13+
14+
const { property } = node.expression.callee;
15+
16+
/**
17+
* Check to see if toMatchSnapshot is being used
18+
*/
19+
if (
20+
property !== undefined &&
21+
property.type === 'Identifier' &&
22+
property.name === 'toMatchSnapshot'
23+
) {
24+
context.report({ node: node, messageId: 'noPlaywrightToMatchSnapshotAssertion' });
25+
}
26+
}
27+
}
28+
}
29+
};

core/playwright.config.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,6 @@ const config: PlaywrightTestConfig = {
5555
timeout: 5000,
5656
toHaveScreenshot: {
5757
threshold: 0.1
58-
},
59-
toMatchSnapshot: {
60-
threshold: 0.1
6158
}
6259
},
6360
/* Fail the build on CI if you accidentally left test.only in the source code. */

0 commit comments

Comments
 (0)