-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathno-default-export-warning.test.ts
More file actions
134 lines (117 loc) · 4.34 KB
/
no-default-export-warning.test.ts
File metadata and controls
134 lines (117 loc) · 4.34 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { sentryEsbuildPlugin } from "../src";
import { describe, it, expect, beforeAll, afterAll } from "vitest";
import * as esbuild from "esbuild";
import * as path from "path";
import * as fs from "fs";
import * as os from "os";
describe("esbuild proxy module default export handling", () => {
let tmpDir: string;
beforeAll(() => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "sentry-esbuild-test-"));
});
afterAll(() => {
fs.rmSync(tmpDir, { recursive: true, force: true });
});
it("should build successfully without warnings when entry point has no default export", async () => {
const inputFile = path.join(tmpDir, "no-default.ts");
fs.writeFileSync(
inputFile,
[
'import * as path from "path";',
'console.log("No default export here", path.sep);',
"",
].join("\n")
);
const outDir = path.join(tmpDir, "out-no-default");
const result = await esbuild.build({
entryPoints: [inputFile],
sourcemap: true,
bundle: true,
outdir: outDir,
platform: "node",
plugins: [
sentryEsbuildPlugin({
telemetry: false,
release: { name: "test-release", create: false },
}),
],
write: true,
});
// If esbuild version is new enough, warnings about "Import 'default' will always be
// undefined" will be captured in result.warnings. On older esbuild versions this array
// may be empty regardless, so the test at least verifies no build errors occur.
const importUndefinedWarnings = (result.warnings || []).filter(
(w) => w.text.includes("Import") && w.text.includes("default") && w.text.includes("undefined")
);
expect(importUndefinedWarnings).toHaveLength(0);
});
it("should preserve default export when the entry point has one", async () => {
const inputFile = path.join(tmpDir, "with-default.ts");
fs.writeFileSync(
inputFile,
["export const foo = 42;", 'export default function main() { return "hello"; }', ""].join(
"\n"
)
);
const outDir = path.join(tmpDir, "out-with-default");
const result = await esbuild.build({
entryPoints: [inputFile],
sourcemap: true,
bundle: true,
outdir: outDir,
platform: "node",
format: "esm",
plugins: [
sentryEsbuildPlugin({
telemetry: false,
release: { name: "test-release", create: false },
}),
],
write: true,
});
// Should produce no warnings
const importUndefinedWarnings = (result.warnings || []).filter(
(w) => w.text.includes("Import") && w.text.includes("default") && w.text.includes("undefined")
);
expect(importUndefinedWarnings).toHaveLength(0);
// Verify the output contains the default export function
const outputFiles = fs.readdirSync(outDir).filter((f) => f.endsWith(".js"));
expect(outputFiles.length).toBeGreaterThan(0);
const outputContent = fs.readFileSync(path.join(outDir, outputFiles[0] as string), "utf-8");
expect(outputContent).toContain("main");
});
it("should preserve named exports when the entry point has no default export", async () => {
const inputFile = path.join(tmpDir, "named-only.ts");
fs.writeFileSync(
inputFile,
["export const foo = 42;", 'export const bar = "hello";', ""].join("\n")
);
const outDir = path.join(tmpDir, "out-named-only");
const result = await esbuild.build({
entryPoints: [inputFile],
sourcemap: true,
bundle: true,
outdir: outDir,
platform: "node",
format: "esm",
plugins: [
sentryEsbuildPlugin({
telemetry: false,
release: { name: "test-release", create: false },
}),
],
write: true,
});
// Should produce no warnings
const importUndefinedWarnings = (result.warnings || []).filter(
(w) => w.text.includes("Import") && w.text.includes("default") && w.text.includes("undefined")
);
expect(importUndefinedWarnings).toHaveLength(0);
// Verify the output contains the named exports
const outputFiles = fs.readdirSync(outDir).filter((f) => f.endsWith(".js"));
expect(outputFiles.length).toBeGreaterThan(0);
const outputContent = fs.readFileSync(path.join(outDir, outputFiles[0] as string), "utf-8");
expect(outputContent).toContain("foo");
expect(outputContent).toContain("bar");
});
});