aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/bun.js/filesystem_router.test.ts85
1 files changed, 85 insertions, 0 deletions
diff --git a/test/bun.js/filesystem_router.test.ts b/test/bun.js/filesystem_router.test.ts
new file mode 100644
index 000000000..c375cbb22
--- /dev/null
+++ b/test/bun.js/filesystem_router.test.ts
@@ -0,0 +1,85 @@
+import { FileSystemRouter } from "bun";
+import { it, expect } from "bun:test";
+import path, { dirname, resolve } from "path";
+import fs, { realpathSync, rmSync } from "fs";
+import { tmpdir } from "os";
+
+function createTree(basedir, paths) {
+ for (const end of paths) {
+ const abs = path.join(basedir, end);
+ try {
+ const dir = dirname(abs);
+ if (dir.length > 0 && dir !== "/") fs.mkdirSync(dir, { recursive: true });
+ } catch (e) {}
+ fs.writeFileSync(abs, "export default " + JSON.stringify(end) + ";\n");
+ }
+}
+
+it("should find files", () => {
+ const tempdir = realpathSync(tmpdir()) + "/";
+
+ rmSync(tempdir + "fs-router-test-01", { recursive: true, force: true });
+
+ createTree(tempdir + "fs-router-test-01", [
+ "a.tsx",
+ "b.tsx",
+ "abc/[id].tsx",
+ "abc/index.tsx",
+ "abc/def/[id].tsx",
+ "abc/def/index.tsx",
+ "abc/def/ghi/[id].tsx",
+ "abc/def/ghi/index.tsx",
+ "abc/def/ghi/jkl/[id].tsx",
+ "abc/def/ghi/jkl/index.tsx",
+ "[id].tsx",
+ "index.tsx",
+ "foo/[id].tsx",
+ "catch-all/[...id].tsx",
+ ]);
+
+ const router = new FileSystemRouter({
+ dir: tempdir + "fs-router-test-01/",
+ fileExtensions: [".tsx"],
+ style: "nextjs",
+ });
+
+ const routes = router.routes;
+ const fixture = {
+ "/": `${tempdir}fs-router-test-01/index.tsx`,
+ "/[id]": `${tempdir}fs-router-test-01/[id].tsx`,
+ "/a": `${tempdir}fs-router-test-01/a.tsx`,
+ "/abc": `${tempdir}fs-router-test-01/abc/index.tsx`,
+ "/abc/[id]": `${tempdir}fs-router-test-01/abc/[id].tsx`,
+ "/abc/def/[id]": `${tempdir}fs-router-test-01/abc/def/[id].tsx`,
+ "/abc/def/ghi": `${tempdir}fs-router-test-01/abc/def/ghi/index.tsx`,
+ "/abc/def/ghi/[id]": `${tempdir}fs-router-test-01/abc/def/ghi/[id].tsx`,
+ "/abc/def/ghi/jkl": `${tempdir}fs-router-test-01/abc/def/ghi/jkl/index.tsx`,
+ "/abc/def/ghi/jkl/[id]": `${tempdir}fs-router-test-01/abc/def/ghi/jkl/[id].tsx`,
+ "/abc/def": `${tempdir}fs-router-test-01/abc/def/index.tsx`,
+ "/b": `${tempdir}fs-router-test-01/b.tsx`,
+ "/foo/[id]": `${tempdir}fs-router-test-01/foo/[id].tsx`,
+ "/catch-all/[...id]": `${tempdir}fs-router-test-01/catch-all/[...id].tsx`,
+ };
+ for (const route in fixture) {
+ if (!(route in routes)) {
+ throw new Error(`Route ${route} not found`);
+ }
+
+ expect(routes[route]).toBe(fixture[route]);
+ }
+
+ expect(Object.keys(routes).length).toBe(Object.keys(fixture).length);
+
+ expect(router.match("/never/gonna/give/you/up")).toBe(null);
+ expect(
+ router.match(
+ "/catch-all/we-are-no-strangers-to-love/you/know/the/rules/and/so/do/i",
+ ).params.id,
+ ).toBe("we-are-no-strangers-to-love/you/know/the/rules/and/so/do/i");
+ expect(router.match("/").name).toBe("/");
+ expect(router.match("/index").name).toBe("/");
+ expect(router.match("/index/").name).toBe("/");
+ expect(router.match("/a").name).toBe("/a");
+ expect(router.match("/b").name).toBe("/b");
+ expect(router.match("/abc/123").params.id).toBe("123");
+});
' /> Jarred Sumner 1-2/+2 2022-01-05Fix crash that sometimes happens after 30 secondsGravatar Jarred Sumner 5-106/+185 2022-01-05[bun bun][bun dev] Fix crash affecting large projectsGravatar Jarred Sumner 1-26/+119 2022-01-05move some code aroundGravatar Jarred Sumner 2-281/+284 2022-01-05we want the opposite of thisGravatar Jarred Sumner 1-1/+0 2022-01-05[JS Parser] Reduce memory usage by ~8%Gravatar Jarred Sumner 6-7/+42 2022-01-05minimal integration tests for macrosGravatar Jarred Sumner 4-0/+47 2022-01-05Update resolver.zigGravatar Jarred Sumner 1-3/+0 2022-01-05Update options.zigGravatar Jarred Sumner 1-2/+25 2022-01-05Update http.zigGravatar Jarred Sumner 1-1/+1 2022-01-05Add module condition to the node platform (#104)Gravatar Mateusz Burzyński 1-1/+4 2022-01-05Drop redundant comments (#103)Gravatar Mateusz Burzyński 1-23/+0 2022-01-05Tweak default main fields for the bun platform to match other popular bundler...Gravatar Mateusz Burzyński 1-10/+7 2022-01-04:skull: dead codeGravatar Jarred Sumner 1-13/+0 2022-01-04[bun dev] Print error in status line textGravatar Jarred Sumner 1-3/+13 2022-01-04noramlize some errorsGravatar Jarred Sumner 3-3/+4 2022-01-04[Bun.js][bun dev] Support macros inside of Bun.jsGravatar Jarred Sumner 5-51/+103 2022-01-04[bun bun] Fix error when regenerating node_modules.bun after moving itGravatar Jarred Sumner 1-1/+17 2022-01-04Improve how we detect if terminal colors are supportedGravatar Jarred Sumner 3-11/+66 2022-01-04Improve error handling when out of file handlesGravatar Jarred Sumner 5-58/+248 2022-01-04Update build-idGravatar Jarred Sumner 1-1/+1 2022-01-04Downgrade mimalloc due to crashesGravatar Jarred Sumner 1-0/+0 2022-01-04Upload compressed `.dSYM` for every releaseGravatar Jarred Sumner 2-4/+34 2022-01-04Update .gitignoreGravatar Jarred Sumner 1-0/+3 2022-01-04[bun install] Fix more cases where bytes are printed instead of stringsGravatar Jarred Sumner 1-10/+38 2022-01-04minor perf optimization: remove this loop on macOSGravatar Jarred Sumner 2-4/+8 2022-01-03Update crash_reporter_linux.zigbun-v0.0.66Gravatar Jarred Sumner 1-1/+1 2022-01-03:confused:Gravatar Jarred Sumner 5-0/+1 2022-01-03:nail_care:Gravatar Jarred Sumner 3-1652/+1826 2022-01-03Update crash_reporter_linux.zigGravatar Jarred Sumner 1-1/+1 2022-01-03Update PLCrashReport.mGravatar Jarred Sumner 1-1/+1 2022-01-03Update PLCrashReport.mGravatar Jarred Sumner 1-2/+1 2022-01-03:lock:Gravatar Jarred Sumner 4-1/+1 2022-01-03dead codeGravatar Jarred Sumner 13-1881/+1660