Skip to content

Commit 33e1a26

Browse files
committed
test: separate alias tests from imports tests
1 parent 7385506 commit 33e1a26

File tree

3 files changed

+151
-142
lines changed

3 files changed

+151
-142
lines changed
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
import { testSuite, expect } from 'manten';
2+
import { createFixture } from 'fs-fixture';
3+
import { execa } from 'execa';
4+
import { outdent } from 'outdent';
5+
import { pkgroll } from '../../utils.js';
6+
import { createPackageJson } from '../../fixtures.js';
7+
8+
export default testSuite(({ describe }, nodePath: string) => {
9+
describe('imports - non-# import handling', async ({ test }) => {
10+
test('non-# imports are skipped', async () => {
11+
const packagePath = 'node_modules/test-pkg';
12+
const consumedPackage = {
13+
'package.json': createPackageJson({
14+
name: 'test-pkg',
15+
type: 'module',
16+
imports: {
17+
// @ts-expect-error - Testing non-# import (not spec-compliant but we handle it)
18+
'my-alias': './src/internal.js',
19+
},
20+
exports: './dist/index.js',
21+
}),
22+
src: {
23+
'index.ts': 'export const value = 456;',
24+
'internal.js': 'export const internal = true;',
25+
},
26+
};
27+
await using fixture = await createFixture({
28+
[packagePath]: consumedPackage,
29+
'load-pkg.mjs': outdent`
30+
import { value } from "test-pkg";
31+
console.log(value);
32+
`,
33+
});
34+
35+
const result = await pkgroll([], {
36+
cwd: fixture.getPath(packagePath),
37+
nodePath,
38+
});
39+
40+
expect(result.exitCode).toBe(0);
41+
expect(result.stderr).toBe('');
42+
43+
const indexExists = await fixture.exists(`${packagePath}/dist/index.js`);
44+
expect(indexExists).toBe(true);
45+
46+
const internalExists = await fixture.exists(`${packagePath}/dist/internal.js`);
47+
expect(internalExists).toBe(false);
48+
49+
const { stdout } = await execa('node', ['load-pkg.mjs'], { cwd: fixture.path });
50+
expect(stdout).toBe('456');
51+
});
52+
53+
test('mixed # and non-# imports - only # are built', async () => {
54+
const packagePath = 'node_modules/test-pkg';
55+
const consumedPackage = {
56+
'package.json': createPackageJson({
57+
name: 'test-pkg',
58+
type: 'module',
59+
imports: {
60+
'#valid': './dist/valid.js',
61+
// @ts-expect-error - Testing non-# import (not spec-compliant)
62+
'invalid-alias': './src/invalid.js',
63+
},
64+
exports: './dist/index.js',
65+
}),
66+
src: {
67+
'valid.ts': 'export const valid = "works";',
68+
'invalid.js': 'export const invalid = "skipped";',
69+
'index.ts': 'export { valid } from "#valid";',
70+
},
71+
};
72+
await using fixture = await createFixture({
73+
[packagePath]: consumedPackage,
74+
'load-pkg.mjs': outdent`
75+
import { valid } from "test-pkg";
76+
console.log(valid);
77+
`,
78+
});
79+
80+
const result = await pkgroll([], {
81+
cwd: fixture.getPath(packagePath),
82+
nodePath,
83+
});
84+
85+
expect(result.exitCode).toBe(0);
86+
expect(result.stderr).toBe('');
87+
88+
const validExists = await fixture.exists(`${packagePath}/dist/valid.js`);
89+
expect(validExists).toBe(true);
90+
91+
const invalidExists = await fixture.exists(`${packagePath}/dist/invalid.js`);
92+
expect(invalidExists).toBe(false);
93+
94+
const distContent = await fixture.readFile(`${packagePath}/dist/index.js`, 'utf8');
95+
expect(distContent).toMatch('#valid');
96+
97+
const { stdout } = await execa('node', ['load-pkg.mjs'], { cwd: fixture.path });
98+
expect(stdout).toBe('works');
99+
});
100+
101+
test('condition keys (node/default) processed despite not starting with #', async () => {
102+
const packagePath = 'node_modules/test-pkg';
103+
const consumedPackage = {
104+
'package.json': createPackageJson({
105+
name: 'test-pkg',
106+
type: 'module',
107+
imports: {
108+
'#platform': {
109+
node: './dist/node.js',
110+
default: './dist/browser.js',
111+
},
112+
},
113+
exports: './dist/index.js',
114+
}),
115+
src: {
116+
'node.ts': 'export const env = "node";',
117+
'browser.ts': 'export const env = "browser";',
118+
'index.ts': 'export { env } from "#platform";',
119+
},
120+
};
121+
await using fixture = await createFixture({
122+
[packagePath]: consumedPackage,
123+
'load-pkg.mjs': outdent`
124+
import { env } from "test-pkg";
125+
console.log(env);
126+
`,
127+
});
128+
129+
const result = await pkgroll([], {
130+
cwd: fixture.getPath(packagePath),
131+
nodePath,
132+
});
133+
134+
expect(result.exitCode).toBe(0);
135+
expect(result.stderr).toBe('');
136+
137+
const nodeExists = await fixture.exists(`${packagePath}/dist/node.js`);
138+
expect(nodeExists).toBe(true);
139+
140+
const browserExists = await fixture.exists(`${packagePath}/dist/browser.js`);
141+
expect(browserExists).toBe(true);
142+
143+
const distContent = await fixture.readFile(`${packagePath}/dist/index.js`, 'utf8');
144+
expect(distContent).toMatch('#platform');
145+
146+
const { stdout } = await execa('node', ['load-pkg.mjs'], { cwd: fixture.path });
147+
expect(stdout).toBe('node');
148+
});
149+
});
150+
});

