Open History to see the last commit
1import { describe, expectTypeOf, it } from 'vitest'
2β
3import {
4 Serializable,
5 TsrSerializable,
6 ValidateSerializable,
7 ValidateSerializableResult,
8} from '../src/ssr/serializer/transformer'
9β
10describe('Serializer', () => {
11 describe('Default types are serializable: $name', () => {
12 it('string', () => {
13 const value: string = 'hello'
14 expectTypeOf<
15 ValidateSerializableResult<typeof value, Serializable>
16 >().toBeString()
17 })
18 it('number', () => {
19 const value: number = 123
20β
21 expectTypeOf<
22 ValidateSerializableResult<typeof value, Serializable>
23 >().toBeNumber()
24 })
25 it('boolean', () => {
26 const value: boolean = true
27β
28 expectTypeOf<
29 ValidateSerializableResult<typeof value, Serializable>
30 >().toBeBoolean()
31 })
32 it('null', () => {
33 const value = null
34β
35 expectTypeOf<
36 ValidateSerializableResult<typeof value, Serializable>
37 >().toBeNull()
38 })
39 it('undefined', () => {
40 const value = undefined
41β
42 expectTypeOf<
43 ValidateSerializableResult<typeof value, Serializable>
44 >().toBeUndefined()
45 })
46 it('bigint', () => {
47 const value = BigInt(123)
48β
49 expectTypeOf<
50 ValidateSerializableResult<typeof value, Serializable>
51 >().toBeBigInt()
52 })
53 it('Date', () => {
54 const value = new Date()
55β
56 expectTypeOf<
57 ValidateSerializableResult<typeof value, Serializable>
58 >().toEqualTypeOf<Date>()
59 })
60 })
61 it('fails for non-serializable types', () => {
62 const value = () => {}
63 expectTypeOf<
64 ValidateSerializable<typeof value, Serializable>
65 >().toEqualTypeOf<'Function is not serializable'>()
66 })
67β
68 it('works for types extending TsrSerializable', () => {
69 type MyCustomType = { f: () => {} } & TsrSerializable
70 expectTypeOf<
71 ValidateSerializable<MyCustomType, Serializable>
72 >().toEqualTypeOf<MyCustomType>()
73 })
74})