Skip to content

Commit

Permalink
Non unique custom routes ID error and test
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasferreira committed Oct 31, 2022
1 parent 9228db0 commit 3738662
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
12 changes: 12 additions & 0 deletions packages/remix-dev/__tests__/defineRoutes-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,16 @@ describe("defineRoutes", () => {

expect(Object.entries(routes)).toHaveLength(3);
});

it("throws an error when one route already exists with the same custom ID", () => {
function defineNonUniqueRoutes() {
defineRoutes((route) => {
route("/user/:id", "routes/index.tsx", { id: "user" });
route("/user", "routes/index.tsx", { id: "user" });
route("/other", "routes/other-route.tsx");
});
}

expect(defineNonUniqueRoutes).toThrow("must be unique");
});
});
7 changes: 7 additions & 0 deletions packages/remix-dev/config/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,13 @@ export function defineRoutes(
file,
};

if (!!options.id && options.id in routes) {
throw new Error(
`You tried to define a route with custom id "${options.id}" but one ` +
"already exists. Custom route ids must be unique"
);
}

routes[route.id] = route;

if (children) {
Expand Down

0 comments on commit 3738662

Please sign in to comment.