summaryrefslogtreecommitdiff
path: root/test/es-module/test-esm-loader-resolve-type.mjs
blob: 482320c664c5d8e852233aae3fc1c7858f844002 (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
// Flags: --loader ./test/fixtures/es-module-loaders/hook-resolve-type.mjs
import { allowGlobals } from '../common/index.mjs';
import * as fixtures from '../common/fixtures.mjs';
import { strict as assert } from 'assert';
import * as fs from 'fs';

allowGlobals(global.getModuleTypeStats);

const { importedESM: importedESMBefore,
        importedCJS: importedCJSBefore } = await global.getModuleTypeStats();

const basePath =
  new URL('./node_modules/', import.meta.url);

const rel = (file) => new URL(file, basePath);
const createDir = (path) => {
  if (!fs.existsSync(path)) {
    fs.mkdirSync(path);
  }
};

const moduleName = 'module-counter-by-type';
const moduleDir = rel(`${moduleName}`);
try {
  createDir(basePath);
  createDir(moduleDir);
  fs.cpSync(
    fixtures.path('es-modules', moduleName),
    moduleDir,
    { recursive: true }
  );


  await import(`${moduleName}`);
} finally {
  fs.rmSync(basePath, { recursive: true, force: true });
}

const { importedESM: importedESMAfter,
        importedCJS: importedCJSAfter } = await global.getModuleTypeStats();

// Dynamic import above should increment ESM counter but not CJS counter
assert.strictEqual(importedESMBefore + 1, importedESMAfter);
assert.strictEqual(importedCJSBefore, importedCJSAfter);