1
- const mockV4Sign = jest . fn ( ) ;
2
- const mockPresign = jest . fn ( ) ;
3
- const mockV4 = jest . fn ( ) . mockReturnValue ( {
4
- presign : mockPresign ,
5
- sign : mockV4Sign ,
6
- } ) ;
7
- jest . mock ( "@smithy/signature-v4" , ( ) => ( {
8
- SignatureV4 : mockV4 ,
9
- } ) ) ;
1
+ import { beforeEach , describe , expect , test as it , vi } from "vitest" ;
10
2
3
+ vi . mock ( "@smithy/signature-v4" , ( ) => ( {
4
+ SignatureV4 : vi . fn ( ) . mockReturnValue ( {
5
+ presign : vi . fn ( ) ,
6
+ sign : vi . fn ( ) ,
7
+ } ) ,
8
+ } ) ) ;
11
9
import { PollyClient , SynthesizeSpeechCommand } from "@aws-sdk/client-polly" ;
10
+ import { SignatureV4 } from "@smithy/signature-v4" ;
12
11
13
- jest . mock ( "@aws-sdk/util-format-url" , ( ) => ( {
12
+ vi . mock ( "@aws-sdk/util-format-url" , ( ) => ( {
14
13
formatUrl : ( url : any ) => url ,
15
14
} ) ) ;
16
15
@@ -19,6 +18,7 @@ import { AwsCredentialIdentity, RequestPresigningArguments } from "@smithy/types
19
18
import { getSignedUrl } from "./getSignedUrls" ;
20
19
21
20
describe ( "getSignedUrl" , ( ) => {
21
+ const mockInstance = new SignatureV4 ( { } as any ) ;
22
22
const credentials : AwsCredentialIdentity = {
23
23
secretAccessKey : "unit-test" ,
24
24
accessKeyId : "unit-test" ,
@@ -27,12 +27,12 @@ describe("getSignedUrl", () => {
27
27
const clientParams = { region : "us-foo-1" , credentials } ;
28
28
29
29
beforeEach ( ( ) => {
30
- mockPresign . mockReset ( ) ;
30
+ vi . mocked ( mockInstance ) . presign . mockReset ( ) ;
31
31
} ) ;
32
32
33
33
it ( "should call SignatureV4.sign" , async ( ) => {
34
34
const mockPresigned = "a presigned url" ;
35
- mockPresign . mockReturnValue ( mockPresigned ) ;
35
+ vi . mocked ( mockInstance ) . presign . mockReturnValue ( mockPresigned as any ) ;
36
36
const client = new PollyClient ( clientParams ) ;
37
37
const command = new SynthesizeSpeechCommand ( {
38
38
Text : "hello world, this is alex" ,
@@ -41,15 +41,15 @@ describe("getSignedUrl", () => {
41
41
} ) ;
42
42
const signed = await getSignedUrl ( client , command ) ;
43
43
expect ( signed ) . toBe ( mockPresigned ) ;
44
- expect ( mockPresign ) . toBeCalled ( ) ;
45
- expect ( mockV4Sign ) . not . toBeCalled ( ) ;
44
+ expect ( vi . mocked ( mockInstance ) . presign ) . toBeCalled ( ) ;
45
+ expect ( vi . mocked ( mockInstance ) . sign ) . not . toBeCalled ( ) ;
46
46
expect ( client . middlewareStack . remove ( "presignInterceptMiddleware" ) ) . toBe ( false ) ;
47
47
expect ( command . middlewareStack . remove ( "presignInterceptMiddleware" ) ) . toBe ( false ) ;
48
48
} ) ;
49
49
50
50
it ( "should presign with signing region and service in context if exists" , async ( ) => {
51
51
const mockPresigned = "a presigned url" ;
52
- mockPresign . mockReturnValue ( mockPresigned ) ;
52
+ vi . mocked ( mockInstance ) . presign . mockReturnValue ( mockPresigned as any ) ;
53
53
const signingRegion = "aws-foo-1" ;
54
54
const signingService = "bar" ;
55
55
const client = new PollyClient ( clientParams ) ;
@@ -70,16 +70,16 @@ describe("getSignedUrl", () => {
70
70
VoiceId : "Kimberly" ,
71
71
} ) ;
72
72
await getSignedUrl ( client , command ) ;
73
- expect ( mockPresign ) . toBeCalled ( ) ;
74
- expect ( mockPresign . mock . calls [ 0 ] [ 1 ] ) . toMatchObject ( {
73
+ expect ( vi . mocked ( mockInstance ) . presign ) . toBeCalled ( ) ;
74
+ expect ( vi . mocked ( mockInstance ) . presign . mock . calls [ 0 ] [ 1 ] ) . toMatchObject ( {
75
75
signingRegion,
76
76
signingService,
77
77
} ) ;
78
78
} ) ;
79
79
80
80
it ( "should presign with parameters from presign options if set" , async ( ) => {
81
81
const mockPresigned = "a presigned url" ;
82
- mockPresign . mockReturnValue ( mockPresigned ) ;
82
+ vi . mocked ( mockInstance ) . presign . mockReturnValue ( mockPresigned as any ) ;
83
83
const options : RequestPresigningArguments = {
84
84
signingRegion : "aws-foo-1" ,
85
85
signingService : "bar" ,
@@ -95,12 +95,12 @@ describe("getSignedUrl", () => {
95
95
VoiceId : "Kimberly" ,
96
96
} ) ;
97
97
await getSignedUrl ( client , command , options ) ;
98
- expect ( mockPresign ) . toBeCalled ( ) ;
99
- expect ( mockPresign . mock . calls [ 0 ] [ 1 ] ) . toMatchObject ( options ) ;
98
+ expect ( vi . mocked ( mockInstance ) . presign ) . toBeCalled ( ) ;
99
+ expect ( vi . mocked ( mockInstance ) . presign . mock . calls [ 0 ] [ 1 ] ) . toMatchObject ( options ) ;
100
100
} ) ;
101
101
it ( "should not throw if called concurrently" , async ( ) => {
102
102
const mockPresigned = "a presigned url" ;
103
- mockPresign . mockReturnValue ( mockPresigned ) ;
103
+ vi . mocked ( mockInstance ) . presign . mockReturnValue ( mockPresigned as any ) ;
104
104
const client = new PollyClient ( clientParams ) ;
105
105
const command = new SynthesizeSpeechCommand ( {
106
106
Text : "hello world, this is alex" ,
@@ -110,6 +110,6 @@ describe("getSignedUrl", () => {
110
110
const result = await Promise . all ( [ getSignedUrl ( client , command ) , getSignedUrl ( client , command ) ] ) ;
111
111
expect ( result ) . toBeInstanceOf ( Array ) ;
112
112
expect ( result ) . toHaveLength ( 2 ) ;
113
- expect ( mockPresign ) . toHaveBeenCalledTimes ( 2 ) ;
113
+ expect ( vi . mocked ( mockInstance ) . presign ) . toHaveBeenCalledTimes ( 2 ) ;
114
114
} ) ;
115
115
} ) ;
0 commit comments