Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MoLow committed Jul 25, 2022
1 parent ed204e6 commit a7d6bae
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
75 changes: 75 additions & 0 deletions test/es-module/test-esm-import-flag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
'use strict';

const fixtures = require('../common/fixtures');
const assert = require('assert');
const { spawnSync } = require('child_process');

{
// known limitation:
// imports should not be imported when evaluating text
const { status, stderr, stdout } = spawnSync(
process.execPath,
[
'--experimental-import',
fixtures.path('es-modules', 'mjs-file.mjs'),
'-e', 'console.log("log")'
],
{ encoding: 'utf8' },
);

assert.strictEqual(status, 0);
assert.strictEqual(stderr, '');
assert.strictEqual(stdout, 'log\n');
}

{
const { status, stderr, stdout } = spawnSync(
process.execPath,
[
'--experimental-import',
fixtures.path('es-modules', 'mjs-file.mjs'),
fixtures.path('es-modules', 'cjs-file.cjs')
],
{ encoding: 'utf8' },
);

assert.strictEqual(status, 0);
assert.strictEqual(stderr, '');
assert.strictEqual(stdout, '.mjs file\n.cjs file\n');
}

{
const { status, stderr, stdout } = spawnSync(
process.execPath,
[
'--experimental-import',
fixtures.path('es-modules', 'mjs-file.mjs'),
fixtures.path('es-modules', 'mjs-file.mjs')
],
{ encoding: 'utf8' },
);

assert.strictEqual(status, 0);
assert.strictEqual(stderr, '');
assert.strictEqual(stdout, '.mjs file\n.mjs file\n');
}

{
let start = Date.now();
const { status, stderr, stdout } = spawnSync(
process.execPath,
[
'--experimental-import',
fixtures.path('es-modules', 'esm-top-level-await.mjs'),
fixtures.path('es-modules', 'print_imported.mjs')
],
{ encoding: 'utf8' },
);

assert.strictEqual(status, 0);
assert.strictEqual(stderr, '');
const output = JSON.parse(stdout);
assert.ok(output.time >= start);
assert.ok(output.time_async > start);
assert.ok(output.time_async_delayed >= start + 100);
}
18 changes: 18 additions & 0 deletions test/fixtures/es-modules/esm-top-level-await.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { setTimeout } from 'timers/promises';

export const time = Date.now();

export const time_async = await new Promise((resolve) => {
process.nextTick(() => resolve(Date.now()))
})

export const time_async_delayed = await new Promise(async (resolve) => {
await setTimeout(100)
resolve(Date.now());
});

globalThis.imported = {
time,
time_async,
time_async_delayed
};
1 change: 1 addition & 0 deletions test/fixtures/es-modules/print_imported.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
process.stdout.write(JSON.stringify(imported));

0 comments on commit a7d6bae

Please sign in to comment.