1
+ import { setTimeout } from 'node:timers/promises' ;
1
2
import { describe , expect , it } from 'vitest' ;
2
3
import { validator } from '../../src/decorator.js' ;
3
4
import { SchemaValidationError } from '../../src/errors.js' ;
@@ -21,29 +22,31 @@ const outboundSchema = {
21
22
} ;
22
23
23
24
describe ( 'Decorator: validator' , ( ) => {
24
- it ( 'should validate inbound and outbound successfully' , ( ) => {
25
+ it ( 'validates both inbound and outbound successfully' , async ( ) => {
25
26
// Prepare
26
27
class TestClass {
27
28
@validator ( { inboundSchema, outboundSchema } )
28
- multiply ( input : { value : number } ) : { result : number } {
29
+ async multiply ( input : { value : number } ) : Promise < { result : number } > {
30
+ await setTimeout ( 1 ) ; // simulate some processing time
29
31
return { result : input . value * 2 } ;
30
32
}
31
33
}
32
34
const instance = new TestClass ( ) ;
33
35
const input = { value : 5 } ;
34
36
35
37
// Act
36
- const output = instance . multiply ( input ) ;
38
+ const output = await instance . multiply ( input ) ;
37
39
38
40
// Assess
39
41
expect ( output ) . toEqual ( { result : 10 } ) ;
40
42
} ) ;
41
43
42
- it ( 'should throw error on inbound validation failure' , ( ) => {
44
+ it ( 'throws an error on inbound validation failure' , async ( ) => {
43
45
// Prepare
44
46
class TestClass {
45
47
@validator ( { inboundSchema, outboundSchema } )
46
- multiply ( input : { value : number } ) : { result : number } {
48
+ async multiply ( input : { value : number } ) : Promise < { result : number } > {
49
+ await setTimeout ( 1 ) ; // simulate some processing time
47
50
return { result : input . value * 2 } ;
48
51
}
49
52
}
@@ -53,72 +56,80 @@ describe('Decorator: validator', () => {
53
56
} ;
54
57
55
58
// Act & Assess
56
- expect ( instance . multiply ( invalidInput ) ) . toThrow ( SchemaValidationError ) ;
59
+ await expect ( instance . multiply ( invalidInput ) ) . rejects . toThrow (
60
+ SchemaValidationError
61
+ ) ;
57
62
} ) ;
58
63
59
- it ( 'should throw error on outbound validation failure' , ( ) => {
64
+ it ( 'throws an error on outbound validation failure' , async ( ) => {
60
65
// Prepare
61
66
class TestClassInvalid {
62
67
@validator ( { inboundSchema, outboundSchema } )
63
- multiply ( _input : { value : number } ) {
68
+ async multiply ( _input : { value : number } ) {
69
+ await setTimeout ( 1 ) ; // simulate some processing time
64
70
return { result : 'invalid' } ;
65
71
}
66
72
}
67
73
const instance = new TestClassInvalid ( ) ;
68
74
69
75
// Act & Assess
70
- expect ( instance . multiply ( { value : 5 } ) ) . toThrow ( SchemaValidationError ) ;
76
+ await expect ( instance . multiply ( { value : 5 } ) ) . rejects . toThrow (
77
+ SchemaValidationError
78
+ ) ;
71
79
} ) ;
72
80
73
- it ( 'should no-op when no schemas are provided' , ( ) => {
81
+ it ( 'results in a no-op when no schemas are provided' , async ( ) => {
74
82
// Prepare
75
83
class TestClassNoOp {
76
84
@validator ( { } )
77
- echo ( input : unknown ) : unknown {
85
+ async echo ( input : unknown ) : Promise < unknown > {
86
+ await setTimeout ( 1 ) ; // simulate some processing time
78
87
return input ;
79
88
}
80
89
}
81
90
const instance = new TestClassNoOp ( ) ;
82
91
const data = { foo : 'bar' } ;
83
92
84
93
// Act
85
- const result = instance . echo ( data ) ;
94
+ const result = await instance . echo ( data ) ;
86
95
87
96
// Assess
88
97
expect ( result ) . toEqual ( data ) ;
89
98
} ) ;
90
99
91
- it ( 'should validate inbound only' , ( ) => {
100
+ it ( 'validates the inbound schema only' , async ( ) => {
92
101
// Prepare
93
102
class TestClassInbound {
94
103
@validator ( { inboundSchema } )
95
- process ( input : { value : number } ) : { data : string } {
104
+ async process ( input : { value : number } ) : Promise < { data : string } > {
105
+ await setTimeout ( 1 ) ; // simulate some processing time
96
106
return { data : JSON . stringify ( input ) } ;
97
107
}
98
108
}
99
109
const instance = new TestClassInbound ( ) ;
100
110
const input = { value : 10 } ;
101
111
102
112
// Act
103
- const output = instance . process ( input ) ;
113
+ const output = await instance . process ( input ) ;
104
114
105
115
// Assess
106
116
expect ( output ) . toEqual ( { data : JSON . stringify ( input ) } ) ;
107
117
} ) ;
108
118
109
- it ( 'should validate outbound only' , ( ) => {
119
+ it ( 'validates the outbound schema only' , async ( ) => {
110
120
// Prepare
111
121
class TestClassOutbound {
112
122
@validator ( { outboundSchema } )
113
- process ( _input : { text : string } ) : { result : number } {
123
+ async process ( _input : { text : string } ) : Promise < { result : number } > {
124
+ await setTimeout ( 1 ) ; // simulate some processing time
114
125
return { result : 42 } ;
115
126
}
116
127
}
117
128
const instance = new TestClassOutbound ( ) ;
118
129
const input = { text : 'hello' } ;
119
130
120
131
// Act
121
- const output = instance . process ( input ) ;
132
+ const output = await instance . process ( input ) ;
122
133
123
134
// Assess
124
135
expect ( output ) . toEqual ( { result : 42 } ) ;
0 commit comments