readme-assert 7.1.0 → 7.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/readme.md +9 -0
- package/src/comment-to-assert.js +25 -0
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -71,6 +71,15 @@ const b = () => {
|
|
|
71
71
|
b(); // throws /fail/
|
|
72
72
|
```
|
|
73
73
|
|
|
74
|
+
Or use `//=>` with an error name and optional message to match both:
|
|
75
|
+
|
|
76
|
+
```javascript test
|
|
77
|
+
const c = () => {
|
|
78
|
+
throw new TypeError("bad input");
|
|
79
|
+
};
|
|
80
|
+
c(); //=> TypeError: bad input
|
|
81
|
+
```
|
|
82
|
+
|
|
74
83
|
### console.log
|
|
75
84
|
|
|
76
85
|
Assert console output — the call is preserved and an assertion is added:
|
package/src/comment-to-assert.js
CHANGED
|
@@ -7,6 +7,7 @@ import MagicString from "magic-string";
|
|
|
7
7
|
* expr //=> value → assert.deepEqual(expr, value)
|
|
8
8
|
* expr // → value → assert.deepEqual(expr, value)
|
|
9
9
|
* expr // throws /pat/ → assert.throws(() => { expr }, /pat/)
|
|
10
|
+
* expr //=> Error: msg → assert.throws(() => { expr }, { message: "msg" })
|
|
10
11
|
* console.log(x) //=> v → console.log(x); assert.deepEqual(x, v)
|
|
11
12
|
* expr //=> resolves to v → assert.deepEqual(await expr, v)
|
|
12
13
|
* expr // rejects /pat/ → assert.rejects(() => expr, /pat/)
|
|
@@ -41,6 +42,8 @@ export function commentToAssert(code, { typescript = false } = {}) {
|
|
|
41
42
|
const resolvesMatch = rest.match(/^resolves\s+(?:to\s+)?([\s\S]*)$/);
|
|
42
43
|
changed = true;
|
|
43
44
|
|
|
45
|
+
const errorMatch = rest.match(/^((?:[A-Z]\w+)?Error)(?::\s*(.*))?$/);
|
|
46
|
+
|
|
44
47
|
if (resolvesMatch) {
|
|
45
48
|
// expr //=> resolves to value → assert.deepEqual(await expr, value)
|
|
46
49
|
const expected = resolvesMatch[1].trim();
|
|
@@ -53,6 +56,28 @@ export function commentToAssert(code, { typescript = false } = {}) {
|
|
|
53
56
|
comment.end,
|
|
54
57
|
`assert.deepEqual(await ${exprSource}, ${expected});`,
|
|
55
58
|
);
|
|
59
|
+
} else if (errorMatch) {
|
|
60
|
+
// expr //=> TypeError: msg → assert.throws(() => { expr }, { name, message })
|
|
61
|
+
const errorName = errorMatch[1];
|
|
62
|
+
const errorMessage = errorMatch[2]?.trim();
|
|
63
|
+
const exprSource = code.slice(
|
|
64
|
+
node.expression.start,
|
|
65
|
+
node.expression.end,
|
|
66
|
+
);
|
|
67
|
+
const props = [`name: "${errorName}"`];
|
|
68
|
+
if (errorMessage) {
|
|
69
|
+
const reMatch = errorMessage.match(/^\/(.+)\/([gimsuy]*)$/);
|
|
70
|
+
props.push(
|
|
71
|
+
reMatch
|
|
72
|
+
? `message: /${reMatch[1]}/${reMatch[2]}`
|
|
73
|
+
: `message: "${errorMessage}"`,
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
s.overwrite(
|
|
77
|
+
node.start,
|
|
78
|
+
comment.end,
|
|
79
|
+
`assert.throws(() => { ${exprSource}; }, { ${props.join(", ")} });`,
|
|
80
|
+
);
|
|
56
81
|
} else if (isConsoleCall(node.expression)) {
|
|
57
82
|
// console.log(expr) //=> value → keep log, add assertion after.
|
|
58
83
|
// Stay on the same line so subsequent markdown line numbers are
|