Skip to content

Commit c1b430b

Browse files
azuclaude
andcommitted
test: add unit tests for async IIFE patterns
- Add comprehensive test cases for async immediately invoked function expressions - Test basic async/await patterns within IIFE - Test multiple awaits, error handling, and complex async operations - Test nested async functions and multiple independent IIFEs - Verify error detection for mismatched expectations - All tests use English descriptions for consistency 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent a9f2a65 commit c1b430b

File tree

1 file changed

+210
-0
lines changed

1 file changed

+210
-0
lines changed
Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
import * as assert from "node:assert";
2+
import { runPowerDoctest } from "../src/power-doctest.js";
3+
4+
describe("Async IIFE (Immediately Invoked Function Expression) patterns", () => {
5+
it("should handle basic async IIFE pattern", async () => {
6+
const content = `
7+
(async function() {
8+
const result = await Promise.resolve("basic async processing");
9+
console.log(result); // => "basic async processing"
10+
})();
11+
`;
12+
const results = await runPowerDoctest({
13+
content,
14+
contentType: "javascript",
15+
filePath: "./test-async-iife.js",
16+
packageDir: process.cwd(),
17+
packageJSON: undefined,
18+
disableRunning: false,
19+
});
20+
21+
assert.strictEqual(results.length, 1);
22+
assert.strictEqual(results[0].status, "fulfilled");
23+
});
24+
25+
it("should handle async IIFE with multiple awaits", async () => {
26+
const content = `
27+
(async function() {
28+
const first = await Promise.resolve("first result");
29+
const second = await Promise.resolve("second result");
30+
31+
console.log(first); // => "first result"
32+
console.log(second); // => "second result"
33+
34+
const combined = \`\${first} + \${second}\`;
35+
console.log(combined); // => "first result + second result"
36+
})();
37+
`;
38+
const results = await runPowerDoctest({
39+
content,
40+
contentType: "javascript",
41+
filePath: "./test-multiple-await.js",
42+
packageDir: process.cwd(),
43+
packageJSON: undefined,
44+
disableRunning: false,
45+
});
46+
47+
assert.strictEqual(results.length, 1);
48+
assert.strictEqual(results[0].status, "fulfilled");
49+
});
50+
51+
it("should handle async IIFE with error handling", async () => {
52+
const content = `
53+
(async function() {
54+
try {
55+
const result = await Promise.resolve("successful result");
56+
console.log(result); // => "successful result"
57+
} catch (error) {
58+
console.log("Error occurred:", error.message);
59+
}
60+
})();
61+
`;
62+
const results = await runPowerDoctest({
63+
content,
64+
contentType: "javascript",
65+
filePath: "./test-error-handling.js",
66+
packageDir: process.cwd(),
67+
packageJSON: undefined,
68+
disableRunning: false,
69+
});
70+
71+
assert.strictEqual(results.length, 1);
72+
assert.strictEqual(results[0].status, "fulfilled");
73+
});
74+
75+
it("should handle async IIFE with time-consuming operations", async () => {
76+
const content = `
77+
(async function() {
78+
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
79+
80+
await delay(50); // wait 50ms
81+
const result = "time-consuming process completed";
82+
console.log(result); // => "time-consuming process completed"
83+
})();
84+
`;
85+
const results = await runPowerDoctest({
86+
content,
87+
contentType: "javascript",
88+
filePath: "./test-delay.js",
89+
packageDir: process.cwd(),
90+
packageJSON: undefined,
91+
disableRunning: false,
92+
});
93+
94+
assert.strictEqual(results.length, 1);
95+
assert.strictEqual(results[0].status, "fulfilled");
96+
});
97+
98+
it("should handle async IIFE with complex async operations", async () => {
99+
const content = `
100+
(async function() {
101+
const fetchData = async (id) => {
102+
// simulate async data fetching
103+
await new Promise(resolve => setTimeout(resolve, 10));
104+
return { id, data: \`data\${id}\` };
105+
};
106+
107+
const data1 = await fetchData(1);
108+
const data2 = await fetchData(2);
109+
110+
console.log(data1); // => { id: 1, data: "data1" }
111+
console.log(data2); // => { id: 2, data: "data2" }
112+
113+
const combined = [data1, data2];
114+
console.log(combined.length); // => 2
115+
})();
116+
`;
117+
const results = await runPowerDoctest({
118+
content,
119+
contentType: "javascript",
120+
filePath: "./test-complex-async.js",
121+
packageDir: process.cwd(),
122+
packageJSON: undefined,
123+
disableRunning: false,
124+
});
125+
126+
assert.strictEqual(results.length, 1);
127+
assert.strictEqual(results[0].status, "fulfilled");
128+
});
129+
130+
it("should properly detect error cases", async () => {
131+
const content = `
132+
(async function() {
133+
const result = await Promise.resolve("actual value");
134+
console.log(result); // => "wrong expected value"
135+
})();
136+
`;
137+
const results = await runPowerDoctest({
138+
content,
139+
contentType: "javascript",
140+
filePath: "./test-error-case.js",
141+
packageDir: process.cwd(),
142+
packageJSON: undefined,
143+
disableRunning: false,
144+
});
145+
146+
assert.strictEqual(results.length, 1);
147+
assert.strictEqual(results[0].status, "rejected");
148+
149+
if (results[0].status === "rejected") {
150+
const error = results[0].reason;
151+
assert.ok(error && error.message.includes("strictEqual"));
152+
}
153+
});
154+
155+
it("should handle nested async IIFE", async () => {
156+
const content = `
157+
(async function() {
158+
const outerResult = await Promise.resolve("outer result");
159+
console.log(outerResult); // => "outer result"
160+
161+
const innerFunction = async () => {
162+
const innerResult = await Promise.resolve("inner result");
163+
return innerResult;
164+
};
165+
166+
const inner = await innerFunction();
167+
console.log(inner); // => "inner result"
168+
})();
169+
`;
170+
const results = await runPowerDoctest({
171+
content,
172+
contentType: "javascript",
173+
filePath: "./test-nested.js",
174+
packageDir: process.cwd(),
175+
packageJSON: undefined,
176+
disableRunning: false,
177+
});
178+
179+
assert.strictEqual(results.length, 1);
180+
assert.strictEqual(results[0].status, "fulfilled");
181+
});
182+
183+
it("should handle multiple independent async IIFEs", async () => {
184+
const content = `
185+
(async function() {
186+
const result1 = await Promise.resolve("first function");
187+
console.log(result1); // => "first function"
188+
})();
189+
190+
(async function() {
191+
const result2 = await Promise.resolve("second function");
192+
console.log(result2); // => "second function"
193+
})();
194+
`;
195+
const results = await runPowerDoctest({
196+
content,
197+
contentType: "javascript",
198+
filePath: "./test-multiple-iife.js",
199+
packageDir: process.cwd(),
200+
packageJSON: undefined,
201+
disableRunning: false,
202+
});
203+
204+
// Multiple IIFEs may return multiple results
205+
assert.ok(results.length >= 1);
206+
results.forEach((result) => {
207+
assert.strictEqual(result.status, "fulfilled");
208+
});
209+
});
210+
});

0 commit comments

Comments
 (0)