nandi95
nandi95
NNuxt
Created by JuanMCe on 4/10/2025 in #❓・help
middleware on nested routes
Nuxt unit testing sets up nuxt and runs plugins middlewares etc
15 replies
NNuxt
Created by JuanMCe on 4/10/2025 in #❓・help
middleware on nested routes
15 replies
NNuxt
Created by JuanMCe on 4/10/2025 in #❓・help
middleware on nested routes
yeah I'm sure, I'm open to be proven wrong tho
15 replies
NNuxt
Created by JuanMCe on 4/10/2025 in #❓・help
middleware on nested routes
pages:extends from top of my head
15 replies
NNuxt
Created by JuanMCe on 4/10/2025 in #❓・help
middleware on nested routes
You can either add it on all page files under the admin folder or assign the middleware to all pages in the folder in a hook
15 replies
NNuxt
Created by JuanMCe on 4/10/2025 in #❓・help
middleware on nested routes
This is expected behaviour
15 replies
NNuxt
Created by nandi95 on 4/10/2025 in #❓・help
tests are queued but never run
the middleware for reference:
export default defineNuxtRouteMiddleware(async () => {
const { ready, loggedIn, fetch } = useUserSession();

// If the session is not ready yet, fetch it first
if (!ready.value) {
// this won't resolve in tests
await fetch();
}

// After fetching (or if already ready), check if user is logged in
if (!loggedIn.value) {
// Redirect to login page if not logged in,
// You can specify a redirect query parameter to return after login
return navigateTo({
path: '/auth/auth0'
// to implement redirect, we need to have a way to save state while auth0 login flow is ongoing
// query: { redirect: to.fullPath }
}, { external: true });
}
});
export default defineNuxtRouteMiddleware(async () => {
const { ready, loggedIn, fetch } = useUserSession();

// If the session is not ready yet, fetch it first
if (!ready.value) {
// this won't resolve in tests
await fetch();
}

// After fetching (or if already ready), check if user is logged in
if (!loggedIn.value) {
// Redirect to login page if not logged in,
// You can specify a redirect query parameter to return after login
return navigateTo({
path: '/auth/auth0'
// to implement redirect, we need to have a way to save state while auth0 login flow is ongoing
// query: { redirect: to.fullPath }
}, { external: true });
}
});
30 replies
NNuxt
Created by nandi95 on 4/10/2025 in #❓・help
tests are queued but never run
sure I mean more like the forever going promise
30 replies
NNuxt
Created by nandi95 on 4/10/2025 in #❓・help
tests are queued but never run
@danielroe is this worthy of raising as an issue?
30 replies
NNuxt
Created by nandi95 on 4/10/2025 in #❓・help
tests are queued but never run
when changing to jsdom that give me an error that you can only navigate by hashes
30 replies
NNuxt
Created by nandi95 on 4/10/2025 in #❓・help
tests are queued but never run
when using nuxt vitest environment it runs your component in the context of your nuxt app (aka all the bells and whistles). This means in my case the auth middleware when runs makes a request which never completes because nuxt environment? Anyhow vitest happy to sit as long as you keep the process running without a warning 🙃
30 replies
NNuxt
Created by nandi95 on 4/10/2025 in #❓・help
tests are queued but never run
I just found the issue
30 replies
NNuxt
Created by nandi95 on 4/10/2025 in #❓・help
tests are queued but never run
Ah okay
30 replies
NNuxt
Created by nandi95 on 4/10/2025 in #❓・help
tests are queued but never run
I see Dan isn't using this vitest nuxt environment: https://github.com/danielroe/roe.dev/blob/main/test/unit/behaviour.spec.ts
30 replies
NNuxt
Created by nandi95 on 4/10/2025 in #❓・help
tests are queued but never run
it's something to do with the nuxt vitest environment, if I remove that, it works just fine but obviously errors on a nuxt related issue (accessing vueApp)
30 replies
NNuxt
Created by nandi95 on 4/10/2025 in #❓・help
tests are queued but never run
@kapa.ai re-evaluate with these comments in mind
30 replies
NNuxt
Created by nandi95 on 4/10/2025 in #❓・help
tests are queued but never run
that's with the nuxt config:
testUtils: {
startOnBoot: true,
logToConsole: true,
vitestConfig
}
testUtils: {
startOnBoot: true,
logToConsole: true,
vitestConfig
}
vitestConfig being an inport from the above file
30 replies
NNuxt
Created by nandi95 on 4/10/2025 in #❓・help
tests are queued but never run
No description
30 replies
NNuxt
Created by nandi95 on 4/10/2025 in #❓・help
tests are queued but never run
// drawer.test.ts
import { describe, test, expect } from 'vitest';
import { mountSuspended } from '@nuxt/test-utils/runtime';
import { DashboardDrawer } from '#components';

