readme-assert 7.2.2 → 7.3.0

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "readme-assert",
3
- "version": "7.2.2",
3
+ "version": "7.3.0",
4
4
  "description": "Run code blocks in your readme as tests",
5
5
  "keywords": [
6
6
  "readme",
@@ -13,6 +13,9 @@ import MagicString from "magic-string";
13
13
  * expr // rejects /pat/ → assert.rejects(() => expr, /pat/)
14
14
  * expr //=> rejects Error: msg → assert.rejects(() => expr, { message: "msg" })
15
15
  *
16
+ * When the expression is an AwaitExpression, throws/Error assertions are
17
+ * promoted to async rejects (await converts rejection → throw).
18
+ *
16
19
  * Uses oxc-parser for AST + comment extraction. Handles both JS and TS.
17
20
  *
18
21
  * @param {string} code - JavaScript or TypeScript source
@@ -34,6 +37,8 @@ export function commentToAssert(code, { typescript = false } = {}) {
34
37
  const comment = findTrailingComment(comments, node, code);
35
38
  if (!comment) continue;
36
39
 
40
+ const isAwait = node.expression.type === "AwaitExpression";
41
+
37
42
  const match = comment.value.match(/^\s*(=>|→|->)\s*([\s\S]*)$/);
38
43
  const throwsMatch = comment.value.match(/^\s*throws\s+([\s\S]*)$/);
39
44
  const rejectsMatch = comment.value.match(/^\s*rejects\s+([\s\S]*)$/);
@@ -75,10 +80,13 @@ export function commentToAssert(code, { typescript = false } = {}) {
75
80
  s.overwrite(
76
81
  node.start,
77
82
  comment.end,
78
- `await assert.rejects(() => ${exprSource}, { ${props.join(", ")} });`,
83
+ isAwait
84
+ ? `await assert.rejects(async () => { ${exprSource}; }, { ${props.join(", ")} });`
85
+ : `await assert.rejects(() => ${exprSource}, { ${props.join(", ")} });`,
79
86
  );
80
87
  } else if (errorMatch) {
81
88
  // expr //=> TypeError: msg → assert.throws(() => { expr }, { name, message })
89
+ // await expr //=> Error: msg → assert.rejects(async () => { expr }, { name, message })
82
90
  const errorName = errorMatch[1];
83
91
  const errorMessage = errorMatch[2]?.trim();
84
92
  const exprSource = code.slice(
@@ -92,7 +100,9 @@ export function commentToAssert(code, { typescript = false } = {}) {
92
100
  s.overwrite(
93
101
  node.start,
94
102
  comment.end,
95
- `assert.throws(() => { ${exprSource}; }, { ${props.join(", ")} });`,
103
+ isAwait
104
+ ? `await assert.rejects(async () => { ${exprSource}; }, { ${props.join(", ")} });`
105
+ : `assert.throws(() => { ${exprSource}; }, { ${props.join(", ")} });`,
96
106
  );
97
107
  } else if (isConsoleCall(node.expression)) {
98
108
  // console.log(expr) //=> value → keep log, add assertion after.
@@ -128,7 +138,9 @@ export function commentToAssert(code, { typescript = false } = {}) {
128
138
  s.overwrite(
129
139
  node.start,
130
140
  comment.end,
131
- `assert.throws(() => { ${exprSource}; }, ${pattern});`,
141
+ isAwait
142
+ ? `await assert.rejects(async () => { ${exprSource}; }, ${pattern});`
143
+ : `assert.throws(() => { ${exprSource}; }, ${pattern});`,
132
144
  );
133
145
  changed = true;
134
146
  } else if (rejectsMatch) {
@@ -140,7 +152,9 @@ export function commentToAssert(code, { typescript = false } = {}) {
140
152
  s.overwrite(
141
153
  node.start,
142
154
  comment.end,
143
- `await assert.rejects(() => ${exprSource}, ${pattern});`,
155
+ isAwait
156
+ ? `await assert.rejects(async () => { ${exprSource}; }, ${pattern});`
157
+ : `await assert.rejects(() => ${exprSource}, ${pattern});`,
144
158
  );
145
159
  changed = true;
146
160
  }
package/src/generate.js CHANGED
@@ -66,7 +66,9 @@ function assembleUnit(blocks) {
66
66
  }
67
67
 
68
68
  const hasESM = imports.length > 0;
69
- const hasCJS = /\brequire\s*\(/.test(bodyLines.join("\n"));
69
+ const body = bodyLines.join("\n");
70
+ const hasAwait = /\bawait\s/.test(body);
71
+ const hasCJS = !hasAwait && /\brequire\s*\(/.test(body);
70
72
 
71
73
  // Place assert import on line 0 (before markdown line 1) so line numbers
72
74
  // in the generated code match the original markdown positions exactly.