tests/specs/builds/imports.ts

Lines changed: 0 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -520,148 +520,6 @@ export default testSuite(({ describe }, nodePath: string) => {
520520
});
521521
});
522522

523-
describe('non-# import handling', ({ test }) => {
524-
test('non-# imports are skipped', async () => {
525-
const packagePath = 'node_modules/test-pkg';
526-
const consumedPackage = {
527-
'package.json': createPackageJson({
528-
name: 'test-pkg',
529-
type: 'module',
530-
imports: {
531-
// @ts-expect-error - Testing non-# import (not spec-compliant but we handle it)
532-
'my-alias': './src/internal.js',
533-
},
534-
exports: './dist/index.js',
535-
}),
536-
src: {
537-
'index.ts': 'export const value = 456;',
538-
'internal.js': 'export const internal = true;',
539-
},
540-
};
541-
await using fixture = await createFixture({
542-
[packagePath]: consumedPackage,
543-
'load-pkg.mjs': outdent`
544-
import { value } from "test-pkg";
545-
console.log(value);
546-
`,
547-
});
548-
549-
const result = await pkgroll([], {
550-
cwd: fixture.getPath(packagePath),
551-
nodePath,
552-
});
553-
554-
expect(result.exitCode).toBe(0);
555-
expect(result.stderr).toBe('');
556-
557-
const indexExists = await fixture.exists(`${packagePath}/dist/index.js`);
558-
expect(indexExists).toBe(true);
559-
560-
const internalExists = await fixture.exists(`${packagePath}/dist/internal.js`);
561-
expect(internalExists).toBe(false);
562-
563-
const { stdout } = await execa('node', ['load-pkg.mjs'], { cwd: fixture.path });
564-
expect(stdout).toBe('456');
565-
});
566-
567-
test('mixed # and non-# imports - only # are built', async () => {
568-
const packagePath = 'node_modules/test-pkg';
569-
const consumedPackage = {
570-
'package.json': createPackageJson({
571-
name: 'test-pkg',
572-
type: 'module',
573-
imports: {
574-
'#valid': './dist/valid.js',
575-
// @ts-expect-error - Testing non-# import (not spec-compliant)
576-
'invalid-alias': './src/invalid.js',
577-
},
578-
exports: './dist/index.js',
579-
}),
580-
src: {
581-
'valid.ts': 'export const valid = "works";',
582-
'invalid.js': 'export const invalid = "skipped";',
583-
'index.ts': 'export { valid } from "#valid";',
584-
},
585-
};
586-
await using fixture = await createFixture({
587-
[packagePath]: consumedPackage,
588-
'load-pkg.mjs': outdent`
589-
import { valid } from "test-pkg";
590-
console.log(valid);
591-
`,
592-
});
593-
594-
const result = await pkgroll([], {
595-
cwd: fixture.getPath(packagePath),
596-
nodePath,
597-
});
598-
599-
expect(result.exitCode).toBe(0);
600-
expect(result.stderr).toBe('');
601-
602-
const validExists = await fixture.exists(`${packagePath}/dist/valid.js`);
603-
expect(validExists).toBe(true);
604-
605-
const invalidExists = await fixture.exists(`${packagePath}/dist/invalid.js`);
606-
expect(invalidExists).toBe(false);
607-
608-
const distContent = await fixture.readFile(`${packagePath}/dist/index.js`, 'utf8');
609-
expect(distContent).toMatch('#valid');
610-
611-
const { stdout } = await execa('node', ['load-pkg.mjs'], { cwd: fixture.path });
612-
expect(stdout).toBe('works');
613-
});
614-
615-
test('condition keys (node/default) processed despite not starting with #', async () => {
616-
const packagePath = 'node_modules/test-pkg';
617-
const consumedPackage = {
618-
'package.json': createPackageJson({
619-
name: 'test-pkg',
620-
type: 'module',
621-
imports: {
622-
'#platform': {
623-
node: './dist/node.js',
624-
default: './dist/browser.js',
625-
},
626-
},
627-
exports: './dist/index.js',
628-
}),
629-
src: {
630-
'node.ts': 'export const env = "node";',
631-
'browser.ts': 'export const env = "browser";',
632-
'index.ts': 'export { env } from "#platform";',
633-
},
634-
};
635-
await using fixture = await createFixture({
636-
[packagePath]: consumedPackage,
637-
'load-pkg.mjs': outdent`
638-
import { env } from "test-pkg";
639-
console.log(env);
640-
`,
641-
});
642-
643-
const result = await pkgroll([], {
644-
cwd: fixture.getPath(packagePath),
645-
nodePath,
646-
});
647-
648-
expect(result.exitCode).toBe(0);
649-
expect(result.stderr).toBe('');
650-
651-
const nodeExists = await fixture.exists(`${packagePath}/dist/node.js`);
652-
expect(nodeExists).toBe(true);
653-
654-
const browserExists = await fixture.exists(`${packagePath}/dist/browser.js`);
655-
expect(browserExists).toBe(true);
656-
657-
const distContent = await fixture.readFile(`${packagePath}/dist/index.js`, 'utf8');
658-
expect(distContent).toMatch('#platform');
659-
660-
const { stdout } = await execa('node', ['load-pkg.mjs'], { cwd: fixture.path });
661-
expect(stdout).toBe('node');
662-
});
663-
});
664-
665523
describe('edge cases', ({ test }) => {
666524
test('monorepo: hoisted dependencies - # imports from dependencies are bundled', async () => {
667525
const packagePath = 'packages/my-package';

tests/specs/builds/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export default testSuite(({ describe }, nodePath: string) => {
1515
runTestSuite(import('./wildcard-exports.js'), nodePath);
1616
runTestSuite(import('./package-imports.js'), nodePath);
1717
runTestSuite(import('./imports.js'), nodePath);
18+
runTestSuite(import('./imports-alias.js'), nodePath);
1819
runTestSuite(import('./bin.js'), nodePath);
1920
runTestSuite(import('./dependencies.js'), nodePath);
2021
runTestSuite(import('./real-dependencies.js'), nodePath);

0 commit comments

Comments
 (0)