Skip to content

Commit 6af55da

Browse files
hiddentaomattphillips
authored andcommitted
fix handling of expression functions (#3)
1 parent 6e21ebe commit 6af55da

File tree

6 files changed

+99
-10
lines changed

6 files changed

+99
-10
lines changed

package.json

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
"version": "0.0.1",
44
"description": "Babel plugin that adds the number of assertions found in each test with expect.assertions(n)",
55
"main": "dist/index.js",
6-
"files": ["dist"],
6+
"files": [
7+
"dist"
8+
],
79
"scripts": {
810
"build": "babel src -d dist --ignore *.test.js",
911
"contributor": "all-contributors add",
@@ -22,7 +24,14 @@
2224
"type": "git",
2325
"url": "git+https://github.com/mattphillips/babel-jest-assertions.git"
2426
},
25-
"keywords": ["babel", "jest", "plugin", "assertions", "expect", "test"],
27+
"keywords": [
28+
"babel",
29+
"jest",
30+
"plugin",
31+
"assertions",
32+
"expect",
33+
"test"
34+
],
2635
"author": "Matt Phillips <[email protected]> (mattphillips.io)",
2736
"license": "MIT",
2837
"bugs": {
@@ -46,12 +55,20 @@
4655
"prettier": "^1.6.1"
4756
},
4857
"lint-staged": {
49-
"*.js": ["yarn prettier", "git add"]
58+
"*.js": [
59+
"yarn prettier",
60+
"git add"
61+
]
5062
},
5163
"jest": {
5264
"testEnvironment": "node",
53-
"testPathIgnorePatterns": ["/node_modules/", "/fixtures/"],
54-
"coveragePathIgnorePatterns": ["/node_modules/"],
65+
"testPathIgnorePatterns": [
66+
"/node_modules/",
67+
"/fixtures/"
68+
],
69+
"coveragePathIgnorePatterns": [
70+
"/node_modules/"
71+
],
5572
"coverageThreshold": {
5673
"global": {
5774
"branches": 100,
@@ -60,5 +77,8 @@
6077
"statements": 100
6178
}
6279
}
80+
},
81+
"dependencies": {
82+
"babel-generator": "^6.26.0"
6383
}
6484
}

src/__snapshots__/async.test.js.snap

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,3 +270,22 @@ describe('.add', () => {
270270
});
271271
"
272272
`;
273+
274+
exports[`Handles expression functions 1`] = `
275+
"
276+
describe('.add', () => {
277+
it('returns 1 when given 0 and 1', async () =>
278+
expect(add(1, 0)).toEqual(1));
279+
});
280+
281+
↓ ↓ ↓ ↓ ↓ ↓
282+
283+
describe('.add', () => {
284+
it('returns 1 when given 0 and 1', async () => {
285+
expect.assertions(1);
286+
expect.hasAssertions();
287+
return expect(add(1, 0)).toEqual(1);
288+
});
289+
});
290+
"
291+
`;

src/__snapshots__/sync.test.js.snap

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ describe('.add', () => {
130130
131131
const a = 1; // expect(a).toEqual(1);
132132
const b = 2; /* expect(b).toEqual(2); */
133-
133+
134134
expect(add(1, 0)).toEqual(1);
135135
/*
136136
expect(add(6, 1).toEqual(7));
@@ -163,3 +163,22 @@ describe('.add', () => {
163163
});
164164
"
165165
`;
166+
167+
exports[`Handles expression functions 1`] = `
168+
"
169+
describe('.add', () => {
170+
it('returns 1 when given 0 and 1', () =>
171+
expect(add(1, 0)).toEqual(1));
172+
});
173+
174+
↓ ↓ ↓ ↓ ↓ ↓
175+
176+
describe('.add', () => {
177+
it('returns 1 when given 0 and 1', () => {
178+
expect.assertions(1);
179+
expect.hasAssertions();
180+
return expect(add(1, 0)).toEqual(1);
181+
});
182+
});
183+
"
184+
`;

src/async.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,15 @@ pluginTester({
137137
});
138138
});
139139
`
140+
},
141+
'Handles expression functions': {
142+
snapshot: true,
143+
code: `
144+
describe('.add', () => {
145+
it('returns 1 when given 0 and 1', async () =>
146+
expect(add(1, 0)).toEqual(1));
147+
});
148+
`
140149
}
141150
}
142151
});

src/index.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
const generate = require('babel-generator').default;
2+
13
const looksLike = require('./utils/looks-like');
24

3-
module.exports = function({ template, transformFromAst, types }) {
5+
module.exports = function({ template, types }) {
46
return {
57
name: 'babel-assertions',
68
visitor: {
@@ -38,7 +40,17 @@ module.exports = function({ template, transformFromAst, types }) {
3840
return;
3941
}
4042

41-
const { code } = transformFromAst(types.Program([path.node]));
43+
// get the test case body
44+
let body = path.node.expression.arguments[1].body;
45+
46+
// if it's an expression, e.g: () => (expression)
47+
if (types.isCallExpression(body)) {
48+
// convert it into a block statement: () => { return (expression); }
49+
body = path.node.expression.arguments[1].body = types.blockStatement([types.returnStatement(body)]);
50+
}
51+
52+
// generate the code
53+
const { code } = generate(body);
4254

4355
// remove comments
4456
const normalisedCode = code.replace(/\/\/(.*)/g, '').replace(/\/\*([\s\S]*?)\*\//g, '');
@@ -47,7 +59,8 @@ module.exports = function({ template, transformFromAst, types }) {
4759
const containsExpectAssertions = normalisedCode.includes('expect.assertions(');
4860
const containsHasAssertions = normalisedCode.includes('expect.hasAssertions()');
4961

50-
const args = path.node.expression.arguments[1].body.body;
62+
const args = body.body;
63+
5164
if (!containsHasAssertions) {
5265
const hasAssertions = template('expect.hasAssertions();')();
5366
args.unshift(hasAssertions);

src/sync.test.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,23 @@ pluginTester({
8080
8181
const a = 1; // expect(a).toEqual(1);
8282
const b = 2; /* expect(b).toEqual(2); */
83-
83+
8484
expect(add(1, 0)).toEqual(1);
8585
/*
8686
expect(add(6, 1).toEqual(7));
8787
*/
8888
});
8989
});
9090
`
91+
},
92+
'Handles expression functions': {
93+
snapshot: true,
94+
code: `
95+
describe('.add', () => {
96+
it('returns 1 when given 0 and 1', () =>
97+
expect(add(1, 0)).toEqual(1));
98+
});
99+
`
91100
}
92101
}
93102
});

0 commit comments

Comments
 (0)