2023-07-11 16:49:06 +00:00
|
|
|
import { spawnPromisified } from '../common/index.mjs';
|
2023-08-10 23:07:30 +00:00
|
|
|
import tmpdir from '../common/tmpdir.js';
|
2023-07-11 16:49:06 +00:00
|
|
|
|
|
|
|
import assert from 'node:assert';
|
|
|
|
import { mkdir, writeFile } from 'node:fs/promises';
|
|
|
|
import * as path from 'node:path';
|
|
|
|
import { execPath } from 'node:process';
|
|
|
|
import { describe, it, before } from 'node:test';
|
|
|
|
|
2024-03-23 21:11:28 +00:00
|
|
|
describe('ESM in main field', { concurrency: !process.env.TEST_PARALLEL }, () => {
|
2023-07-11 16:49:06 +00:00
|
|
|
before(() => tmpdir.refresh());
|
|
|
|
|
|
|
|
it('should handle fully-specified relative path without any warning', async () => {
|
2023-08-21 16:41:53 +00:00
|
|
|
const cwd = tmpdir.resolve(Math.random().toString());
|
2023-07-11 16:49:06 +00:00
|
|
|
const pkgPath = path.join(cwd, './node_modules/pkg/');
|
|
|
|
await mkdir(pkgPath, { recursive: true });
|
|
|
|
await writeFile(path.join(pkgPath, './index.js'), 'console.log("Hello World!")');
|
|
|
|
await writeFile(path.join(pkgPath, './package.json'), JSON.stringify({
|
|
|
|
main: './index.js',
|
|
|
|
type: 'module',
|
|
|
|
}));
|
|
|
|
const { code, stdout, stderr } = await spawnPromisified(execPath, [
|
|
|
|
'--input-type=module',
|
|
|
|
'--eval', 'import "pkg"',
|
|
|
|
], { cwd });
|
|
|
|
|
|
|
|
assert.strictEqual(stderr, '');
|
|
|
|
assert.match(stdout, /^Hello World!\r?\n$/);
|
|
|
|
assert.strictEqual(code, 0);
|
|
|
|
});
|
|
|
|
it('should handle fully-specified absolute path without any warning', async () => {
|
2023-08-21 16:41:53 +00:00
|
|
|
const cwd = tmpdir.resolve(Math.random().toString());
|
2023-07-11 16:49:06 +00:00
|
|
|
const pkgPath = path.join(cwd, './node_modules/pkg/');
|
|
|
|
await mkdir(pkgPath, { recursive: true });
|
|
|
|
await writeFile(path.join(pkgPath, './index.js'), 'console.log("Hello World!")');
|
|
|
|
await writeFile(path.join(pkgPath, './package.json'), JSON.stringify({
|
|
|
|
main: path.join(pkgPath, './index.js'),
|
|
|
|
type: 'module',
|
|
|
|
}));
|
|
|
|
const { code, stdout, stderr } = await spawnPromisified(execPath, [
|
|
|
|
'--input-type=module',
|
|
|
|
'--eval', 'import "pkg"',
|
|
|
|
], { cwd });
|
|
|
|
|
|
|
|
assert.strictEqual(stderr, '');
|
|
|
|
assert.match(stdout, /^Hello World!\r?\n$/);
|
|
|
|
assert.strictEqual(code, 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should emit warning when "main" and "exports" are missing', async () => {
|
2023-08-21 16:41:53 +00:00
|
|
|
const cwd = tmpdir.resolve(Math.random().toString());
|
2023-07-11 16:49:06 +00:00
|
|
|
const pkgPath = path.join(cwd, './node_modules/pkg/');
|
|
|
|
await mkdir(pkgPath, { recursive: true });
|
|
|
|
await writeFile(path.join(pkgPath, './index.js'), 'console.log("Hello World!")');
|
|
|
|
await writeFile(path.join(pkgPath, './package.json'), JSON.stringify({
|
|
|
|
type: 'module',
|
|
|
|
}));
|
|
|
|
const { code, stdout, stderr } = await spawnPromisified(execPath, [
|
|
|
|
'--input-type=module',
|
|
|
|
'--eval', 'import "pkg"',
|
|
|
|
], { cwd });
|
|
|
|
|
|
|
|
assert.match(stderr, /\[DEP0151\]/);
|
|
|
|
assert.match(stdout, /^Hello World!\r?\n$/);
|
|
|
|
assert.strictEqual(code, 0);
|
|
|
|
});
|
|
|
|
it('should emit warning when "main" is falsy', async () => {
|
2023-08-21 16:41:53 +00:00
|
|
|
const cwd = tmpdir.resolve(Math.random().toString());
|
2023-07-11 16:49:06 +00:00
|
|
|
const pkgPath = path.join(cwd, './node_modules/pkg/');
|
|
|
|
await mkdir(pkgPath, { recursive: true });
|
|
|
|
await writeFile(path.join(pkgPath, './index.js'), 'console.log("Hello World!")');
|
|
|
|
await writeFile(path.join(pkgPath, './package.json'), JSON.stringify({
|
|
|
|
type: 'module',
|
|
|
|
main: '',
|
|
|
|
}));
|
|
|
|
const { code, stdout, stderr } = await spawnPromisified(execPath, [
|
|
|
|
'--input-type=module',
|
|
|
|
'--eval', 'import "pkg"',
|
|
|
|
], { cwd });
|
|
|
|
|
|
|
|
assert.match(stderr, /\[DEP0151\]/);
|
|
|
|
assert.match(stdout, /^Hello World!\r?\n$/);
|
|
|
|
assert.strictEqual(code, 0);
|
|
|
|
});
|
|
|
|
it('should emit warning when "main" is a relative path without extension', async () => {
|
2023-08-21 16:41:53 +00:00
|
|
|
const cwd = tmpdir.resolve(Math.random().toString());
|
2023-07-11 16:49:06 +00:00
|
|
|
const pkgPath = path.join(cwd, './node_modules/pkg/');
|
|
|
|
await mkdir(pkgPath, { recursive: true });
|
|
|
|
await writeFile(path.join(pkgPath, './index.js'), 'console.log("Hello World!")');
|
|
|
|
await writeFile(path.join(pkgPath, './package.json'), JSON.stringify({
|
|
|
|
main: 'index',
|
|
|
|
type: 'module',
|
|
|
|
}));
|
|
|
|
const { code, stdout, stderr } = await spawnPromisified(execPath, [
|
|
|
|
'--input-type=module',
|
|
|
|
'--eval', 'import "pkg"',
|
|
|
|
], { cwd });
|
|
|
|
|
|
|
|
assert.match(stderr, /\[DEP0151\]/);
|
|
|
|
assert.match(stdout, /^Hello World!\r?\n$/);
|
|
|
|
assert.strictEqual(code, 0);
|
|
|
|
});
|
|
|
|
it('should emit warning when "main" is an absolute path without extension', async () => {
|
2023-08-21 16:41:53 +00:00
|
|
|
const cwd = tmpdir.resolve(Math.random().toString());
|
2023-07-11 16:49:06 +00:00
|
|
|
const pkgPath = path.join(cwd, './node_modules/pkg/');
|
|
|
|
await mkdir(pkgPath, { recursive: true });
|
|
|
|
await writeFile(path.join(pkgPath, './index.js'), 'console.log("Hello World!")');
|
|
|
|
await writeFile(path.join(pkgPath, './package.json'), JSON.stringify({
|
|
|
|
main: pkgPath + 'index',
|
|
|
|
type: 'module',
|
|
|
|
}));
|
|
|
|
const { code, stdout, stderr } = await spawnPromisified(execPath, [
|
|
|
|
'--input-type=module',
|
|
|
|
'--eval', 'import "pkg"',
|
|
|
|
], { cwd });
|
|
|
|
|
|
|
|
assert.match(stderr, /\[DEP0151\]/);
|
|
|
|
assert.match(stdout, /^Hello World!\r?\n$/);
|
|
|
|
assert.strictEqual(code, 0);
|
|
|
|
});
|
|
|
|
});
|