Skip to content

Commit 6e21ebe

Browse files
hiddentaomattphillips
authored andcommitted
Ignore commented-out expect statements (#2)
* ignore commented out expect statements * update README * update README and contributor list * updated tests * update contributors * more icons lol
1 parent 7035d9f commit 6e21ebe

File tree

7 files changed

+200
-5
lines changed

7 files changed

+200
-5
lines changed

.all-contributorsrc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@
1818
"infra",
1919
"test"
2020
]
21+
},
22+
{
23+
"login": "hiddentao",
24+
"name": "Ramesh Nair",
25+
"avatar_url": "https://avatars0.githubusercontent.com/u/266594?v=4",
26+
"profile": "https://hiddentao.com/",
27+
"contributions": [
28+
"code",
29+
"doc",
30+
"example",
31+
"test"
32+
]
2133
}
2234
]
2335
}

README.md

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,37 @@ it('counts multiple assertions too', async () => {
140140
});
141141
```
142142

143+
**Comments are ignored**
144+
```js
145+
it('ignores commented-out assertions', async () => {
146+
const res = await fetch('www.example.com');
147+
// expect(res.json).toBeTruthy();
148+
const json = await res.json();
149+
/*
150+
expect(json).toEqual({ whatever: 'trevor1' });
151+
*/
152+
expect(json).toEqual({ whatever: 'trevor' });
153+
/* expect(json).toEqual({ whatever: 'trevor2' }); */
154+
});
155+
```
156+
157+
`↓ ↓ ↓ ↓ ↓ ↓`
158+
159+
```js
160+
it('counts multiple assertions too', async () => {
161+
expect.hasAssertions();
162+
expect.assertions(1);
163+
const res = await fetch('www.example.com');
164+
// expect(res.json).toBeTruthy();
165+
const json = await res.json();
166+
/*
167+
expect(json).toEqual({ whatever: 'trevor1' });
168+
*/
169+
expect(json).toEqual({ whatever: 'trevor' });
170+
/* expect(json).toEqual({ whatever: 'trevor2' }); */
171+
});
172+
```
173+
143174
### Override
144175

145176
If you add either `expect.assertions(number)` or `expect.hasAssertions()` then your defaults will be favoured and the
@@ -180,8 +211,8 @@ it('will leave test as override supplied', () => {
180211
## Contributors
181212

182213
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
183-
| [<img src="https://avatars0.githubusercontent.com/u/5610087?v=4" width="100px;"/><br /><sub>Matt Phillips</sub>](http://mattphillips.io)<br />[💻](https://github.com/mattphillips/babel-jest-assertions/commits?author=mattphillips "Code") [📖](https://github.com/mattphillips/babel-jest-assertions/commits?author=mattphillips "Documentation") [🚇](#infra-mattphillips "Infrastructure (Hosting, Build-Tools, etc)") [⚠️](https://github.com/mattphillips/babel-jest-assertions/commits?author=mattphillips "Tests") |
184-
| :---: |
214+
| [<img src="https://avatars0.githubusercontent.com/u/5610087?v=4" width="100px;"/><br /><sub>Matt Phillips</sub>](http://mattphillips.io)<br />[💻](https://github.com/mattphillips/babel-jest-assertions/commits?author=mattphillips "Code") [📖](https://github.com/mattphillips/babel-jest-assertions/commits?author=mattphillips "Documentation") [🚇](#infra-mattphillips "Infrastructure (Hosting, Build-Tools, etc)") [⚠️](https://github.com/mattphillips/babel-jest-assertions/commits?author=mattphillips "Tests") | [<img src="https://avatars0.githubusercontent.com/u/266594?v=4" width="100px;"/><br /><sub>Ramesh Nair</sub>](https://hiddentao.com/)<br />[💻](https://github.com/mattphillips/babel-jest-assertions/commits?author=hiddentao "Code") [📖](https://github.com/mattphillips/babel-jest-assertions/commits?author=hiddentao "Documentation") [💡](#example-hiddentao "Examples") [⚠️](https://github.com/mattphillips/babel-jest-assertions/commits?author=hiddentao "Tests") |
215+
| :---: | :---: |
185216
<!-- ALL-CONTRIBUTORS-LIST:END -->
186217

187218
## LICENSE

src/__snapshots__/async.test.js.snap

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,3 +217,56 @@ describe('.add', () => {
217217
});
218218
"
219219
`;
220+
221+
exports[`Does not count commented out expect statements 1`] = `
222+
"
223+
describe('.add', () => {
224+
it('resolves 1', async () => {
225+
// expect(add(1, 2)).toEqual(3);
226+
try {
227+
/*
228+
expect(add(4, 5).toEqual(9));
229+
*/
230+
const value = await Promise.resolve(add(0, 1));
231+
232+
const a = 1; // expect(a).toEqual(1);
233+
const b = 2; /* expect(b).toEqual(2); */
234+
235+
expect(value).toEqual(1);
236+
// expect(value).toEqual(2);
237+
} catch (err) {
238+
/*
239+
expect(add(6, 1).toEqual(7));
240+
*/
241+
}
242+
});
243+
});
244+
245+
↓ ↓ ↓ ↓ ↓ ↓
246+
247+
describe('.add', () => {
248+
it('resolves 1', async () => {
249+
expect.assertions(1);
250+
expect.hasAssertions();
251+
252+
// expect(add(1, 2)).toEqual(3);
253+
try {
254+
/*
255+
expect(add(4, 5).toEqual(9));
256+
*/
257+
const value = await Promise.resolve(add(0, 1));
258+
259+
const a = 1; // expect(a).toEqual(1);
260+
const b = 2; /* expect(b).toEqual(2); */
261+
262+
expect(value).toEqual(1);
263+
// expect(value).toEqual(2);
264+
} catch (err) {
265+
/*
266+
expect(add(6, 1).toEqual(7));
267+
*/
268+
}
269+
});
270+
});
271+
"
272+
`;

