aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2023-09-10 22:15:35 -0800
committerGravatar GitHub <noreply@github.com> 2023-09-10 23:15:35 -0700
commit51d3d4382281f789f8175079ed426a63529eb3e7 (patch)
tree14f6fe77a1e3b300488e9343d8e9d54f64bde376 /test
parentedea4f095a3bebf54f986c0fa038482316f4cde8 (diff)
downloadbun-51d3d4382281f789f8175079ed426a63529eb3e7.tar.gz
bun-51d3d4382281f789f8175079ed426a63529eb3e7.tar.zst
bun-51d3d4382281f789f8175079ed426a63529eb3e7.zip
Support named imports for json & toml files at runtime (#4783)
* Support named exports in json imports * Support named imports for `*.json` files * Remove stale comments * Don't export arrays as non-default * Add test for default exports * Don't break webpack --------- Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
Diffstat (limited to 'test')
-rw-r--r--test/transpiler/runtime-transpiler-fixture-duplicate-keys.json5
-rw-r--r--test/transpiler/runtime-transpiler-json-fixture.json8
-rw-r--r--test/transpiler/runtime-transpiler.test.ts130
-rw-r--r--test/transpiler/tsconfig.is-just-a-number.json1
-rw-r--r--test/transpiler/tsconfig.with-commas.json11
5 files changed, 155 insertions, 0 deletions
diff --git a/test/transpiler/runtime-transpiler-fixture-duplicate-keys.json b/test/transpiler/runtime-transpiler-fixture-duplicate-keys.json
new file mode 100644
index 000000000..3093e5c80
--- /dev/null
+++ b/test/transpiler/runtime-transpiler-fixture-duplicate-keys.json
@@ -0,0 +1,5 @@
+{
+ "a": 1,
+ "b": 2,
+ "a": "4"
+}
diff --git a/test/transpiler/runtime-transpiler-json-fixture.json b/test/transpiler/runtime-transpiler-json-fixture.json
new file mode 100644
index 000000000..81517c7fe
--- /dev/null
+++ b/test/transpiler/runtime-transpiler-json-fixture.json
@@ -0,0 +1,8 @@
+{
+ "name": "Spiral 4v4 NS",
+ "description": "4v4 unshared map. 4 spawns in a spiral. Preferred to play with 4v4 NS.",
+ "version": "1.0",
+ "creator": "Grand Homie",
+ "players": [8, 8],
+ "default": { "a": 1 }
+}
diff --git a/test/transpiler/runtime-transpiler.test.ts b/test/transpiler/runtime-transpiler.test.ts
index c1881ee67..23e972be4 100644
--- a/test/transpiler/runtime-transpiler.test.ts
+++ b/test/transpiler/runtime-transpiler.test.ts
@@ -30,3 +30,133 @@ describe("// @bun", () => {
expect(exitCode).toBe(0);
});
});
+
+describe("json imports", () => {
+ test("require(*.json)", async () => {
+ const {
+ name,
+ description,
+ players,
+ version,
+ creator,
+ default: defaultExport,
+ ...other
+ } = require("./runtime-transpiler-json-fixture.json");
+ const obj = {
+ "name": "Spiral 4v4 NS",
+ "description": "4v4 unshared map. 4 spawns in a spiral. Preferred to play with 4v4 NS.",
+ "version": "1.0",
+ "creator": "Grand Homie",
+ "players": [8, 8],
+ default: { a: 1 },
+ };
+ expect({
+ name,
+ description,
+ players,
+ version,
+ creator,
+ default: { a: 1 },
+ }).toEqual(obj);
+ expect(other).toEqual({});
+
+ // This tests that importing and requiring when already in the cache keeps the state the same
+ {
+ const {
+ name,
+ description,
+ players,
+ version,
+ creator,
+ default: defaultExport,
+ // @ts-ignore
+ } = await import("./runtime-transpiler-json-fixture.json");
+ const obj = {
+ "name": "Spiral 4v4 NS",
+ "description": "4v4 unshared map. 4 spawns in a spiral. Preferred to play with 4v4 NS.",
+ "version": "1.0",
+ "creator": "Grand Homie",
+ "players": [8, 8],
+ default: { a: 1 },
+ };
+ expect({
+ name,
+ description,
+ players,
+ version,
+ creator,
+ default: { a: 1 },
+ }).toEqual(obj);
+ // They should be strictly equal
+ expect(defaultExport.players).toBe(players);
+ expect(defaultExport).toEqual(obj);
+ }
+
+ delete require.cache[require.resolve("./runtime-transpiler-json-fixture.json")];
+ });
+
+ test("import(*.json)", async () => {
+ const {
+ name,
+ description,
+ players,
+ version,
+ creator,
+ default: defaultExport,
+ // @ts-ignore
+ } = await import("./runtime-transpiler-json-fixture.json");
+ delete require.cache[require.resolve("./runtime-transpiler-json-fixture.json")];
+ const obj = {
+ "name": "Spiral 4v4 NS",
+ "description": "4v4 unshared map. 4 spawns in a spiral. Preferred to play with 4v4 NS.",
+ "version": "1.0",
+ "creator": "Grand Homie",
+ "players": [8, 8],
+ default: { a: 1 },
+ };
+ expect({
+ name,
+ description,
+ players,
+ version,
+ creator,
+ default: { a: 1 },
+ }).toEqual(obj);
+ // They should be strictly equal
+ expect(defaultExport.players).toBe(players);
+ expect(defaultExport).toEqual(obj);
+ });
+
+ test("should support comments in tsconfig.json", async () => {
+ // @ts-ignore
+ const { buildOptions, default: defaultExport } = await import("./tsconfig.with-commas.json");
+ delete require.cache[require.resolve("./tsconfig.with-commas.json")];
+ const obj = {
+ "buildOptions": {
+ "outDir": "dist",
+ "baseUrl": ".",
+ "paths": {
+ "src/*": ["src/*"],
+ },
+ },
+ };
+ expect({
+ buildOptions,
+ }).toEqual(obj);
+ // They should be strictly equal
+ expect(defaultExport.buildOptions).toBe(buildOptions);
+ expect(defaultExport).toEqual(obj);
+ });
+
+ test("should handle non-boecjts in tsconfig.json", async () => {
+ // @ts-ignore
+ const { default: num } = await import("./tsconfig.is-just-a-number.json");
+ delete require.cache[require.resolve("./tsconfig.is-just-a-number.json")];
+ expect(num).toBe(1);
+ });
+
+ test("should handle duplicate keys", async () => {
+ // @ts-ignore
+ expect((await import("./runtime-transpiler-fixture-duplicate-keys.json")).a).toBe("4");
+ });
+});
diff --git a/test/transpiler/tsconfig.is-just-a-number.json b/test/transpiler/tsconfig.is-just-a-number.json
new file mode 100644
index 000000000..d00491fd7
--- /dev/null
+++ b/test/transpiler/tsconfig.is-just-a-number.json
@@ -0,0 +1 @@
+1
diff --git a/test/transpiler/tsconfig.with-commas.json b/test/transpiler/tsconfig.with-commas.json
new file mode 100644
index 000000000..d30671e77
--- /dev/null
+++ b/test/transpiler/tsconfig.with-commas.json
@@ -0,0 +1,11 @@
+{
+ "buildOptions": {
+ "outDir": "dist",
+ "baseUrl": ".",
+ "paths": {
+ "src/*": ["src/*"]
+ }
+ }
+ // such comment
+ // very much
+}