chore: improve types in tests (#27293)

This commit is contained in:
Daniel Roe 2024-06-07 16:57:37 +01:00 committed by GitHub
parent 601a2620b8
commit 27945b4925
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 16 additions and 11 deletions

View File

@ -20,7 +20,6 @@ import { callOnce } from '#app/composables/once'
import { useLoadingIndicator } from '#app/composables/loading-indicator' import { useLoadingIndicator } from '#app/composables/loading-indicator'
import { useRouteAnnouncer } from '#app/composables/route-announcer' import { useRouteAnnouncer } from '#app/composables/route-announcer'
// @ts-expect-error virtual file
import { asyncDataDefaults, nuxtDefaultErrorValue } from '#build/nuxt.config.mjs' import { asyncDataDefaults, nuxtDefaultErrorValue } from '#build/nuxt.config.mjs'
registerEndpoint('/api/test', defineEventHandler(event => ({ registerEndpoint('/api/test', defineEventHandler(event => ({
@ -38,7 +37,6 @@ describe('app config', () => {
`) `)
updateAppConfig({ updateAppConfig({
new: 'value', new: 'value',
// @ts-expect-error property does not exist
nuxt: { nested: 42 }, nuxt: { nested: 42 },
}) })
expect(appConfig).toMatchInlineSnapshot(` expect(appConfig).toMatchInlineSnapshot(`
@ -165,7 +163,7 @@ describe('useAsyncData', () => {
// https://github.com/nuxt/nuxt/issues/23411 // https://github.com/nuxt/nuxt/issues/23411
it('should initialize with error set to null when immediate: false', async () => { it('should initialize with error set to null when immediate: false', async () => {
const { error, execute } = useAsyncData(() => ({}), { immediate: false }) const { error, execute } = useAsyncData(() => Promise.resolve({}), { immediate: false })
expect(error.value).toBe(asyncDataDefaults.errorValue) expect(error.value).toBe(asyncDataDefaults.errorValue)
await execute() await execute()
expect(error.value).toBe(asyncDataDefaults.errorValue) expect(error.value).toBe(asyncDataDefaults.errorValue)
@ -217,7 +215,7 @@ describe('useAsyncData', () => {
}) })
it('allows custom access to a cache', async () => { it('allows custom access to a cache', async () => {
const { data } = await useAsyncData(() => ({ val: true }), { getCachedData: () => ({ val: false }) }) const { data } = await useAsyncData(() => Promise.resolve({ val: true }), { getCachedData: () => ({ val: false }) })
expect(data.value).toMatchInlineSnapshot(` expect(data.value).toMatchInlineSnapshot(`
{ {
"val": false, "val": false,
@ -317,6 +315,7 @@ describe('useFetch', () => {
it('should timeout', async () => { it('should timeout', async () => {
const { status, error } = await useFetch( const { status, error } = await useFetch(
// @ts-expect-error should resolve to a string
() => new Promise(resolve => setTimeout(resolve, 5000)), () => new Promise(resolve => setTimeout(resolve, 5000)),
{ timeout: 1 }, { timeout: 1 },
) )
@ -534,6 +533,7 @@ describe('loading state', () => {
describe.skipIf(process.env.TEST_MANIFEST === 'manifest-off')('app manifests', () => { describe.skipIf(process.env.TEST_MANIFEST === 'manifest-off')('app manifests', () => {
it('getAppManifest', async () => { it('getAppManifest', async () => {
const manifest = await getAppManifest() const manifest = await getAppManifest()
// @ts-expect-error timestamp is not optional
delete manifest.timestamp delete manifest.timestamp
expect(manifest).toMatchInlineSnapshot(` expect(manifest).toMatchInlineSnapshot(`
{ {

View File

@ -263,12 +263,12 @@ describe('client components', () => {
const componentId = 'ClientWithSlot-12345' const componentId = 'ClientWithSlot-12345'
vi.doMock(mockPath, () => ({ vi.doMock(mockPath, () => ({
default: { default: defineComponent({
name: 'ClientWithSlot', name: 'ClientWithSlot',
setup (_, { slots }) { setup (_, { slots }) {
return () => h('div', { class: 'client-component' }, slots.default()) return () => h('div', { class: 'client-component' }, slots.default?.())
}, },
}, }),
})) }))
const stubFetch = vi.fn(() => { const stubFetch = vi.fn(() => {

View File

@ -11,9 +11,10 @@ vi.mock('#app', async (original) => {
} }
}) })
function pluginFactory (name: string, dependsOn?: string[], sequence: string[], parallel = true) { function pluginFactory (name: string, dependsOn: string[] | undefined, sequence: string[], parallel = true) {
return defineNuxtPlugin({ return defineNuxtPlugin({
name, name,
// @ts-expect-error we have a strong type for plugin names
dependsOn, dependsOn,
async setup () { async setup () {
sequence.push(`start ${name}`) sequence.push(`start ${name}`)
@ -71,7 +72,7 @@ describe('plugin dependsOn', () => {
pluginFactory('A', undefined, sequence), pluginFactory('A', undefined, sequence),
pluginFactory('B', ['A'], sequence), pluginFactory('B', ['A'], sequence),
defineNuxtPlugin({ defineNuxtPlugin({
name, name: 'some plugin',
async setup () { async setup () {
sequence.push('start C') sequence.push('start C')
await new Promise(resolve => setTimeout(resolve, 5)) await new Promise(resolve => setTimeout(resolve, 5))
@ -99,7 +100,7 @@ describe('plugin dependsOn', () => {
const plugins = [ const plugins = [
pluginFactory('A', undefined, sequence), pluginFactory('A', undefined, sequence),
defineNuxtPlugin({ defineNuxtPlugin({
name, name: 'some plugin',
async setup () { async setup () {
sequence.push('start C') sequence.push('start C')
await new Promise(resolve => setTimeout(resolve, 50)) await new Promise(resolve => setTimeout(resolve, 50))

3
test/nuxt/tsconfig.json Normal file
View File

@ -0,0 +1,3 @@
{
"extends": "../../.nuxt/tsconfig.json"
}

View File

@ -44,7 +44,8 @@
"**/examples/**", "**/examples/**",
"**/docs/**", "**/docs/**",
"**/playground/**", "**/playground/**",
"**/test/nuxt/**",
"**/test/fixtures/**", "**/test/fixtures/**",
"test/nuxt/**" "**/test/fixtures-temp/**"
] ]
} }