|
| 1 | +// Copyright 2018-2024 the Deno authors. MIT license. |
| 2 | + |
| 3 | +import { assertEquals, assertThrows } from "@std/assert"; |
| 4 | +import { |
| 5 | + resolvePolyfillOptions, |
| 6 | + resolveUseImportMetaPolyfill, |
| 7 | +} from "./polyfills.ts"; |
| 8 | + |
| 9 | +Deno.test("resolvePolyfillOptions - undefined leaves everything to the target", () => { |
| 10 | + assertEquals(resolvePolyfillOptions(undefined), {}); |
| 11 | +}); |
| 12 | + |
| 13 | +Deno.test("resolvePolyfillOptions - boolean applies to every polyfill", () => { |
| 14 | + assertEquals(resolvePolyfillOptions(false), { |
| 15 | + arrayFindLast: false, |
| 16 | + arrayFromAsync: false, |
| 17 | + errorCause: false, |
| 18 | + importMeta: false, |
| 19 | + objectHasOwn: false, |
| 20 | + promiseWithResolvers: false, |
| 21 | + stringReplaceAll: false, |
| 22 | + }); |
| 23 | + assertEquals( |
| 24 | + Object.values(resolvePolyfillOptions(true)).every((v) => v), |
| 25 | + true, |
| 26 | + ); |
| 27 | +}); |
| 28 | + |
| 29 | +Deno.test("resolvePolyfillOptions - object only includes what's specified", () => { |
| 30 | + assertEquals( |
| 31 | + resolvePolyfillOptions({ importMeta: false, errorCause: true }), |
| 32 | + { importMeta: false, errorCause: true }, |
| 33 | + ); |
| 34 | +}); |
| 35 | + |
| 36 | +Deno.test("resolvePolyfillOptions - undefined values fall back to the target", () => { |
| 37 | + assertEquals( |
| 38 | + resolvePolyfillOptions({ importMeta: undefined, errorCause: true }), |
| 39 | + { errorCause: true }, |
| 40 | + ); |
| 41 | +}); |
| 42 | + |
| 43 | +Deno.test("resolvePolyfillOptions - throws for an unknown polyfill", () => { |
| 44 | + assertThrows( |
| 45 | + () => |
| 46 | + resolvePolyfillOptions( |
| 47 | + { importMata: false } as Record<string, boolean>, |
| 48 | + ), |
| 49 | + Error, |
| 50 | + "Unknown polyfill 'importMata'", |
| 51 | + ); |
| 52 | +}); |
| 53 | + |
| 54 | +Deno.test("resolveUseImportMetaPolyfill - required when emitting a script module", () => { |
| 55 | + assertEquals( |
| 56 | + resolveUseImportMetaPolyfill({ |
| 57 | + polyfills: {}, |
| 58 | + target: "Latest", |
| 59 | + emitScriptModule: true, |
| 60 | + }), |
| 61 | + true, |
| 62 | + ); |
| 63 | +}); |
| 64 | + |
| 65 | +Deno.test("resolveUseImportMetaPolyfill - throws when disabled with a script module", () => { |
| 66 | + assertThrows( |
| 67 | + () => |
| 68 | + resolveUseImportMetaPolyfill({ |
| 69 | + polyfills: { importMeta: false }, |
| 70 | + target: "ES2021", |
| 71 | + emitScriptModule: true, |
| 72 | + }), |
| 73 | + Error, |
| 74 | + "cannot be disabled when emitting a script module", |
| 75 | + ); |
| 76 | +}); |
| 77 | + |
| 78 | +Deno.test("resolveUseImportMetaPolyfill - esm only follows the target by default", () => { |
| 79 | + assertEquals( |
| 80 | + resolveUseImportMetaPolyfill({ |
| 81 | + polyfills: {}, |
| 82 | + target: "ES2021", |
| 83 | + emitScriptModule: false, |
| 84 | + }), |
| 85 | + true, |
| 86 | + ); |
| 87 | + assertEquals( |
| 88 | + resolveUseImportMetaPolyfill({ |
| 89 | + polyfills: {}, |
| 90 | + target: "Latest", |
| 91 | + emitScriptModule: false, |
| 92 | + }), |
| 93 | + false, |
| 94 | + ); |
| 95 | +}); |
| 96 | + |
| 97 | +Deno.test("resolveUseImportMetaPolyfill - esm only respects an explicit override", () => { |
| 98 | + assertEquals( |
| 99 | + resolveUseImportMetaPolyfill({ |
| 100 | + polyfills: { importMeta: false }, |
| 101 | + target: "ES2021", |
| 102 | + emitScriptModule: false, |
| 103 | + }), |
| 104 | + false, |
| 105 | + ); |
| 106 | + assertEquals( |
| 107 | + resolveUseImportMetaPolyfill({ |
| 108 | + polyfills: { importMeta: true }, |
| 109 | + target: "Latest", |
| 110 | + emitScriptModule: false, |
| 111 | + }), |
| 112 | + true, |
| 113 | + ); |
| 114 | +}); |
0 commit comments