1+ const generate = require ( 'babel-generator' ) . default ;
2+
13const 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 ) ;
0 commit comments