node/test/es-module/test-esm-extension-lookup-deprecation.mjs
Antoine du Hamel bae14b7914
test: do not set concurrency on parallelized runs
Our CI already run test files in parallel, having `node:test` spawns
child processes concurrently could lead to oversubscribing the CI
machine. This commit sets the `concurrency` depending
on the presence of `TEST_PARALLEL` in the env, so running the test
file individually still spawns child processes concurrently, and
running the whole test suite does not oversubscribe the machine.

PR-URL: https://github.com/nodejs/node/pull/52177
Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
2024-03-23 21:11:28 +00:00

122 lines
4.8 KiB
JavaScript

import { spawnPromisified } from '../common/index.mjs';
import tmpdir from '../common/tmpdir.js';
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';
describe('ESM in main field', { concurrency: !process.env.TEST_PARALLEL }, () => {
before(() => tmpdir.refresh());
it('should handle fully-specified relative path without any warning', async () => {
const cwd = tmpdir.resolve(Math.random().toString());
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 () => {
const cwd = tmpdir.resolve(Math.random().toString());
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 () => {
const cwd = tmpdir.resolve(Math.random().toString());
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 () => {
const cwd = tmpdir.resolve(Math.random().toString());
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 () => {
const cwd = tmpdir.resolve(Math.random().toString());
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 () => {
const cwd = tmpdir.resolve(Math.random().toString());
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);
});
});