1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
# This file is running in CommonJS (in Node) or as a classic Script (in the browser tests) so it can use import() within an async function, but not at the top level; and we can’t use static import.
test "dynamic import assertion", ->
try
{ default: secret } = await import('data:application/json,{"ofLife":42}', { assert: { type: 'json' } })
eq secret.ofLife, 42
catch exception
# This parses on Node 16.14.x but throws an error because JSON modules aren’t unflagged there yet; remove this try/catch once the unflagging of `--experimental-json-modules` is backported (see https://github.com/nodejs/node/pull/41736#issuecomment-1086738670)
unless exception.message is 'Invalid module "data:application/json,{"ofLife":42}" has an unsupported MIME type "application/json"'
throw exception
test "assert keyword", ->
assert = 1
try
{ default: assert } = await import('data:application/json,{"thatIAm":42}', { assert: { type: 'json' } })
eq assert.thatIAm, 42
catch exception
# This parses on Node 16.14.x but throws an error because JSON modules aren’t unflagged there yet; remove this try/catch once the unflagging of `--experimental-json-modules` is backported (see https://github.com/nodejs/node/pull/41736#issuecomment-1086738670)
unless exception.message is 'Invalid module "data:application/json,{"thatIAm":42}" has an unsupported MIME type "application/json"'
throw exception
eqJS """
import assert from 'regression-test'
""", """
import assert from 'regression-test';
"""
test "static import assertion", ->
eqJS """
import 'data:application/json,{"foo":3}' assert { type: 'json' }
""", """
import 'data:application/json,{"foo":3}' assert {
type: 'json'
};
"""
eqJS """
import secret from 'data:application/json,{"ofLife":42}' assert { type: 'json' }
""", """
import secret from 'data:application/json,{"ofLife":42}' assert {
type: 'json'
};
"""
eqJS """
import * as secret from 'data:application/json,{"ofLife":42}' assert { type: 'json' }
""", """
import * as secret from 'data:application/json,{"ofLife":42}' assert {
type: 'json'
};
"""
# The only file types for which import assertions are currently supported are JSON (Node and browsers) and CSS (browsers), neither of which support named exports; however there’s nothing in the JavaScript grammar preventing a future supported file type from providing named exports.
eqJS """
import { foo } from './file.unknown' assert { type: 'unknown' }
""", """
import {
foo
} from './file.unknown' assert {
type: 'unknown'
};
"""
eqJS """
import file, { foo } from './file.unknown' assert { type: 'unknown' }
""", """
import file, {
foo
} from './file.unknown' assert {
type: 'unknown'
};
"""
eqJS """
import foo from 'bar' assert {}
""", """
import foo from 'bar' assert {};
"""
test "static export with assertion", ->
eqJS """
export * from 'data:application/json,{"foo":3}' assert { type: 'json' }
""", """
export * from 'data:application/json,{"foo":3}' assert {
type: 'json'
};
"""
eqJS """
export { profile } from './user.json' assert { type: 'json' }
""", """
export {
profile
} from './user.json' assert {
type: 'json'
};
"""
|