Skip to content

test(nuxt): Add unit tests for catch-all routes #16891

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 10, 2025
Merged

Conversation

s1gr1d
Copy link
Member

@s1gr1d s1gr1d commented Jul 10, 2025

Test follow-up for this PR: #16843

Re-organizes the unit tests a bit to be less repetitive with default data that is aligned to real-world examples.


Adds unit tests for cases mentioned here:

  • differentiate dynamic vs static route on the same path (users/:id vs users/settings)
  • cases for catch-all routes

@@ -0,0 +1,272 @@
import { describe, expect, it } from 'vitest';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sadly the diff for this file is not visible as I did not commit the naming change in the previous PR :/

The file was already renamed with .test.ts in the other PR and the tests already went through locally. I just now found out that it was still committed with the old name (without .test.ts) which means it did not run through CI :(

Comment on lines +5 to +32
/** Creates a mock NuxtPage object with all existing pages. Nuxt provides this during the build time in the "pages:extend" hook.
* The content is inspired by a real-world example. */
const createMockPagesData = (
overrides: NuxtPageSubset[] = [],
addDefaultPageData: boolean = true,
): NuxtPageSubset[] => {
const defaultBase = [
// Basic routes
{ path: '/', file: '/private/folders/application/pages/index.vue' },
{ path: '/simple-page', file: '/private/folders/application/pages/simple-page.vue' },
{ path: '/a/nested/simple-page', file: '/private/folders/application/pages/a/nested/simple-page.vue' },
// Dynamic routes (directory and file)
{ path: '/user/:userId()', file: '/private/folders/application/pages/user/[userId].vue' },
{ path: '/group-:name()/:id()', file: '/private/folders/application/pages/group-[name]/[id].vue' },
// Catch-all routes
{ path: '/catch-all/:path(.*)*', file: '/private/folders/application/pages/catch-all/[...path].vue' },
];

return [...(addDefaultPageData ? defaultBase : []), ...overrides];
};

// The base of modules when loading a specific page during runtime (inspired by real-world examples).
const defaultSSRContextModules = new Set([
'node_modules/nuxt/dist/app/components/nuxt-root.vue',
'app.vue',
'components/Button.vue',
// ...the specific requested page is added in the test (e.g. 'pages/user/[userId].vue')
]);
Copy link
Member Author

@s1gr1d s1gr1d Jul 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will highlight the new stuff for the tests in this PR to make it easier to look through.

Those are the new functions to create some default data.

Comment on lines +84 to +110
description: 'dynamic route with brackets in file name',
modules: new Set([...defaultSSRContextModules, 'pages/user/[userId].vue']),
requestedUrl: '/user/123',
buildTimePagesData: createMockPagesData(),
expected: { parametrizedRoute: '/user/:userId()' },
},
{
description: 'dynamic route with brackets in directory and file name',
modules: new Set([...defaultSSRContextModules, 'pages/group-[name]/[id].vue']),
requestedUrl: '/group-sentry/123',
buildTimePagesData: createMockPagesData(),
expected: { parametrizedRoute: '/group-:name()/:id()' },
},
{
description: 'catch all route (simple)',
modules: new Set([...defaultSSRContextModules, 'pages/catch-all/[...path].vue']),
requestedUrl: '/catch-all/whatever',
buildTimePagesData: createMockPagesData(),
expected: { parametrizedRoute: '/catch-all/:path(.*)*' },
},
{
description: 'catch all route (nested)',
modules: new Set([...defaultSSRContextModules, 'pages/catch-all/[...path].vue']),
requestedUrl: '/catch-all/whatever/you/want',
buildTimePagesData: createMockPagesData(),
expected: { parametrizedRoute: '/catch-all/:path(.*)*' },
},
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those are all the basic tests for dynamic and catch-all routes.

Comment on lines +149 to +174
it('should return correct route app has a dynamic route and a static route that share the same path', () => {
const modules = new Set([...defaultSSRContextModules, 'pages/user/settings.vue']);

const buildTimePagesData = createMockPagesData(
[
{ path: '/user/settings', file: '/private/folders/application/pages/user/settings.vue' },
{ path: '/user/:userId()', file: '/private/folders/application/pages/user/[userId].vue' },
],
false,
);

const result = extractParametrizedRouteFromContext(modules, '/user/settings', buildTimePagesData);
expect(result).toEqual({ parametrizedRoute: '/user/settings' });
});

it('should return correct route app has a dynamic route and a static route that share the same path (reverse)', () => {
const modules = new Set([...defaultSSRContextModules, 'pages/user/settings.vue']);

const buildTimePagesData = createMockPagesData([
{ path: '/user/:userId()', file: '/private/folders/application/pages/user/[userId].vue' },
{ path: '/user/settings', file: '/private/folders/application/pages/user/settings.vue' },
]);

const result = extractParametrizedRouteFromContext(modules, '/user/settings', buildTimePagesData);
expect(result).toEqual({ parametrizedRoute: '/user/settings' });
});
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those are the tests for users/:id vs users/settings.

@s1gr1d s1gr1d requested a review from Lms24 July 10, 2025 12:07
Copy link
Member

@Lms24 Lms24 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, thanks a lot for adding these tests!

@s1gr1d s1gr1d merged commit affebb4 into develop Jul 10, 2025
40 checks passed
@s1gr1d s1gr1d deleted the sig/nuxt-param-tests branch July 10, 2025 12:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants