| [9af201e] | 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 | exports.default = void 0;
|
|---|
| 7 |
|
|---|
| 8 | var _utils = require("./utils");
|
|---|
| 9 |
|
|---|
| 10 | const messages = {
|
|---|
| 11 | tooManyDescribes: 'There should not be more than {{ max }} describe{{ s }} at the top level',
|
|---|
| 12 | unexpectedTestCase: 'All test cases must be wrapped in a describe block.',
|
|---|
| 13 | unexpectedHook: 'All hooks must be wrapped in a describe block.'
|
|---|
| 14 | };
|
|---|
| 15 |
|
|---|
| 16 | var _default = (0, _utils.createRule)({
|
|---|
| 17 | name: __filename,
|
|---|
| 18 | meta: {
|
|---|
| 19 | docs: {
|
|---|
| 20 | category: 'Best Practices',
|
|---|
| 21 | description: 'Require test cases and hooks to be inside a `describe` block',
|
|---|
| 22 | recommended: false
|
|---|
| 23 | },
|
|---|
| 24 | messages,
|
|---|
| 25 | type: 'suggestion',
|
|---|
| 26 | schema: [{
|
|---|
| 27 | type: 'object',
|
|---|
| 28 | properties: {
|
|---|
| 29 | maxNumberOfTopLevelDescribes: {
|
|---|
| 30 | type: 'number',
|
|---|
| 31 | minimum: 1
|
|---|
| 32 | }
|
|---|
| 33 | },
|
|---|
| 34 | additionalProperties: false
|
|---|
| 35 | }]
|
|---|
| 36 | },
|
|---|
| 37 | defaultOptions: [{}],
|
|---|
| 38 |
|
|---|
| 39 | create(context) {
|
|---|
| 40 | var _context$options$;
|
|---|
| 41 |
|
|---|
| 42 | const {
|
|---|
| 43 | maxNumberOfTopLevelDescribes = Infinity
|
|---|
| 44 | } = (_context$options$ = context.options[0]) !== null && _context$options$ !== void 0 ? _context$options$ : {};
|
|---|
| 45 | let numberOfTopLevelDescribeBlocks = 0;
|
|---|
| 46 | let numberOfDescribeBlocks = 0;
|
|---|
| 47 | return {
|
|---|
| 48 | CallExpression(node) {
|
|---|
| 49 | if ((0, _utils.isDescribeCall)(node)) {
|
|---|
| 50 | numberOfDescribeBlocks++;
|
|---|
| 51 |
|
|---|
| 52 | if (numberOfDescribeBlocks === 1) {
|
|---|
| 53 | numberOfTopLevelDescribeBlocks++;
|
|---|
| 54 |
|
|---|
| 55 | if (numberOfTopLevelDescribeBlocks > maxNumberOfTopLevelDescribes) {
|
|---|
| 56 | context.report({
|
|---|
| 57 | node,
|
|---|
| 58 | messageId: 'tooManyDescribes',
|
|---|
| 59 | data: {
|
|---|
| 60 | max: maxNumberOfTopLevelDescribes,
|
|---|
| 61 | s: maxNumberOfTopLevelDescribes === 1 ? '' : 's'
|
|---|
| 62 | }
|
|---|
| 63 | });
|
|---|
| 64 | }
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | return;
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | if (numberOfDescribeBlocks === 0) {
|
|---|
| 71 | if ((0, _utils.isTestCaseCall)(node)) {
|
|---|
| 72 | context.report({
|
|---|
| 73 | node,
|
|---|
| 74 | messageId: 'unexpectedTestCase'
|
|---|
| 75 | });
|
|---|
| 76 | return;
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | if ((0, _utils.isHook)(node)) {
|
|---|
| 80 | context.report({
|
|---|
| 81 | node,
|
|---|
| 82 | messageId: 'unexpectedHook'
|
|---|
| 83 | });
|
|---|
| 84 | return;
|
|---|
| 85 | }
|
|---|
| 86 | }
|
|---|
| 87 | },
|
|---|
| 88 |
|
|---|
| 89 | 'CallExpression:exit'(node) {
|
|---|
| 90 | if ((0, _utils.isDescribeCall)(node)) {
|
|---|
| 91 | numberOfDescribeBlocks--;
|
|---|
| 92 | }
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | };
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | });
|
|---|
| 99 |
|
|---|
| 100 | exports.default = _default; |
|---|