ci: add types testing with tstyche (#9803)

As proposed here
https://github.com/payloadcms/payload/pull/9782#issuecomment-2522090135
with additional testing of our types we can be more sure that we don't
break them between updates.

This PR already adds types testing for most Local API methods
6beb921c2e/test/types/types.spec.ts
but new tests for types can be easily added, either to that same file or
you can create `types.spec.ts` in any other test folder.

The new test folder uses `strict: true` to ensure our types do not break
with it.

---------

Co-authored-by: Tom Mrazauskas <tom@mrazauskas.de>
This commit is contained in:
Sasha
2024-12-07 16:38:25 +02:00
committed by GitHub
parent 1fdc7cc70d
commit f09ee0b84b
11 changed files with 452 additions and 0 deletions

80
test/types/types.spec.ts Normal file
View File

@@ -0,0 +1,80 @@
import type { BulkOperationResult, PaginatedDocs, SelectType, TypeWithVersion } from 'payload'
import payload from 'payload'
import { describe, expect, test } from 'tstyche'
import type { Menu, Post, User } from './payload-types.js'
describe('Types testing', () => {
test('payload.find', () => {
expect(payload.find({ collection: 'users' })).type.toBe<Promise<PaginatedDocs<User>>>()
})
test('payload.findByID', () => {
expect(payload.findByID({ id: 1, collection: 'users' })).type.toBe<Promise<User>>()
})
test('payload.findByID with disableErrors: true', () => {
expect(payload.findByID({ id: 1, collection: 'users', disableErrors: true })).type.toBe<
Promise<null | User>
>()
})
test('payload.create', () => {
expect(payload.create({ collection: 'users', data: { email: 'user@email.com' } })).type.toBe<
Promise<User>
>()
})
test('payload.update by ID', () => {
expect(payload.update({ id: 1, collection: 'users', data: {} })).type.toBe<Promise<User>>()
})
test('payload.update many', () => {
expect(payload.update({ where: {}, collection: 'users', data: {} })).type.toBe<
Promise<BulkOperationResult<'users', SelectType>>
>()
})
test('payload.delete by ID', () => {
expect(payload.delete({ id: 1, collection: 'users' })).type.toBe<Promise<User>>()
})
test('payload.delete many', () => {
expect(payload.delete({ where: {}, collection: 'users' })).type.toBe<
Promise<BulkOperationResult<'users', SelectType>>
>()
})
test('payload.findGlobal', () => {
expect(payload.findGlobal({ slug: 'menu' })).type.toBe<Promise<Menu>>()
})
test('payload.updateGlobal', () => {
expect(payload.updateGlobal({ data: {}, slug: 'menu' })).type.toBe<Promise<Menu>>()
})
test('payload.findVersions', () => {
expect(payload.findVersions({ collection: 'posts' })).type.toBe<
Promise<PaginatedDocs<TypeWithVersion<Post>>>
>()
})
test('payload.findVersionByID', () => {
expect(payload.findVersionByID({ id: 'id', collection: 'posts' })).type.toBe<
Promise<TypeWithVersion<Post>>
>()
})
test('payload.findGlobalVersions', () => {
expect(payload.findGlobalVersions({ slug: 'menu' })).type.toBe<
Promise<PaginatedDocs<TypeWithVersion<Menu>>>
>()
})
test('payload.findGlobalVersionByID', () => {
expect(payload.findGlobalVersionByID({ id: 'id', slug: 'menu' })).type.toBe<
Promise<TypeWithVersion<Menu>>
>()
})
})