Skip to content

Commit 31056c9

Browse files
authored
fix: make Promise.withResolvers polyfill file a module (#493)
1 parent 1734d12 commit 31056c9

5 files changed

Lines changed: 71 additions & 0 deletions

File tree

rs-lib/src/polyfills/mod.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,23 @@ impl PolyfillTester {
179179
})
180180
}
181181
}
182+
183+
#[cfg(test)]
184+
mod test {
185+
use super::*;
186+
187+
#[test]
188+
fn polyfill_scripts_are_modules() {
189+
// a polyfill may end up being the only one in the output file, so each
190+
// must be a module on its own for `declare global` to be valid
191+
for polyfill in all_polyfills() {
192+
let text = polyfill.get_file_text();
193+
assert!(
194+
text.lines().any(
195+
|line| line.starts_with("export ") || line.starts_with("import ")
196+
),
197+
"polyfill script was not a module: {text}"
198+
);
199+
}
200+
}
201+
}

rs-lib/src/polyfills/scripts/es2021.promise-withResolvers.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,5 @@ if (Promise.withResolvers === undefined) {
2020
return out;
2121
};
2222
}
23+
24+
export {};

tests/integration.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,25 @@ Deno.test("should build and test polyfill project", async () => {
927927
});
928928
});
929929

930+
Deno.test("should build and test the promise with resolvers polyfill project", async () => {
931+
// this polyfill ends up alone in the polyfill file, so ensure the generated
932+
// file is still a module and thus type checks (see #440)
933+
await runTest("polyfill_promise_with_resolvers_project", {
934+
entryPoints: ["mod.ts"],
935+
outDir: "./npm",
936+
shims: {
937+
...getAllShimOptions(false),
938+
deno: "dev",
939+
},
940+
package: {
941+
name: "polyfill-package",
942+
version: "1.0.0",
943+
},
944+
}, (output) => {
945+
output.assertExists("esm/_dnt.polyfills.js");
946+
});
947+
});
948+
930949
Deno.test("should build and test the array find last polyfill project", async () => {
931950
await runTest("polyfill_array_find_last_project", {
932951
entryPoints: ["mod.ts"],
@@ -1316,6 +1335,7 @@ async function runTest(
13161335
| "polyfill_project"
13171336
| "polyfill_array_from_async_project"
13181337
| "polyfill_array_find_last_project"
1338+
| "polyfill_promise_with_resolvers_project"
13191339
| "polyfill_import_meta_project"
13201340
| "module_mappings_project"
13211341
| "node_types_project"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// this project must only use the `Promise.withResolvers` polyfill so that it
2+
// ends up alone in the generated polyfill file (see #440)
3+
export function withResolvers<T>() {
4+
return Promise.withResolvers<T>();
5+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { withResolvers } from "./mod.ts";
2+
3+
function assertEquals(a: unknown, b: unknown) {
4+
if (a !== b) {
5+
throw new Error(`${a} did not equal ${b}`);
6+
}
7+
}
8+
9+
Deno.test("should resolve", async () => {
10+
const { promise, resolve } = withResolvers<number>();
11+
setTimeout(() => resolve(5), 10);
12+
assertEquals(await promise, 5);
13+
});
14+
15+
Deno.test("should reject", async () => {
16+
const { promise, reject } = withResolvers<number>();
17+
setTimeout(() => reject(new Error("test")), 10);
18+
try {
19+
await promise;
20+
throw new Error("Did not throw.");
21+
} catch (err) {
22+
assertEquals((err as Error).message, "test");
23+
}
24+
});

0 commit comments

Comments
 (0)