| 1 | "use strict";
|
|---|
| 2 | Object.defineProperty(exports, "__esModule", { value: true });
|
|---|
| 3 | exports.RULE_NAME = void 0;
|
|---|
| 4 | const create_testing_library_rule_1 = require("../create-testing-library-rule");
|
|---|
| 5 | const node_utils_1 = require("../node-utils");
|
|---|
| 6 | exports.RULE_NAME = 'no-wait-for-multiple-assertions';
|
|---|
| 7 | exports.default = (0, create_testing_library_rule_1.createTestingLibraryRule)({
|
|---|
| 8 | name: exports.RULE_NAME,
|
|---|
| 9 | meta: {
|
|---|
| 10 | type: 'suggestion',
|
|---|
| 11 | docs: {
|
|---|
| 12 | description: 'Disallow the use of multiple `expect` calls inside `waitFor`',
|
|---|
| 13 | recommendedConfig: {
|
|---|
| 14 | dom: 'error',
|
|---|
| 15 | angular: 'error',
|
|---|
| 16 | react: 'error',
|
|---|
| 17 | vue: 'error',
|
|---|
| 18 | marko: 'error',
|
|---|
| 19 | },
|
|---|
| 20 | },
|
|---|
| 21 | messages: {
|
|---|
| 22 | noWaitForMultipleAssertion: 'Avoid using multiple assertions within `waitFor` callback',
|
|---|
| 23 | },
|
|---|
| 24 | schema: [],
|
|---|
| 25 | },
|
|---|
| 26 | defaultOptions: [],
|
|---|
| 27 | create(context, _, helpers) {
|
|---|
| 28 | function getExpectNodes(body) {
|
|---|
| 29 | return body.filter((node) => {
|
|---|
| 30 | if (!(0, node_utils_1.isExpressionStatement)(node)) {
|
|---|
| 31 | return false;
|
|---|
| 32 | }
|
|---|
| 33 | const expressionIdentifier = (0, node_utils_1.getPropertyIdentifierNode)(node);
|
|---|
| 34 | if (!expressionIdentifier) {
|
|---|
| 35 | return false;
|
|---|
| 36 | }
|
|---|
| 37 | return expressionIdentifier.name === 'expect';
|
|---|
| 38 | });
|
|---|
| 39 | }
|
|---|
| 40 | function reportMultipleAssertion(node) {
|
|---|
| 41 | if (!node.parent) {
|
|---|
| 42 | return;
|
|---|
| 43 | }
|
|---|
| 44 | const callExpressionNode = node.parent.parent;
|
|---|
| 45 | const callExpressionIdentifier = (0, node_utils_1.getPropertyIdentifierNode)(callExpressionNode);
|
|---|
| 46 | if (!callExpressionIdentifier) {
|
|---|
| 47 | return;
|
|---|
| 48 | }
|
|---|
| 49 | if (!helpers.isAsyncUtil(callExpressionIdentifier, ['waitFor'])) {
|
|---|
| 50 | return;
|
|---|
| 51 | }
|
|---|
| 52 | const expectNodes = getExpectNodes(node.body);
|
|---|
| 53 | if (expectNodes.length <= 1) {
|
|---|
| 54 | return;
|
|---|
| 55 | }
|
|---|
| 56 | for (let i = 0; i < expectNodes.length; i++) {
|
|---|
| 57 | if (i !== 0) {
|
|---|
| 58 | context.report({
|
|---|
| 59 | node: expectNodes[i],
|
|---|
| 60 | messageId: 'noWaitForMultipleAssertion',
|
|---|
| 61 | });
|
|---|
| 62 | }
|
|---|
| 63 | }
|
|---|
| 64 | }
|
|---|
| 65 | return {
|
|---|
| 66 | 'CallExpression > ArrowFunctionExpression > BlockStatement': reportMultipleAssertion,
|
|---|
| 67 | 'CallExpression > FunctionExpression > BlockStatement': reportMultipleAssertion,
|
|---|
| 68 | };
|
|---|
| 69 | },
|
|---|
| 70 | });
|
|---|