1 | import * as ts from 'typescript';
|
2 |
|
3 | import { AstUtils } from './AstUtils';
|
4 | import { Utils } from './Utils';
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | export namespace MochaUtils {
|
10 | export function isMochaTest(node: ts.SourceFile): boolean {
|
11 | return Utils.exists(
|
12 | node.statements,
|
13 | (statement: ts.Statement): boolean => {
|
14 | return isStatementDescribeCall(statement);
|
15 | }
|
16 | );
|
17 | }
|
18 |
|
19 | export function isStatementDescribeCall(statement: ts.Statement): boolean {
|
20 | if (statement.kind === ts.SyntaxKind.ExpressionStatement) {
|
21 | const expression: ts.Expression = (<ts.ExpressionStatement>statement).expression;
|
22 | if (expression.kind === ts.SyntaxKind.CallExpression) {
|
23 | const call: ts.CallExpression = <ts.CallExpression>expression;
|
24 | return isDescribe(call);
|
25 | }
|
26 | }
|
27 | return false;
|
28 | }
|
29 |
|
30 | |
31 |
|
32 |
|
33 | export function isDescribe(call: ts.CallExpression): boolean {
|
34 | const functionName: string = AstUtils.getFunctionName(call);
|
35 | const callText: string = call.expression.getText();
|
36 | return functionName === 'describe' || functionName === 'context' || /(describe|context)\.(only|skip|timeout)/.test(callText);
|
37 | }
|
38 |
|
39 | /**
|
40 | * Tells you if the call is an it(), specify(), before(), etc.
|
41 | */
|
42 | export function isLifecycleMethod(call: ts.CallExpression): boolean {
|
43 | const functionName: string = AstUtils.getFunctionName(call);
|
44 | return (
|
45 | functionName === 'it' ||
|
46 | functionName === 'specify' ||
|
47 | functionName === 'before' ||
|
48 | functionName === 'beforeEach' ||
|
49 | functionName === 'beforeAll' ||
|
50 | functionName === 'after' ||
|
51 | functionName === 'afterEach' ||
|
52 | functionName === 'afterAll'
|
53 | );
|
54 | }
|
55 | }
|