Skip to content

Commit aa73d71

Browse files
authored
Merge pull request #36 from unassert-js/await-expression
feat: Support removal of async assertion such as `assert.rejects`
2 parents bec47b9 + 1db72c2 commit aa73d71

File tree

4 files changed

+30
-9
lines changed

4 files changed

+30
-9
lines changed

README.md

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ Object for configuration options. passed `options` is `Object.assign`ed with def
115115
116116
##### options.modules
117117
118-
Target module names for `require` and `import` call removal.
118+
Target module names for assertion call removal.
119119
120120
For example, the default target modules are as follows.
121121
@@ -173,12 +173,14 @@ In this default case, unassert will remove assertion calls such as,
173173
* `assert.notDeepEqual(actual, expected, [message])`
174174
* `assert.deepStrictEqual(actual, expected, [message])`
175175
* `assert.notDeepStrictEqual(actual, expected, [message])`
176-
* `assert.fail([message])`
177-
* `assert.fail(actual, expected, message, operator)`
176+
* `assert.match(string, regexp[, message])`
177+
* `assert.doesNotMatch(string, regexp[, message])`
178178
* `assert.throws(block, [error], [message])`
179179
* `assert.doesNotThrow(block, [message])`
180-
* `assert.rejects(asyncFn, [error], [message])`
181-
* `assert.doesNotReject(asyncFn, [error], [message])`
180+
* `await assert.rejects(asyncFn, [error], [message])`
181+
* `await assert.doesNotReject(asyncFn, [error], [message])`
182+
* `assert.fail([message])`
183+
* `assert.fail(actual, expected, message, operator)`
182184
* `assert.ifError(value)`
183185
184186
in addition, unassert removes `console.assert` calls as well.
@@ -251,7 +253,7 @@ import { strict as powerAssert } from 'power-assert';
251253
import { default as looseAssert } from 'node:assert';
252254
import strictAssert, { ok, equal as eq } from 'node:assert/strict';
253255

254-
function add (a, b) {
256+
async function add (a, b) {
255257
strictAssert(!isNaN(a));
256258
looseAssert(typeof a === 'number');
257259
eq(typeof b, 'number');
@@ -267,14 +269,17 @@ function add (a, b) {
267269
invariant(someTruthyVal, 'This will not throw');
268270
invariant(someFalseyVal, 'This will throw an error with this message');
269271

272+
await strictAssert.rejects(prms);
273+
await strictAssert.doesNotReject(prms2);
274+
270275
return a + b;
271276
}
272277
```
273278
274279
output:
275280
276281
```javascript
277-
function add(a, b) {
282+
async function add(a, b) {
278283
return a + b;
279284
}
280285
```

src/index.mjs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,18 @@ function createVisitor (options) {
228228
}
229229
break;
230230
}
231+
case syntax.AwaitExpression: {
232+
const childNode = currentNode.argument;
233+
if (isExpressionStatement(parentNode) && isCallExpression(childNode)) {
234+
const callee = childNode.callee;
235+
if (isAssertionFunction(callee) || isAssertionMethod(callee) || isConsoleAssert(callee)) {
236+
// remove parent ExpressionStatement
237+
nodeToRemove.add(parentNode);
238+
this.skip();
239+
}
240+
}
241+
break;
242+
}
231243
}
232244
},
233245
leave: function (currentNode, parentNode) {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
'use strict';
2-
function add(a, b) {
2+
async function add(a, b) {
33
return a + b;
44
}

test/fixtures/various_assertion_methods/fixture.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const assert = require('node:assert');
44

5-
function add (a, b) {
5+
async function add (a, b) {
66
console.assert(typeof a === 'number');
77

88
assert(!isNaN(a));
@@ -54,5 +54,9 @@ function add (a, b) {
5454

5555
assert.ifError(a);
5656
assert.fail(a, b, 'assertion message', '==');
57+
58+
await assert.rejects(prms);
59+
await assert.doesNotReject(prms2);
60+
5761
return a + b;
5862
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy