From 2eda03509b0cae2002b22480c722272954d7dc25 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Thu, 3 Nov 2022 16:04:23 -0400 Subject: [PATCH 1/2] test: update type test for strict mode --- test/fixtures/basic/types.ts | 56 ++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/test/fixtures/basic/types.ts b/test/fixtures/basic/types.ts index 40b90b012e4..69899120afc 100644 --- a/test/fixtures/basic/types.ts +++ b/test/fixtures/basic/types.ts @@ -22,41 +22,41 @@ describe('API routes', () => { }) it('works with useAsyncData', () => { - expectTypeOf(useAsyncData('api-hello', () => $fetch('/api/hello')).data).toEqualTypeOf>() - expectTypeOf(useAsyncData('api-hey', () => $fetch('/api/hey')).data).toEqualTypeOf>() - expectTypeOf(useAsyncData('api-hey-with-pick', () => $fetch('/api/hey'), { pick: ['baz'] }).data).toEqualTypeOf>() + expectTypeOf(useAsyncData('api-hello', () => $fetch('/api/hello')).data).toEqualTypeOf>() + expectTypeOf(useAsyncData('api-hey', () => $fetch('/api/hey')).data).toEqualTypeOf>() + expectTypeOf(useAsyncData('api-hey-with-pick', () => $fetch('/api/hey'), { pick: ['baz'] }).data).toEqualTypeOf>() expectTypeOf(useAsyncData('api-other', () => $fetch('/api/other')).data).toEqualTypeOf>() - expectTypeOf(useAsyncData('api-generics', () => $fetch('/test')).data).toEqualTypeOf>() + expectTypeOf(useAsyncData('api-generics', () => $fetch('/test')).data).toEqualTypeOf>() expectTypeOf(useAsyncData('api-error-generics', () => $fetch('/error')).error).toEqualTypeOf>() expectTypeOf(useAsyncData('api-error-generics', () => $fetch('/error')).error).toEqualTypeOf>() - expectTypeOf(useLazyAsyncData('lazy-api-hello', () => $fetch('/api/hello')).data).toEqualTypeOf>() - expectTypeOf(useLazyAsyncData('lazy-api-hey', () => $fetch('/api/hey')).data).toEqualTypeOf>() - expectTypeOf(useLazyAsyncData('lazy-api-hey-with-pick', () => $fetch('/api/hey'), { pick: ['baz'] }).data).toEqualTypeOf>() + expectTypeOf(useLazyAsyncData('lazy-api-hello', () => $fetch('/api/hello')).data).toEqualTypeOf>() + expectTypeOf(useLazyAsyncData('lazy-api-hey', () => $fetch('/api/hey')).data).toEqualTypeOf>() + expectTypeOf(useLazyAsyncData('lazy-api-hey-with-pick', () => $fetch('/api/hey'), { pick: ['baz'] }).data).toEqualTypeOf>() expectTypeOf(useLazyAsyncData('lazy-api-other', () => $fetch('/api/other')).data).toEqualTypeOf>() - expectTypeOf(useLazyAsyncData('lazy-api-generics', () => $fetch('/test')).data).toEqualTypeOf>() + expectTypeOf(useLazyAsyncData('lazy-api-generics', () => $fetch('/test')).data).toEqualTypeOf>() expectTypeOf(useLazyAsyncData('lazy-error-generics', () => $fetch('/error')).error).toEqualTypeOf>() expectTypeOf(useLazyAsyncData('lazy-error-generics', () => $fetch('/error')).error).toEqualTypeOf>() }) it('works with useFetch', () => { - expectTypeOf(useFetch('/api/hello').data).toEqualTypeOf>() - expectTypeOf(useFetch('/api/hey').data).toEqualTypeOf>() - expectTypeOf(useFetch('/api/hey', { pick: ['baz'] }).data).toEqualTypeOf>() + expectTypeOf(useFetch('/api/hello').data).toEqualTypeOf>() + expectTypeOf(useFetch('/api/hey').data).toEqualTypeOf>() + expectTypeOf(useFetch('/api/hey', { pick: ['baz'] }).data).toEqualTypeOf>() expectTypeOf(useFetch('/api/other').data).toEqualTypeOf>() - expectTypeOf(useFetch('/test').data).toEqualTypeOf>() + expectTypeOf(useFetch('/test').data).toEqualTypeOf>() expectTypeOf(useFetch('/error').error).toEqualTypeOf>() expectTypeOf(useFetch('/error').error).toEqualTypeOf>() - expectTypeOf(useLazyFetch('/api/hello').data).toEqualTypeOf>() - expectTypeOf(useLazyFetch('/api/hey').data).toEqualTypeOf>() - expectTypeOf(useLazyFetch('/api/hey', { pick: ['baz'] }).data).toEqualTypeOf>() + expectTypeOf(useLazyFetch('/api/hello').data).toEqualTypeOf>() + expectTypeOf(useLazyFetch('/api/hey').data).toEqualTypeOf>() + expectTypeOf(useLazyFetch('/api/hey', { pick: ['baz'] }).data).toEqualTypeOf>() expectTypeOf(useLazyFetch('/api/other').data).toEqualTypeOf>() expectTypeOf(useLazyFetch('/api/other').data).toEqualTypeOf>() - expectTypeOf(useLazyFetch('/test').data).toEqualTypeOf>() + expectTypeOf(useLazyFetch('/test').data).toEqualTypeOf>() expectTypeOf(useLazyFetch('/error').error).toEqualTypeOf>() expectTypeOf(useLazyFetch('/error').error).toEqualTypeOf>() @@ -82,7 +82,7 @@ describe('middleware', () => { addRouteMiddleware('example', (to, from) => { expectTypeOf(to).toEqualTypeOf() expectTypeOf(from).toEqualTypeOf() - expectTypeOf(navigateTo).toEqualTypeOf<(to: RouteLocationRaw, options?: NavigateToOptions) => RouteLocationRaw | Promise>() + expectTypeOf(navigateTo).toEqualTypeOf<(to: RouteLocationRaw | null | undefined, options?: NavigateToOptions) => RouteLocationRaw | Promise>() navigateTo('/') abortNavigation() abortNavigation('error string') @@ -157,29 +157,29 @@ describe('composables', () => { expectTypeOf(useCookie('test', { default: () => ref(500) })).toEqualTypeOf>() expectTypeOf(useCookie('test', { default: () => 500 })).toEqualTypeOf>() - expectTypeOf(useAsyncData('test', () => Promise.resolve(500), { default: () => ref(500) }).data).toEqualTypeOf>() - expectTypeOf(useAsyncData('test', () => Promise.resolve(500), { default: () => 500 }).data).toEqualTypeOf>() + expectTypeOf(useAsyncData('test', () => Promise.resolve(500), { default: () => ref(500) }).data).toEqualTypeOf>() + expectTypeOf(useAsyncData('test', () => Promise.resolve(500), { default: () => 500 }).data).toEqualTypeOf>() // @ts-expect-error - expectTypeOf(useAsyncData('test', () => Promise.resolve('500'), { default: () => ref(500) }).data).toEqualTypeOf>() + expectTypeOf(useAsyncData('test', () => Promise.resolve('500'), { default: () => ref(500) }).data).toEqualTypeOf>() // @ts-expect-error - expectTypeOf(useAsyncData('test', () => Promise.resolve('500'), { default: () => 500 }).data).toEqualTypeOf>() + expectTypeOf(useAsyncData('test', () => Promise.resolve('500'), { default: () => 500 }).data).toEqualTypeOf>() - expectTypeOf(useFetch('/test', { default: () => ref(500) }).data).toEqualTypeOf>() - expectTypeOf(useFetch('/test', { default: () => 500 }).data).toEqualTypeOf>() + expectTypeOf(useFetch('/test', { default: () => ref(500) }).data).toEqualTypeOf>() + expectTypeOf(useFetch('/test', { default: () => 500 }).data).toEqualTypeOf>() }) it('infer request url string literal from server/api routes', () => { // request can accept dynamic string type const dynamicStringUrl: string = 'https://example.com/api' - expectTypeOf(useFetch(dynamicStringUrl).data).toEqualTypeOf>>() + expectTypeOf(useFetch(dynamicStringUrl).data).toEqualTypeOf>() // request param should infer string literal type / show auto-complete hint base on server routes, ex: '/api/hello' - expectTypeOf(useFetch('/api/hello').data).toEqualTypeOf>() - expectTypeOf(useLazyFetch('/api/hello').data).toEqualTypeOf>() + expectTypeOf(useFetch('/api/hello').data).toEqualTypeOf>() + expectTypeOf(useLazyFetch('/api/hello').data).toEqualTypeOf>() // request can accept string literal and Request object type - expectTypeOf(useFetch('https://example.com/api').data).toEqualTypeOf>>() - expectTypeOf(useFetch(new Request('test')).data).toEqualTypeOf>>() + expectTypeOf(useFetch('https://example.com/api').data).toEqualTypeOf>() + expectTypeOf(useFetch(new Request('test')).data).toEqualTypeOf>() }) it('provides proper type support when using overloads', () => { From efbfe7778c500a838d5b7aee964fa24112d77f7e Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Thu, 3 Nov 2022 16:18:08 -0400 Subject: [PATCH 2/2] test: update more bits --- .../basic/components/Nested/SugarCounter.vue | 5 ++++- test/fixtures/basic/nuxt.config.ts | 6 +++--- .../basic/pages/client-only-components.vue | 11 +++++++---- test/fixtures/basic/pages/useAsyncData/refresh.vue | 14 +++++++------- 4 files changed, 21 insertions(+), 15 deletions(-) diff --git a/test/fixtures/basic/components/Nested/SugarCounter.vue b/test/fixtures/basic/components/Nested/SugarCounter.vue index 224dca4e5e2..059cf8f5f2b 100644 --- a/test/fixtures/basic/components/Nested/SugarCounter.vue +++ b/test/fixtures/basic/components/Nested/SugarCounter.vue @@ -1,6 +1,9 @@ diff --git a/test/fixtures/basic/nuxt.config.ts b/test/fixtures/basic/nuxt.config.ts index 884b0af0748..65664c7d715 100644 --- a/test/fixtures/basic/nuxt.config.ts +++ b/test/fixtures/basic/nuxt.config.ts @@ -67,7 +67,7 @@ export default defineNuxtConfig({ }, function (_options, nuxt) { const routesToDuplicate = ['/async-parent', '/fixed-keyed-child-parent', '/keyed-child-parent', '/with-layout', '/with-layout2'] - const stripLayout = (page: NuxtPage) => ({ + const stripLayout = (page: NuxtPage): NuxtPage => ({ ...page, children: page.children?.map(child => stripLayout(child)), name: 'internal-' + page.name, @@ -92,7 +92,7 @@ export default defineNuxtConfig({ ], hooks: { 'prepare:types' ({ tsConfig }) { - tsConfig.include = tsConfig.include.filter(i => i !== '../../../../**/*') + tsConfig.include = tsConfig.include!.filter(i => i !== '../../../../**/*') }, 'modules:done' () { addComponent({ @@ -103,7 +103,7 @@ export default defineNuxtConfig({ } }, experimental: { - inlineSSRStyles: id => !id.includes('assets.vue'), + inlineSSRStyles: id => !!id && !id.includes('assets.vue'), reactivityTransform: true, treeshakeClientOnly: true }, diff --git a/test/fixtures/basic/pages/client-only-components.vue b/test/fixtures/basic/pages/client-only-components.vue index e5e1094a92e..ca18e6ef848 100644 --- a/test/fixtures/basic/pages/client-only-components.vue +++ b/test/fixtures/basic/pages/client-only-components.vue @@ -58,10 +58,13 @@ diff --git a/test/fixtures/basic/pages/useAsyncData/refresh.vue b/test/fixtures/basic/pages/useAsyncData/refresh.vue index 5f312fd434c..c4cf1021d6e 100644 --- a/test/fixtures/basic/pages/useAsyncData/refresh.vue +++ b/test/fixtures/basic/pages/useAsyncData/refresh.vue @@ -11,28 +11,28 @@ const { data, refresh } = await useCounter() const { data: data2, refresh: refresh2 } = await useCounter() -let inital = data.value.count +let initial = data.value!.count // Refresh on client and server side await refresh() -if (data.value.count !== inital + 1) { - throw new Error('Data not refreshed?' + data.value.count + ' : ' + data2.value.count) +if (data.value!.count !== initial + 1) { + throw new Error('Data not refreshed?' + data.value!.count + ' : ' + data2.value!.count) } -if (data.value.count !== data2.value.count) { +if (data.value!.count !== data2.value!.count) { throw new Error('AsyncData not synchronised') } -inital = data.value.count +initial = data.value!.count await refresh2() -if (data.value.count !== inital + 1) { +if (data.value!.count !== initial + 1) { throw new Error('data2 refresh not syncronised?') } -if (data.value.count !== data2.value.count) { +if (data.value!.count !== data2.value!.count) { throw new Error('AsyncData not synchronised') }