Skip to content

Commit e7ff6a7

Browse files
committed
Refactor accessing test body
1 parent 5eb639d commit e7ff6a7

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

src/index.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,33 +41,29 @@ module.exports = function({ template, types }) {
4141
return;
4242
}
4343

44-
// get the test case body
45-
let body = path.node.expression.arguments[1].body;
44+
const test = path.node.expression.arguments[1];
4645

47-
// if it's an expression, e.g: () => (expression)
48-
if (types.isCallExpression(body)) {
49-
// convert it into a block statement: () => { return (expression); }
50-
body = path.node.expression.arguments[1].body = types.blockStatement([types.returnStatement(body)]);
46+
if (types.isCallExpression(test.body)) {
47+
test.body = types.blockStatement([types.returnStatement(test.body)]);
5148
}
5249

53-
// generate the code
54-
const { code } = generate(body);
50+
const { code } = generate(test.body);
5551
const normalisedCode = removeComments(code);
5652

5753
const count = (normalisedCode.match(/expect\(/g) || []).length;
5854
const containsExpectAssertions = normalisedCode.includes('expect.assertions(');
5955
const containsHasAssertions = normalisedCode.includes('expect.hasAssertions()');
6056

61-
const args = body.body;
57+
const body = test.body.body;
6258

6359
if (!containsHasAssertions) {
6460
const hasAssertions = template('expect.hasAssertions();')();
65-
args.unshift(hasAssertions);
61+
body.unshift(hasAssertions);
6662
}
6763

6864
if (count > 0 && !containsExpectAssertions) {
6965
const assertions = template(`expect.assertions(${count})`)();
70-
args.unshift(assertions);
66+
body.unshift(assertions);
7167
}
7268
}
7369
}

0 commit comments

Comments
 (0)