Skip to content

feat: Support removal of async assertion such as assert.rejects #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ Object for configuration options. passed `options` is `Object.assign`ed with def

##### options.modules

Target module names for `require` and `import` call removal.
Target module names for assertion call removal.

For example, the default target modules are as follows.

Expand Down Expand Up @@ -173,12 +173,14 @@ In this default case, unassert will remove assertion calls such as,
* `assert.notDeepEqual(actual, expected, [message])`
* `assert.deepStrictEqual(actual, expected, [message])`
* `assert.notDeepStrictEqual(actual, expected, [message])`
* `assert.fail([message])`
* `assert.fail(actual, expected, message, operator)`
* `assert.match(string, regexp[, message])`
* `assert.doesNotMatch(string, regexp[, message])`
* `assert.throws(block, [error], [message])`
* `assert.doesNotThrow(block, [message])`
* `assert.rejects(asyncFn, [error], [message])`
* `assert.doesNotReject(asyncFn, [error], [message])`
* `await assert.rejects(asyncFn, [error], [message])`
* `await assert.doesNotReject(asyncFn, [error], [message])`
* `assert.fail([message])`
* `assert.fail(actual, expected, message, operator)`
* `assert.ifError(value)`

in addition, unassert removes `console.assert` calls as well.
Expand Down Expand Up @@ -251,7 +253,7 @@ import { strict as powerAssert } from 'power-assert';
import { default as looseAssert } from 'node:assert';
import strictAssert, { ok, equal as eq } from 'node:assert/strict';

function add (a, b) {
async function add (a, b) {
strictAssert(!isNaN(a));
looseAssert(typeof a === 'number');
eq(typeof b, 'number');
Expand All @@ -267,14 +269,17 @@ function add (a, b) {
invariant(someTruthyVal, 'This will not throw');
invariant(someFalseyVal, 'This will throw an error with this message');

await strictAssert.rejects(prms);
await strictAssert.doesNotReject(prms2);

return a + b;
}
```

output:

```javascript
function add(a, b) {
async function add(a, b) {
return a + b;
}
```
Expand Down
12 changes: 12 additions & 0 deletions src/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,18 @@ function createVisitor (options) {
}
break;
}
case syntax.AwaitExpression: {
const childNode = currentNode.argument;
if (isExpressionStatement(parentNode) && isCallExpression(childNode)) {
const callee = childNode.callee;
if (isAssertionFunction(callee) || isAssertionMethod(callee) || isConsoleAssert(callee)) {
// remove parent ExpressionStatement
nodeToRemove.add(parentNode);
this.skip();
}
}
break;
}
}
},
leave: function (currentNode, parentNode) {
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/various_assertion_methods/expected.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict';
function add(a, b) {
async function add(a, b) {
return a + b;
}
6 changes: 5 additions & 1 deletion test/fixtures/various_assertion_methods/fixture.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

function add (a, b) {
async function add (a, b) {
console.assert(typeof a === 'number');

assert(!isNaN(a));
Expand Down Expand Up @@ -54,5 +54,9 @@ function add (a, b) {

assert.ifError(a);
assert.fail(a, b, 'assertion message', '==');

await assert.rejects(prms);
await assert.doesNotReject(prms2);

return a + b;
}
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