src/__snapshots__/sync.test.js.snap

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,50 @@ describe('.add', () => {
116116
});
117117
"
118118
`;
119+
120+
exports[`Does not count commented out expect statements 1`] = `
121+
"
122+
describe('.add', () => {
123+
it('returns 1 when given 0 and 1', () => {
124+
// expect(add(1, 2)).toEqual(3);
125+
expect(add(0, 1)).toEqual(1);
126+
127+
/*
128+
expect(add(4, 5).toEqual(9));
129+
*/
130+
131+
const a = 1; // expect(a).toEqual(1);
132+
const b = 2; /* expect(b).toEqual(2); */
133+
134+
expect(add(1, 0)).toEqual(1);
135+
/*
136+
expect(add(6, 1).toEqual(7));
137+
*/
138+
});
139+
});
140+
141+
↓ ↓ ↓ ↓ ↓ ↓
142+
143+
describe('.add', () => {
144+
it('returns 1 when given 0 and 1', () => {
145+
expect.assertions(2);
146+
expect.hasAssertions();
147+
148+
// expect(add(1, 2)).toEqual(3);
149+
expect(add(0, 1)).toEqual(1);
150+
151+
/*
152+
expect(add(4, 5).toEqual(9));
153+
*/
154+
155+
const a = 1; // expect(a).toEqual(1);
156+
const b = 2; /* expect(b).toEqual(2); */
157+
158+
expect(add(1, 0)).toEqual(1);
159+
/*
160+
expect(add(6, 1).toEqual(7));
161+
*/
162+
});
163+
});
164+
"
165+
`;

src/async.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,32 @@ pluginTester({
111111
});
112112
});
113113
`
114+
},
115+
'Does not count commented out expect statements': {
116+
snapshot: true,
117+
code: `
118+
describe('.add', () => {
119+
it('resolves 1', async () => {
120+
// expect(add(1, 2)).toEqual(3);
121+
try {
122+
/*
123+
expect(add(4, 5).toEqual(9));
124+
*/
125+
const value = await Promise.resolve(add(0, 1));
126+
127+
const a = 1; // expect(a).toEqual(1);
128+
const b = 2; /* expect(b).toEqual(2); */
129+
130+
expect(value).toEqual(1);
131+
// expect(value).toEqual(2);
132+
} catch (err) {
133+
/*
134+
expect(add(6, 1).toEqual(7));
135+
*/
136+
}
137+
});
138+
});
139+
`
114140
}
115141
}
116142
});

src/index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,12 @@ module.exports = function({ template, transformFromAst, types }) {
4040

4141
const { code } = transformFromAst(types.Program([path.node]));
4242

43-
const count = (code.match(/expect\(/g) || []).length;
44-
const containsExpectAssertions = code.includes('expect.assertions(');
45-
const containsHasAssertions = code.includes('expect.hasAssertions()');
43+
// remove comments
44+
const normalisedCode = code.replace(/\/\/(.*)/g, '').replace(/\/\*([\s\S]*?)\*\//g, '');
45+
46+
const count = (normalisedCode.match(/expect\(/g) || []).length;
47+
const containsExpectAssertions = normalisedCode.includes('expect.assertions(');
48+
const containsHasAssertions = normalisedCode.includes('expect.hasAssertions()');
4649

4750
const args = path.node.expression.arguments[1].body.body;
4851
if (!containsHasAssertions) {

src/sync.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,29 @@ pluginTester({
6565
});
6666
});
6767
`
68+
},
69+
'Does not count commented out expect statements': {
70+
snapshot: true,
71+
code: `
72+
describe('.add', () => {
73+
it('returns 1 when given 0 and 1', () => {
74+
// expect(add(1, 2)).toEqual(3);
75+
expect(add(0, 1)).toEqual(1);
76+
77+
/*
78+
expect(add(4, 5).toEqual(9));
79+
*/
80+
81+
const a = 1; // expect(a).toEqual(1);
82+
const b = 2; /* expect(b).toEqual(2); */
83+
84+
expect(add(1, 0)).toEqual(1);
85+
/*
86+
expect(add(6, 1).toEqual(7));
87+
*/
88+
});
89+
});
90+
`
6891
}
6992
}
7093
});

0 commit comments

Comments
 (0)