summaryrefslogtreecommitdiff
path: root/test/es-module/test-esm-import-assertion-errors.js
blob: e2abd3fb43976d3dd6302606951661df2e291988 (plain)
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
'use strict';
const common = require('../common');
const { rejects } = require('assert');

const jsModuleDataUrl = 'data:text/javascript,export{}';
const jsonModuleDataUrl = 'data:application/json,""';

common.expectWarning(
  'ExperimentalWarning',
  'Import assertions are not a stable feature of the JavaScript language. ' +
  'Avoid relying on their current behavior and syntax as those might change ' +
  'in a future version of Node.js.'
);

async function test() {
  await rejects(
    import('data:text/css,', { assert: { type: 'css' } }),
    { code: 'ERR_UNKNOWN_MODULE_FORMAT' }
  );

  await rejects(
    import(`data:text/javascript,import${JSON.stringify(jsModuleDataUrl)}assert{type:"json"}`),
    { code: 'ERR_IMPORT_ASSERTION_TYPE_FAILED' }
  );

  await rejects(
    import(jsModuleDataUrl, { assert: { type: 'json' } }),
    { code: 'ERR_IMPORT_ASSERTION_TYPE_FAILED' }
  );

  await rejects(
    import(jsModuleDataUrl, { assert: { type: 'unsupported' } }),
    { code: 'ERR_IMPORT_ASSERTION_TYPE_UNSUPPORTED' }
  );

  await rejects(
    import(jsonModuleDataUrl),
    { code: 'ERR_IMPORT_ASSERTION_TYPE_MISSING' }
  );

  await rejects(
    import(jsonModuleDataUrl, { assert: {} }),
    { code: 'ERR_IMPORT_ASSERTION_TYPE_MISSING' }
  );

  await rejects(
    import(jsonModuleDataUrl, { assert: { foo: 'bar' } }),
    { code: 'ERR_IMPORT_ASSERTION_TYPE_MISSING' }
  );

  await rejects(
    import(jsonModuleDataUrl, { assert: { type: 'unsupported' }}),
    { code: 'ERR_IMPORT_ASSERTION_TYPE_UNSUPPORTED' }
  );
}

test().then(common.mustCall());