forked from ChromeDevTools/devtools-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscreenshot-assertion-in-it-screenshot.js
110 lines (96 loc) · 3.14 KB
/
screenshot-assertion-in-it-screenshot.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2023 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
'use strict';
/**
* @type {import('eslint').Rule.RuleModule}
*/
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'ensure screenshots are asserted in an itScreenshot block',
category: 'Possible Errors',
},
messages: {
itScreenshotRequired: 'A screenshot assertion must be within an itScreenshot() test, not an it() test.',
},
fixable: 'code',
schema: [] // no options
},
create: function(context) {
const SCREENSHOT_ASSERTION_FUNCTIONS =
new Set(['assertElementScreenshotUnchanged', 'assertPageScreenshotUnchanged']);
function nodeIsFunctionCallToCheck(node) {
if (node.expression.type === 'CallExpression') {
return true;
}
if (node.expression.type === 'AwaitExpression') {
return node.expression.argument.type === 'CallExpression';
}
return false;
}
function getNameOfFunctionToCheck(node) {
if (node.type !== 'ExpressionStatement') {
return null;
}
if (!nodeIsFunctionCallToCheck(node)) {
return null;
}
let nameOfCalledFunction = '';
if (node.expression.type === 'CallExpression') {
nameOfCalledFunction = node.expression.callee.name;
} else if (node.expression.type === 'AwaitExpression') {
nameOfCalledFunction = node.expression.argument.callee.name;
}
return nameOfCalledFunction || null;
}
function findItParentForNode(node) {
if (!node || !node.parent) {
return null;
}
const parent = node.parent;
if (parent.type === 'Program') {
return null;
}
if (parent.type === 'ExpressionStatement' && parent.expression.type === 'CallExpression' &&
parent.expression.callee?.name === 'it') {
return parent;
}
return findItParentForNode(node.parent);
}
function checkForScreenshotCalls(bodyNodes) {
for (const node of bodyNodes) {
const functionCallName = getNameOfFunctionToCheck(node);
if (!functionCallName) {
continue;
}
if (SCREENSHOT_ASSERTION_FUNCTIONS.has(functionCallName)) {
context.report({
node,
messageId: 'itScreenshotRequired',
fix(fixer) {
const itExpression = findItParentForNode(node);
const callee = itExpression?.expression?.callee;
if (!callee) {
return [];
}
return [fixer.replaceText(callee, 'itScreenshot')];
}
});
}
}
}
return {
'CallExpression[callee.type="Identifier"][callee.name="it"]'(node) {
const testCallback = node.arguments[1];
const validCallbackTypes = ['ArrowFunctionExpression', 'FunctionExpression'];
if (!testCallback || !validCallbackTypes.includes(testCallback.type)) {
// Oddly structured it: bail.
return;
}
checkForScreenshotCalls(testCallback.body.body);
}
};
}
};