describe('Drawer Component', () => {
test('should not render when open is false', async () => {
expect(1).toBe(1);
const component = await mountSuspended(DashboardDrawer, {
slots: {
default: '<div class="test-content">Test Content</div>'
},
props: {
title: 'Test Title'
}
});

expect(component.html()).not.toContain('test-content');
});

test('should render when open is true', async () => {
const component = await mountSuspended(DashboardDrawer, {
slots: {
default: '<div class="test-content">Test Content</div>'
},
props: {
title: 'Test Title',
open: true
}
});

expect(component.html()).toContain('test-content');
expect(component.html()).toContain('Test Title');
});

test('should emit close event when close button is clicked', async () => {
const component = await mountSuspended(DashboardDrawer, {
slots: {
default: '<div class="test-content">Test Content</div>'
},
props: {
title: 'Test Title',
open: true
}
});

await component.get('button').trigger('click');

expect(component.emitted()).toHaveProperty('close');
});
});
// drawer.test.ts
import { describe, test, expect } from 'vitest';
import { mountSuspended } from '@nuxt/test-utils/runtime';
import { DashboardDrawer } from '#components';

describe('Drawer Component', () => {
test('should not render when open is false', async () => {
expect(1).toBe(1);
const component = await mountSuspended(DashboardDrawer, {
slots: {
default: '<div class="test-content">Test Content</div>'
},
props: {
title: 'Test Title'
}
});

expect(component.html()).not.toContain('test-content');
});

test('should render when open is true', async () => {
const component = await mountSuspended(DashboardDrawer, {
slots: {
default: '<div class="test-content">Test Content</div>'
},
props: {
title: 'Test Title',
open: true
}
});

expect(component.html()).toContain('test-content');
expect(component.html()).toContain('Test Title');
});

test('should emit close event when close button is clicked', async () => {
const component = await mountSuspended(DashboardDrawer, {
slots: {
default: '<div class="test-content">Test Content</div>'
},
props: {
title: 'Test Title',
open: true
}
});

await component.get('button').trigger('click');

expect(component.emitted()).toHaveProperty('close');
});
});
30 replies
NNuxt
Created by nandi95 on 4/10/2025 in #❓・help
tests are queued but never run
// vitest.config.ts
import { defineVitestConfig } from '@nuxt/test-utils/config';
import { configDefaults, defaultInclude } from 'vitest/config';
import { isCI } from 'std-env';

export default defineVitestConfig({
test: {
reporters: isCI ? ['dot', 'github-actions'] : ['default'],
expect: {
requireAssertions: true
},
exclude: [...configDefaults.exclude, 'tests/e2e/**'],
environment: 'nuxt',
workspace: [
{
extends: true,
test: {
name: 'unit',
include: ['tests/unit/' + defaultInclude[0]]
}
}
]
}
});
// vitest.config.ts
import { defineVitestConfig } from '@nuxt/test-utils/config';
import { configDefaults, defaultInclude } from 'vitest/config';
import { isCI } from 'std-env';

export default defineVitestConfig({
test: {
reporters: isCI ? ['dot', 'github-actions'] : ['default'],
expect: {
requireAssertions: true
},
exclude: [...configDefaults.exclude, 'tests/e2e/**'],
environment: 'nuxt',
workspace: [
{
extends: true,
test: {
name: 'unit',
include: ['tests/unit/' + defaultInclude[0]]
}
}
]
}
});
30 replies