mirror of
https://github.com/nuxt/nuxt.git
synced 2025-02-19 15:10:58 +00:00
test: update type test for strict mode (#8669)
This commit is contained in:
parent
5a43e68e77
commit
ee8e9ae656
@ -1,6 +1,9 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
defineProps({
|
defineProps({
|
||||||
count: Number
|
count: {
|
||||||
|
type: Number,
|
||||||
|
required: true
|
||||||
|
}
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
6
test/fixtures/basic/nuxt.config.ts
vendored
6
test/fixtures/basic/nuxt.config.ts
vendored
@ -67,7 +67,7 @@ export default defineNuxtConfig({
|
|||||||
},
|
},
|
||||||
function (_options, nuxt) {
|
function (_options, nuxt) {
|
||||||
const routesToDuplicate = ['/async-parent', '/fixed-keyed-child-parent', '/keyed-child-parent', '/with-layout', '/with-layout2']
|
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,
|
...page,
|
||||||
children: page.children?.map(child => stripLayout(child)),
|
children: page.children?.map(child => stripLayout(child)),
|
||||||
name: 'internal-' + page.name,
|
name: 'internal-' + page.name,
|
||||||
@ -92,7 +92,7 @@ export default defineNuxtConfig({
|
|||||||
],
|
],
|
||||||
hooks: {
|
hooks: {
|
||||||
'prepare:types' ({ tsConfig }) {
|
'prepare:types' ({ tsConfig }) {
|
||||||
tsConfig.include = tsConfig.include.filter(i => i !== '../../../../**/*')
|
tsConfig.include = tsConfig.include!.filter(i => i !== '../../../../**/*')
|
||||||
},
|
},
|
||||||
'modules:done' () {
|
'modules:done' () {
|
||||||
addComponent({
|
addComponent({
|
||||||
@ -103,7 +103,7 @@ export default defineNuxtConfig({
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
experimental: {
|
experimental: {
|
||||||
inlineSSRStyles: id => !id.includes('assets.vue'),
|
inlineSSRStyles: id => !!id && !id.includes('assets.vue'),
|
||||||
reactivityTransform: true,
|
reactivityTransform: true,
|
||||||
treeshakeClientOnly: true
|
treeshakeClientOnly: true
|
||||||
},
|
},
|
||||||
|
@ -58,10 +58,13 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
const stringStatefulComp = ref(null)
|
import { Ref } from 'vue'
|
||||||
const stringStatefulScriptComp = ref(null)
|
type Comp = Ref<{ add: () => void }>
|
||||||
const clientScript = ref(null)
|
|
||||||
const clientSetupScript = ref(null)
|
const stringStatefulComp = ref(null) as any as Comp
|
||||||
|
const stringStatefulScriptComp = ref(null) as any as Comp
|
||||||
|
const clientScript = ref(null) as any as Comp
|
||||||
|
const clientSetupScript = ref(null) as any as Comp
|
||||||
|
|
||||||
const show = ref(false)
|
const show = ref(false)
|
||||||
</script>
|
</script>
|
||||||
|
@ -11,28 +11,28 @@
|
|||||||
const { data, refresh } = await useCounter()
|
const { data, refresh } = await useCounter()
|
||||||
const { data: data2, refresh: refresh2 } = 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
|
// Refresh on client and server side
|
||||||
await refresh()
|
await refresh()
|
||||||
|
|
||||||
if (data.value.count !== inital + 1) {
|
if (data.value!.count !== initial + 1) {
|
||||||
throw new Error('Data not refreshed?' + data.value.count + ' : ' + data2.value.count)
|
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')
|
throw new Error('AsyncData not synchronised')
|
||||||
}
|
}
|
||||||
|
|
||||||
inital = data.value.count
|
initial = data.value!.count
|
||||||
|
|
||||||
await refresh2()
|
await refresh2()
|
||||||
|
|
||||||
if (data.value.count !== inital + 1) {
|
if (data.value!.count !== initial + 1) {
|
||||||
throw new Error('data2 refresh not syncronised?')
|
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')
|
throw new Error('AsyncData not synchronised')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
56
test/fixtures/basic/types.ts
vendored
56
test/fixtures/basic/types.ts
vendored
@ -22,41 +22,41 @@ describe('API routes', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('works with useAsyncData', () => {
|
it('works with useAsyncData', () => {
|
||||||
expectTypeOf(useAsyncData('api-hello', () => $fetch('/api/hello')).data).toEqualTypeOf<Ref<string>>()
|
expectTypeOf(useAsyncData('api-hello', () => $fetch('/api/hello')).data).toEqualTypeOf<Ref<string | null>>()
|
||||||
expectTypeOf(useAsyncData('api-hey', () => $fetch('/api/hey')).data).toEqualTypeOf<Ref<{ foo: string, baz: string }>>()
|
expectTypeOf(useAsyncData('api-hey', () => $fetch('/api/hey')).data).toEqualTypeOf<Ref<{ foo: string, baz: string } | null>>()
|
||||||
expectTypeOf(useAsyncData('api-hey-with-pick', () => $fetch('/api/hey'), { pick: ['baz'] }).data).toEqualTypeOf<Ref<{ baz: string }>>()
|
expectTypeOf(useAsyncData('api-hey-with-pick', () => $fetch('/api/hey'), { pick: ['baz'] }).data).toEqualTypeOf<Ref<{ baz: string } | null>>()
|
||||||
expectTypeOf(useAsyncData('api-other', () => $fetch('/api/other')).data).toEqualTypeOf<Ref<unknown>>()
|
expectTypeOf(useAsyncData('api-other', () => $fetch('/api/other')).data).toEqualTypeOf<Ref<unknown>>()
|
||||||
expectTypeOf(useAsyncData<TestResponse>('api-generics', () => $fetch('/test')).data).toEqualTypeOf<Ref<TestResponse>>()
|
expectTypeOf(useAsyncData<TestResponse>('api-generics', () => $fetch('/test')).data).toEqualTypeOf<Ref<TestResponse | null>>()
|
||||||
|
|
||||||
expectTypeOf(useAsyncData('api-error-generics', () => $fetch('/error')).error).toEqualTypeOf<Ref<Error | null>>()
|
expectTypeOf(useAsyncData('api-error-generics', () => $fetch('/error')).error).toEqualTypeOf<Ref<Error | null>>()
|
||||||
expectTypeOf(useAsyncData<any, string>('api-error-generics', () => $fetch('/error')).error).toEqualTypeOf<Ref<string | null>>()
|
expectTypeOf(useAsyncData<any, string>('api-error-generics', () => $fetch('/error')).error).toEqualTypeOf<Ref<string | null>>()
|
||||||
|
|
||||||
expectTypeOf(useLazyAsyncData('lazy-api-hello', () => $fetch('/api/hello')).data).toEqualTypeOf<Ref<string>>()
|
expectTypeOf(useLazyAsyncData('lazy-api-hello', () => $fetch('/api/hello')).data).toEqualTypeOf<Ref<string | null>>()
|
||||||
expectTypeOf(useLazyAsyncData('lazy-api-hey', () => $fetch('/api/hey')).data).toEqualTypeOf<Ref<{ foo: string, baz: string }>>()
|
expectTypeOf(useLazyAsyncData('lazy-api-hey', () => $fetch('/api/hey')).data).toEqualTypeOf<Ref<{ foo: string, baz: string } | null>>()
|
||||||
expectTypeOf(useLazyAsyncData('lazy-api-hey-with-pick', () => $fetch('/api/hey'), { pick: ['baz'] }).data).toEqualTypeOf<Ref<{ baz: string }>>()
|
expectTypeOf(useLazyAsyncData('lazy-api-hey-with-pick', () => $fetch('/api/hey'), { pick: ['baz'] }).data).toEqualTypeOf<Ref<{ baz: string } | null>>()
|
||||||
expectTypeOf(useLazyAsyncData('lazy-api-other', () => $fetch('/api/other')).data).toEqualTypeOf<Ref<unknown>>()
|
expectTypeOf(useLazyAsyncData('lazy-api-other', () => $fetch('/api/other')).data).toEqualTypeOf<Ref<unknown>>()
|
||||||
expectTypeOf(useLazyAsyncData<TestResponse>('lazy-api-generics', () => $fetch('/test')).data).toEqualTypeOf<Ref<TestResponse>>()
|
expectTypeOf(useLazyAsyncData<TestResponse>('lazy-api-generics', () => $fetch('/test')).data).toEqualTypeOf<Ref<TestResponse | null>>()
|
||||||
|
|
||||||
expectTypeOf(useLazyAsyncData('lazy-error-generics', () => $fetch('/error')).error).toEqualTypeOf<Ref<Error | null>>()
|
expectTypeOf(useLazyAsyncData('lazy-error-generics', () => $fetch('/error')).error).toEqualTypeOf<Ref<Error | null>>()
|
||||||
expectTypeOf(useLazyAsyncData<any, string>('lazy-error-generics', () => $fetch('/error')).error).toEqualTypeOf<Ref<string | null>>()
|
expectTypeOf(useLazyAsyncData<any, string>('lazy-error-generics', () => $fetch('/error')).error).toEqualTypeOf<Ref<string | null>>()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('works with useFetch', () => {
|
it('works with useFetch', () => {
|
||||||
expectTypeOf(useFetch('/api/hello').data).toEqualTypeOf<Ref<string>>()
|
expectTypeOf(useFetch('/api/hello').data).toEqualTypeOf<Ref<string | null>>()
|
||||||
expectTypeOf(useFetch('/api/hey').data).toEqualTypeOf<Ref<{ foo: string, baz: string }>>()
|
expectTypeOf(useFetch('/api/hey').data).toEqualTypeOf<Ref<{ foo: string, baz: string } | null>>()
|
||||||
expectTypeOf(useFetch('/api/hey', { pick: ['baz'] }).data).toEqualTypeOf<Ref<{ baz: string }>>()
|
expectTypeOf(useFetch('/api/hey', { pick: ['baz'] }).data).toEqualTypeOf<Ref<{ baz: string } | null>>()
|
||||||
expectTypeOf(useFetch('/api/other').data).toEqualTypeOf<Ref<unknown>>()
|
expectTypeOf(useFetch('/api/other').data).toEqualTypeOf<Ref<unknown>>()
|
||||||
expectTypeOf(useFetch<TestResponse>('/test').data).toEqualTypeOf<Ref<TestResponse>>()
|
expectTypeOf(useFetch<TestResponse>('/test').data).toEqualTypeOf<Ref<TestResponse | null>>()
|
||||||
|
|
||||||
expectTypeOf(useFetch('/error').error).toEqualTypeOf<Ref<FetchError | null>>()
|
expectTypeOf(useFetch('/error').error).toEqualTypeOf<Ref<FetchError | null>>()
|
||||||
expectTypeOf(useFetch<any, string>('/error').error).toEqualTypeOf<Ref<string | null>>()
|
expectTypeOf(useFetch<any, string>('/error').error).toEqualTypeOf<Ref<string | null>>()
|
||||||
|
|
||||||
expectTypeOf(useLazyFetch('/api/hello').data).toEqualTypeOf<Ref<string>>()
|
expectTypeOf(useLazyFetch('/api/hello').data).toEqualTypeOf<Ref<string | null>>()
|
||||||
expectTypeOf(useLazyFetch('/api/hey').data).toEqualTypeOf<Ref<{ foo: string, baz: string }>>()
|
expectTypeOf(useLazyFetch('/api/hey').data).toEqualTypeOf<Ref<{ foo: string, baz: string } | null>>()
|
||||||
expectTypeOf(useLazyFetch('/api/hey', { pick: ['baz'] }).data).toEqualTypeOf<Ref<{ baz: string }>>()
|
expectTypeOf(useLazyFetch('/api/hey', { pick: ['baz'] }).data).toEqualTypeOf<Ref<{ baz: string } | null>>()
|
||||||
expectTypeOf(useLazyFetch('/api/other').data).toEqualTypeOf<Ref<unknown>>()
|
expectTypeOf(useLazyFetch('/api/other').data).toEqualTypeOf<Ref<unknown>>()
|
||||||
expectTypeOf(useLazyFetch('/api/other').data).toEqualTypeOf<Ref<unknown>>()
|
expectTypeOf(useLazyFetch('/api/other').data).toEqualTypeOf<Ref<unknown>>()
|
||||||
expectTypeOf(useLazyFetch<TestResponse>('/test').data).toEqualTypeOf<Ref<TestResponse>>()
|
expectTypeOf(useLazyFetch<TestResponse>('/test').data).toEqualTypeOf<Ref<TestResponse | null>>()
|
||||||
|
|
||||||
expectTypeOf(useLazyFetch('/error').error).toEqualTypeOf<Ref<FetchError | null>>()
|
expectTypeOf(useLazyFetch('/error').error).toEqualTypeOf<Ref<FetchError | null>>()
|
||||||
expectTypeOf(useLazyFetch<any, string>('/error').error).toEqualTypeOf<Ref<string | null>>()
|
expectTypeOf(useLazyFetch<any, string>('/error').error).toEqualTypeOf<Ref<string | null>>()
|
||||||
@ -82,7 +82,7 @@ describe('middleware', () => {
|
|||||||
addRouteMiddleware('example', (to, from) => {
|
addRouteMiddleware('example', (to, from) => {
|
||||||
expectTypeOf(to).toEqualTypeOf<RouteLocationNormalizedLoaded>()
|
expectTypeOf(to).toEqualTypeOf<RouteLocationNormalizedLoaded>()
|
||||||
expectTypeOf(from).toEqualTypeOf<RouteLocationNormalizedLoaded>()
|
expectTypeOf(from).toEqualTypeOf<RouteLocationNormalizedLoaded>()
|
||||||
expectTypeOf(navigateTo).toEqualTypeOf<(to: RouteLocationRaw, options?: NavigateToOptions) => RouteLocationRaw | Promise<void | NavigationFailure>>()
|
expectTypeOf(navigateTo).toEqualTypeOf<(to: RouteLocationRaw | null | undefined, options?: NavigateToOptions) => RouteLocationRaw | Promise<void | NavigationFailure>>()
|
||||||
navigateTo('/')
|
navigateTo('/')
|
||||||
abortNavigation()
|
abortNavigation()
|
||||||
abortNavigation('error string')
|
abortNavigation('error string')
|
||||||
@ -157,29 +157,29 @@ describe('composables', () => {
|
|||||||
expectTypeOf(useCookie('test', { default: () => ref(500) })).toEqualTypeOf<Ref<number>>()
|
expectTypeOf(useCookie('test', { default: () => ref(500) })).toEqualTypeOf<Ref<number>>()
|
||||||
expectTypeOf(useCookie('test', { default: () => 500 })).toEqualTypeOf<Ref<number>>()
|
expectTypeOf(useCookie('test', { default: () => 500 })).toEqualTypeOf<Ref<number>>()
|
||||||
|
|
||||||
expectTypeOf(useAsyncData('test', () => Promise.resolve(500), { default: () => ref(500) }).data).toEqualTypeOf<Ref<number>>()
|
expectTypeOf(useAsyncData('test', () => Promise.resolve(500), { default: () => ref(500) }).data).toEqualTypeOf<Ref<number | null>>()
|
||||||
expectTypeOf(useAsyncData('test', () => Promise.resolve(500), { default: () => 500 }).data).toEqualTypeOf<Ref<number>>()
|
expectTypeOf(useAsyncData('test', () => Promise.resolve(500), { default: () => 500 }).data).toEqualTypeOf<Ref<number | null>>()
|
||||||
// @ts-expect-error
|
// @ts-expect-error
|
||||||
expectTypeOf(useAsyncData('test', () => Promise.resolve('500'), { default: () => ref(500) }).data).toEqualTypeOf<Ref<number>>()
|
expectTypeOf(useAsyncData('test', () => Promise.resolve('500'), { default: () => ref(500) }).data).toEqualTypeOf<Ref<number | null>>()
|
||||||
// @ts-expect-error
|
// @ts-expect-error
|
||||||
expectTypeOf(useAsyncData('test', () => Promise.resolve('500'), { default: () => 500 }).data).toEqualTypeOf<Ref<number>>()
|
expectTypeOf(useAsyncData('test', () => Promise.resolve('500'), { default: () => 500 }).data).toEqualTypeOf<Ref<number | null>>()
|
||||||
|
|
||||||
expectTypeOf(useFetch('/test', { default: () => ref(500) }).data).toEqualTypeOf<Ref<number>>()
|
expectTypeOf(useFetch('/test', { default: () => ref(500) }).data).toEqualTypeOf<Ref<number | null>>()
|
||||||
expectTypeOf(useFetch('/test', { default: () => 500 }).data).toEqualTypeOf<Ref<number>>()
|
expectTypeOf(useFetch('/test', { default: () => 500 }).data).toEqualTypeOf<Ref<number | null>>()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('infer request url string literal from server/api routes', () => {
|
it('infer request url string literal from server/api routes', () => {
|
||||||
// request can accept dynamic string type
|
// request can accept dynamic string type
|
||||||
const dynamicStringUrl: string = 'https://example.com/api'
|
const dynamicStringUrl: string = 'https://example.com/api'
|
||||||
expectTypeOf(useFetch(dynamicStringUrl).data).toEqualTypeOf<Ref<Pick<unknown, never>>>()
|
expectTypeOf(useFetch(dynamicStringUrl).data).toEqualTypeOf<Ref<unknown>>()
|
||||||
|
|
||||||
// request param should infer string literal type / show auto-complete hint base on server routes, ex: '/api/hello'
|
// request param should infer string literal type / show auto-complete hint base on server routes, ex: '/api/hello'
|
||||||
expectTypeOf(useFetch('/api/hello').data).toEqualTypeOf<Ref<string>>()
|
expectTypeOf(useFetch('/api/hello').data).toEqualTypeOf<Ref<string | null>>()
|
||||||
expectTypeOf(useLazyFetch('/api/hello').data).toEqualTypeOf<Ref<string>>()
|
expectTypeOf(useLazyFetch('/api/hello').data).toEqualTypeOf<Ref<string | null>>()
|
||||||
|
|
||||||
// request can accept string literal and Request object type
|
// request can accept string literal and Request object type
|
||||||
expectTypeOf(useFetch('https://example.com/api').data).toEqualTypeOf<Ref<Pick<unknown, never>>>()
|
expectTypeOf(useFetch('https://example.com/api').data).toEqualTypeOf<Ref<unknown>>()
|
||||||
expectTypeOf(useFetch(new Request('test')).data).toEqualTypeOf<Ref<Pick<unknown, never>>>()
|
expectTypeOf(useFetch(new Request('test')).data).toEqualTypeOf<Ref<unknown>>()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('provides proper type support when using overloads', () => {
|
it('provides proper type support when using overloads', () => {
|
||||||
|
Loading…
Reference in New Issue
Block a user