repeated-imports.any.js (3640B)
1 // META: global=window,dedicatedworker,sharedworker 2 // META: script=/common/utils.js 3 4 promise_test(async test => { 5 await promise_rejects_js(test, TypeError, 6 import("./module.json"), 7 "Dynamic import of a JSON module without a type attribute should fail"); 8 9 // This time the import should succeed because we're using the correct 10 // import even though the previous attempt with the same specifier failed. 11 const result = await import("./module.json", { with: { type: "json" } }); 12 assert_true(result.default.test); 13 }, "Importing a specifier that previously failed due to an incorrect type attribute can succeed if the correct attribute is later given"); 14 15 promise_test(async test => { 16 // Append a URL fragment to the specifier so that this is independent 17 // from the previous test. 18 const result = await import("./module.json#2", { with: { type: "json" } }); 19 assert_true(result.default.test); 20 21 await promise_rejects_js(test, TypeError, 22 import("./module.json#2"), 23 "Dynamic import should fail with the type attribute missing even if the same specifier previously succeeded"); 24 }, "Importing a specifier that previously succeeded with the correct type attribute should fail if the incorrect attribute is later given"); 25 26 promise_test(async test => { 27 const uuid_token = token(); 28 // serve-json-then-js.py gives us JSON the first time 29 const result_json = await import(`../serve-json-then-js.py?key=${uuid_token}`, { with: { type: "json" } }); 30 assert_equals(result_json.default.hello, "world"); 31 32 // Import using the same specifier again; this time we get JS, which 33 // should succeed since we're not asserting a non-JS type this time. 34 const result_js = await import(`../serve-json-then-js.py?key=${uuid_token}`); 35 assert_equals(result_js.default, "hello"); 36 }, "Two modules of different type with the same specifier can load if the server changes its responses"); 37 38 promise_test(async test => { 39 const uuid_token = token(); 40 // serve-json-then-js.py gives us JSON the first time 41 await promise_rejects_js(test, TypeError, 42 import(`../serve-json-then-js.py?key=${uuid_token}`), 43 "Dynamic import of JS with a JSON type attribute should fail"); 44 45 // Import using the same specifier/module type pair again; this time we get JS, 46 // but the import should still fail because the module map entry for this 47 // specifier/module type pair already contains a failure. 48 await promise_rejects_js(test, TypeError, 49 import(`../serve-json-then-js.py?key=${uuid_token}`), 50 "import should always fail if the same specifier/type attribute pair failed previously"); 51 }, "An import should always fail if the same specifier/type attribute pair failed previously"); 52 53 promise_test(async test => { 54 const uuid_token = token(); 55 // serve-json-then-js.py gives us JSON the first time 56 const result_json = await import(`../serve-json-then-js.py?key=${uuid_token}`, { with: { type: "json" } }); 57 assert_equals(result_json.default.hello, "world"); 58 59 // If this were to do another fetch, the import would fail because 60 // serve-json-then-js.py would give us JS this time. But, the module map 61 // entry for this specifier/module type pair already exists, so we 62 // successfully reuse the entry instead of fetching again. 63 const result_json_2 = await import(`../serve-json-then-js.py?key=${uuid_token}`, { with: { type: "json" } }); 64 assert_equals(result_json_2.default.hello, "world"); 65 }, "If an import previously succeeded for a given specifier/type attribute pair, future uses of that pair should yield the same result");