UNPKG

1.47 kBJavaScriptView Raw
1'use strict';
2
3/*
4 * This implementation is adapted from eslint-plugin-jasmine.
5 * MIT license, Remco Haszing.
6 */
7
8const { getDocsUrl, getNodeName } = require('./util');
9
10module.exports = {
11 meta: {
12 docs: {
13 url: getDocsUrl(__filename),
14 },
15 schema: [
16 {
17 type: 'object',
18 properties: {
19 assertFunctionNames: {
20 type: 'array',
21 items: [{ type: 'string' }],
22 },
23 },
24 additionalProperties: false,
25 },
26 ],
27 },
28 create(context) {
29 const unchecked = [];
30 const assertFunctionNames = new Set(
31 context.options[0] && context.options[0].assertFunctionNames
32 ? context.options[0].assertFunctionNames
33 : ['expect']
34 );
35
36 return {
37 CallExpression(node) {
38 const name = getNodeName(node.callee);
39 if (name === 'it' || name === 'test') {
40 unchecked.push(node);
41 } else if (assertFunctionNames.has(name)) {
42 // Return early in case of nested `it` statements.
43 for (const ancestor of context.getAncestors()) {
44 const index = unchecked.indexOf(ancestor);
45 if (index !== -1) {
46 unchecked.splice(index, 1);
47 break;
48 }
49 }
50 }
51 },
52 'Program:exit'() {
53 unchecked.forEach(node =>
54 context.report({
55 message: 'Test has no assertions',
56 node,
57 })
58 );
59 },
60 };
61 },
62};