@@ -51,24 +51,28 @@ const HELPER = `function _avaThrowsHelper(fn, data) {
51
51
}
52
52
}\n` ;
53
53
54
- function wrapped ( throws , expression , line , column ) {
54
+ function wrapped ( throws , line , column , expression , source = expression ) { // eslint-disable-line max-params
55
55
return `t.${ throws } (_avaThrowsHelper(function () {
56
56
return ${ expression } ;
57
57
}, {
58
58
line: ${ line } ,
59
59
column: ${ column } ,
60
- source: "${ expression } ",
60
+ source: "${ source } ",
61
61
filename: "some-file.js"
62
62
}));` ;
63
63
}
64
64
65
+ function indent ( str ) {
66
+ return str . replace ( / \n / g, '\n ' ) . trimRight ( ) ;
67
+ }
68
+
65
69
test ( 'creates a helper' , t => {
66
70
const input = 't.throws(foo())' ;
67
71
const { code} = transform ( input ) ;
68
72
69
73
const expected = [
70
74
HELPER ,
71
- wrapped ( 'throws' , 'foo()' , 1 , 9 )
75
+ wrapped ( 'throws' , 1 , 9 , 'foo()' )
72
76
] . join ( '\n' ) ;
73
77
74
78
t . is ( code , expected ) ;
@@ -81,14 +85,62 @@ test('creates the helper only once', t => {
81
85
82
86
const expected = [
83
87
HELPER ,
84
- wrapped ( 'throws' , 'foo()' , 1 , 9 ) ,
85
- wrapped ( 'throws' , 'bar()' , 2 , 9 )
88
+ wrapped ( 'throws' , 1 , 9 , 'foo()' ) ,
89
+ wrapped ( 'throws' , 2 , 9 , 'bar()' )
86
90
] . join ( '\n' ) ;
87
91
88
92
t . is ( code , expected ) ;
89
93
addExample ( input , code ) ;
90
94
} ) ;
91
95
96
+ test ( 'hoists await expressions' , t => {
97
+ const input = `async function test() {
98
+ t.throws(foo(await bar(), await baz(), qux));
99
+ t.throws(await quux);
100
+ }` ;
101
+ const { code} = transform ( input ) ;
102
+
103
+ const expected = `${ HELPER }
104
+ async function test() {
105
+ var _arg = await bar();
106
+
107
+ var _arg2 = await baz();
108
+
109
+ ${ indent ( wrapped ( 'throws' , 2 , 11 , 'foo(_arg, _arg2, qux)' , 'foo(await bar(), await baz(), qux)' ) ) }
110
+
111
+ var _arg3 = await quux;
112
+
113
+ ${ indent ( wrapped ( 'throws' , 3 , 11 , '_arg3' , 'await quux' ) ) }
114
+ }` ;
115
+
116
+ t . is ( code , expected ) ;
117
+ addExample ( input , code ) ;
118
+ } ) ;
119
+
120
+ test ( 'hoists yield expressions' , t => {
121
+ const input = `function* test() {
122
+ t.throws(foo(yield bar(), yield baz(), qux));
123
+ t.throws(yield quux);
124
+ }` ;
125
+ const { code} = transform ( input ) ;
126
+
127
+ const expected = `${ HELPER }
128
+ function* test() {
129
+ var _arg = yield bar();
130
+
131
+ var _arg2 = yield baz();
132
+
133
+ ${ indent ( wrapped ( 'throws' , 2 , 11 , 'foo(_arg, _arg2, qux)' , 'foo(yield bar(), yield baz(), qux)' ) ) }
134
+
135
+ var _arg3 = yield quux;
136
+
137
+ ${ indent ( wrapped ( 'throws' , 3 , 11 , '_arg3' , 'yield quux' ) ) }
138
+ }` ;
139
+
140
+ t . is ( code , expected ) ;
141
+ addExample ( input , code ) ;
142
+ } ) ;
143
+
92
144
test ( 'does nothing if it does not match' , t => {
93
145
const input = 't.is(foo());' ;
94
146
const { code} = transform ( input ) ;
@@ -103,7 +155,7 @@ test('helps notThrows', t => {
103
155
104
156
const expected = [
105
157
HELPER ,
106
- wrapped ( 'notThrows' , 'baz()' , 1 , 12 )
158
+ wrapped ( 'notThrows' , 1 , 12 , 'baz()' )
107
159
] . join ( '\n' ) ;
108
160
109
161
t . is ( code , expected ) ;
0 commit comments