Skip to content

Commit 42121bd

Browse files
committed
test(signature-v4-multi-region): move testing to vitest
1 parent aa227cc commit 42121bd

12 files changed

+881
-62
lines changed

packages/signature-v4-multi-region/jest.config.e2e.js

Lines changed: 0 additions & 4 deletions
This file was deleted.

packages/signature-v4-multi-region/jest.config.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

packages/signature-v4-multi-region/package.json

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
"build:types": "tsc -p tsconfig.types.json",
1010
"build:types:downlevel": "downlevel-dts dist-types dist-types/ts3.4",
1111
"clean": "rimraf ./dist-* && rimraf *.tsbuildinfo",
12-
"test": "jest",
13-
"test:e2e": "jest -c jest.config.e2e.js",
14-
"test:browser": "karma start karma.conf.js"
12+
"test": "vitest run",
13+
"test:e2e": "vitest run --config vitest.config.e2e.js",
14+
"test:browser": "vitest run --config vitest.config.browser.js"
1515
},
1616
"main": "./dist-cjs/index.js",
1717
"module": "./dist-es/index.js",
@@ -34,7 +34,12 @@
3434
"concurrently": "7.0.0",
3535
"downlevel-dts": "0.10.1",
3636
"rimraf": "3.0.2",
37-
"typescript": "~4.9.5"
37+
"typescript": "~4.9.5",
38+
"vitest": "^2.1.9",
39+
"@vitest/browser": "^1.0.0",
40+
"@vitest/coverage-v8": "^1.0.0",
41+
"chai": "^4.3.7",
42+
"chai-as-promised": "^7.1.1"
3843
},
3944
"engines": {
4045
"node": ">=16.0.0"

packages/signature-v4-multi-region/src/SignatureV4MultiRegion.spec.ts

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { HttpRequest } from "@smithy/protocol-http";
2+
import { beforeEach, describe, expect, it, vi } from "vitest";
23

3-
jest.mock("@smithy/signature-v4");
4-
jest.mock("@aws-sdk/signature-v4-crt");
5-
jest.mock("@smithy/signature-v4a");
4+
vi.mock("@smithy/signature-v4");
5+
vi.mock("@aws-sdk/signature-v4-crt");
6+
vi.mock("@smithy/signature-v4a");
67

78
import { CrtSignerV4 } from "@aws-sdk/signature-v4-crt";
89
import { SignatureV4 } from "@smithy/signature-v4";
@@ -31,49 +32,43 @@ describe("SignatureV4MultiRegion", () => {
3132
beforeEach(() => {
3233
signatureV4CrtContainer.CrtSignerV4 = CrtSignerV4 as any;
3334
signatureV4aContainer.SignatureV4a = SignatureV4a as any;
34-
jest.clearAllMocks();
35+
vi.clearAllMocks();
3536
});
3637

3738
it("should sign with SigV4 signer", async () => {
3839
const signer = new SignatureV4MultiRegion(params);
3940
await signer.sign(minimalRequest);
40-
//@ts-ignore
41-
expect(SignatureV4.mock.instances[0].sign).toBeCalledTimes(1);
41+
expect(SignatureV4.prototype.sign).toBeCalledTimes(1);
4242
});
4343

4444
it("should presign with SigV4 signer", async () => {
4545
const signer = new SignatureV4MultiRegion(params);
4646
await signer.presign(minimalRequest);
47-
//@ts-ignore
48-
expect(SignatureV4.mock.instances[0].presign).toBeCalledTimes(1);
47+
expect(SignatureV4.prototype.presign).toBeCalledTimes(1);
4948
});
5049

5150
it("should sign with SigV4a signer if mult_region option is set", async () => {
5251
const signer = new SignatureV4MultiRegion(params);
5352
await signer.presign(minimalRequest, { signingRegion: "*" });
54-
//@ts-ignore
55-
expect(CrtSignerV4.mock.instances[0].presign).toBeCalledTimes(1);
53+
expect(CrtSignerV4.prototype.presign).toBeCalledTimes(1);
5654
});
5755

5856
it("should presign with SigV4 signer", async () => {
5957
const signer = new SignatureV4MultiRegion(params);
6058
await signer.presign(minimalRequest, { signingRegion: "*" });
61-
//@ts-ignore
62-
expect(CrtSignerV4.mock.instances[0].presign).toBeCalledTimes(1);
59+
expect(CrtSignerV4.prototype.presign).toBeCalledTimes(1);
6360
});
6461

6562
it("should presign with SigV4a signer if signingRegion is '*'", async () => {
6663
const signer = new SignatureV4MultiRegion(params);
6764
await signer.presign(minimalRequest, { signingRegion: "*" });
68-
//@ts-ignore
69-
expect(SignatureV4a.mock.instances[0].presign).toBeCalledTimes(1);
65+
expect(SignatureV4a.prototype.presign).toBeCalledTimes(1);
7066
});
7167

7268
it("should sign with SigV4a signer if signingRegion is '*'", async () => {
7369
const signer = new SignatureV4MultiRegion(params);
7470
await signer.sign(minimalRequest, { signingRegion: "*" });
75-
//@ts-ignore
76-
expect(SignatureV4a.mock.instances[0].sign).toBeCalledTimes(1);
71+
expect(SignatureV4a.prototype.sign).toBeCalledTimes(1);
7772
});
7873

7974
it("should use CrtSignerV4 if available", async () => {

packages/signature-v4-multi-region/src/cloudfront-keyvaluestore-sigv4a.e2e.spec.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import { CloudFrontKeyValueStoreClient, DescribeKeyValueStoreCommand } from "@aw
66
import { GetCallerIdentityCommand, STSClient } from "@aws-sdk/client-sts";
77
import { SignatureV4MultiRegion } from "@aws-sdk/signature-v4-multi-region";
88
import { HttpRequest } from "@smithy/protocol-http";
9+
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
910

10-
jest.setTimeout(300000);
11+
// Setting timeout for tests
12+
const TEST_TIMEOUT = 300000;
1113

1214
describe("CloudFront KeyValue Store with SignatureV4a (JS Implementation)", () => {
1315
let cfClient: CloudFrontClient;
@@ -18,6 +20,9 @@ describe("CloudFront KeyValue Store with SignatureV4a (JS Implementation)", () =
1820
let signer: SignatureV4MultiRegion;
1921

2022
beforeAll(async () => {
23+
// Set timeout for setup
24+
vi.setConfig({ testTimeout: TEST_TIMEOUT });
25+
2126
const stsClient = new STSClient({});
2227
const { Account } = await stsClient.send(new GetCallerIdentityCommand({}));
2328
const timestamp = Date.now();
@@ -53,6 +58,9 @@ describe("CloudFront KeyValue Store with SignatureV4a (JS Implementation)", () =
5358
});
5459

5560
afterAll(async () => {
61+
// Set timeout for teardown
62+
vi.setConfig({ testTimeout: TEST_TIMEOUT });
63+
5664
try {
5765
await cfClient.send(
5866
new DeleteKeyValueStoreCommand({
@@ -76,7 +84,7 @@ describe("CloudFront KeyValue Store with SignatureV4a (JS Implementation)", () =
7684
path: "/",
7785
});
7886

79-
const signSpy = jest.spyOn(signer, "sign");
87+
const signSpy = vi.spyOn(signer, "sign");
8088

8189
await signer.sign(mockRequest, { signingRegion: "*" });
8290

packages/signature-v4-multi-region/src/eventbridge-sigv4a.e2e.spec.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,20 @@ import { DeleteRuleCommand, DescribeRuleCommand, EventBridgeClient, PutRuleComma
55
import { GetCallerIdentityCommand, STSClient } from "@aws-sdk/client-sts";
66
import { SignatureV4MultiRegion } from "@aws-sdk/signature-v4-multi-region";
77
import { HttpRequest } from "@smithy/protocol-http";
8+
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
89

9-
jest.setTimeout(300000);
10+
// Setting timeout for tests
11+
const TEST_TIMEOUT = 300000;
1012

1113
describe("EventBridge with SignatureV4a (JS Implementation)", () => {
1214
let ebClient: EventBridgeClient;
1315
let ruleName: string;
1416
let signer: SignatureV4MultiRegion;
1517

1618
beforeAll(async () => {
19+
// Set timeout for setup
20+
vi.setConfig({ testTimeout: TEST_TIMEOUT });
21+
1722
const stsClient = new STSClient({});
1823
const { Account } = await stsClient.send(new GetCallerIdentityCommand({}));
1924
const timestamp = Date.now();
@@ -37,6 +42,9 @@ describe("EventBridge with SignatureV4a (JS Implementation)", () => {
3742
});
3843

3944
afterAll(async () => {
45+
// Set timeout for teardown
46+
vi.setConfig({ testTimeout: TEST_TIMEOUT });
47+
4048
try {
4149
await ebClient.send(
4250
new DeleteRuleCommand({
@@ -59,7 +67,7 @@ describe("EventBridge with SignatureV4a (JS Implementation)", () => {
5967
path: "/",
6068
});
6169

62-
const signSpy = jest.spyOn(signer, "sign");
70+
const signSpy = vi.spyOn(signer, "sign");
6371

6472
await signer.sign(mockRequest, { signingRegion: "*" });
6573

packages/signature-v4-multi-region/src/s3-mrap-sigv4a.e2e.spec.ts

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,26 @@ import "@smithy/signature-v4a";
22

33
import { Sha256 } from "@aws-crypto/sha256-js";
44
import {
5-
S3Client,
65
CreateBucketCommand,
76
DeleteBucketCommand,
7+
ListObjectsV2Command,
88
PutObjectCommand,
9-
ListObjectsV2Command
9+
S3Client,
1010
} from "@aws-sdk/client-s3";
1111
import {
12-
S3ControlClient,
1312
CreateMultiRegionAccessPointCommand,
1413
DeleteMultiRegionAccessPointCommand,
1514
DescribeMultiRegionAccessPointOperationCommand,
16-
GetMultiRegionAccessPointCommand
15+
GetMultiRegionAccessPointCommand,
16+
S3ControlClient,
1717
} from "@aws-sdk/client-s3-control";
1818
import { GetCallerIdentityCommand, STSClient } from "@aws-sdk/client-sts";
1919
import { SignatureV4MultiRegion } from "@aws-sdk/signature-v4-multi-region";
2020
import { HttpRequest } from "@smithy/protocol-http";
21+
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
2122

22-
jest.setTimeout(1800000); // 30 minutes (MRAP operations can take a while)
23+
// Setting timeout for long-running tests
24+
const LONG_TIMEOUT = 1800000; // 30 minutes
2325

2426
describe("S3 Multi-Region Access Point with SignatureV4a (JS Implementation)", () => {
2527
let s3Client: S3Client;
@@ -32,6 +34,9 @@ describe("S3 Multi-Region Access Point with SignatureV4a (JS Implementation)", (
3234
let mrapArn: string;
3335

3436
beforeAll(async () => {
37+
// Set timeout for setup
38+
vi.setConfig({ testTimeout: LONG_TIMEOUT });
39+
3540
const stsClient = new STSClient({});
3641
const { Account } = await stsClient.send(new GetCallerIdentityCommand({}));
3742
accountId = Account!;
@@ -63,8 +68,12 @@ describe("S3 Multi-Region Access Point with SignatureV4a (JS Implementation)", (
6368
});
6469

6570
// Create buckets
66-
await s3Client.send(new CreateBucketCommand({ Bucket: bucketName1, CreateBucketConfiguration: { LocationConstraint: "us-west-2" } }));
67-
await s3Client.send(new CreateBucketCommand({ Bucket: bucketName2, CreateBucketConfiguration: { LocationConstraint: "us-east-2" } }));
71+
await s3Client.send(
72+
new CreateBucketCommand({ Bucket: bucketName1, CreateBucketConfiguration: { LocationConstraint: "us-west-2" } })
73+
);
74+
await s3Client.send(
75+
new CreateBucketCommand({ Bucket: bucketName2, CreateBucketConfiguration: { LocationConstraint: "us-east-2" } })
76+
);
6877

6978
// Create MRAP
7079
const createResponse = await s3ControlClient.send(
@@ -101,7 +110,7 @@ describe("S3 Multi-Region Access Point with SignatureV4a (JS Implementation)", (
101110
if (describeResponse.AsyncOperation?.RequestStatus === "SUCCESS") {
102111
mrapReady = true;
103112
} else {
104-
await new Promise(resolve => setTimeout(resolve, 30000)); // Wait for 30 seconds before retrying
113+
await new Promise((resolve) => setTimeout(resolve, 30000)); // Wait for 30 seconds before retrying
105114
retries++;
106115
}
107116
}
@@ -120,14 +129,19 @@ describe("S3 Multi-Region Access Point with SignatureV4a (JS Implementation)", (
120129
mrapArn = getResponse.AccessPoint!.Alias!;
121130

122131
// Upload a small file to one of the buckets
123-
await s3Client.send(new PutObjectCommand({
124-
Bucket: bucketName1,
125-
Key: "testfile",
126-
Body: Buffer.from("test", "utf-8")
127-
}));
132+
await s3Client.send(
133+
new PutObjectCommand({
134+
Bucket: bucketName1,
135+
Key: "testfile",
136+
Body: Buffer.from("test", "utf-8"),
137+
})
138+
);
128139
});
129140

130141
afterAll(async () => {
142+
// Set timeout for teardown
143+
vi.setConfig({ testTimeout: LONG_TIMEOUT });
144+
131145
// Delete MRAP
132146
try {
133147
await s3ControlClient.send(
@@ -163,7 +177,7 @@ describe("S3 Multi-Region Access Point with SignatureV4a (JS Implementation)", (
163177
path: "/",
164178
});
165179

166-
const signSpy = jest.spyOn(signer, "sign");
180+
const signSpy = vi.spyOn(signer, "sign");
167181

168182
await signer.sign(mockRequest, { signingRegion: "*" });
169183

@@ -188,6 +202,6 @@ describe("S3 Multi-Region Access Point with SignatureV4a (JS Implementation)", (
188202

189203
expect(response.Contents).toBeDefined();
190204
expect(response.Contents?.length).toBeGreaterThan(0);
191-
expect(response.Contents?.some(object => object.Key === "testfile")).toBe(true);
205+
expect(response.Contents?.some((object) => object.Key === "testfile")).toBe(true);
192206
});
193-
});
207+
});

packages/signature-v4-multi-region/src/sigv4a.browser.spec.ts

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import { SignatureV4MultiRegion } from "@aws-sdk/signature-v4-multi-region";
66
import { HttpRequest } from "@smithy/protocol-http";
77
import chai from "chai";
88
import chaiAsPromised from "chai-as-promised";
9+
import { describe, expect, it } from "vitest";
910

1011
chai.use(chaiAsPromised);
11-
const { expect } = chai;
12+
const { expect: chaiExpect } = chai;
1213

1314
const region = "*";
1415
const credentials = {
@@ -20,8 +21,9 @@ const credentials = {
2021
const s3MrapArn = (window as any).__env__.AWS_SMOKE_TEST_MRAP_ARN;
2122
const cfKvsArn = (window as any).__env__.AWS_SMOKE_TEST_CF_KVS_ARN;
2223

23-
describe("SignatureV4a Browser Tests", function () {
24-
this.timeout(30000); // Set timeout to 30 seconds
24+
describe("SignatureV4a Browser Tests", () => {
25+
// Set timeout to 30 seconds
26+
const TIMEOUT = 30000;
2527

2628
const signer = new SignatureV4MultiRegion({
2729
credentials,
@@ -38,13 +40,16 @@ describe("SignatureV4a Browser Tests", function () {
3840
});
3941

4042
it("should successfully list objects using MRAP", async () => {
43+
// Using longer timeout for this test
44+
vi.setConfig({ testTimeout: TIMEOUT });
45+
4146
const command = new ListObjectsV2Command({
4247
Bucket: s3MrapArn,
4348
});
4449

4550
const response = await s3Client.send(command);
46-
expect(response.$metadata.httpStatusCode).to.equal(200);
47-
expect(response.Contents).to.be.an("array");
51+
chaiExpect(response.$metadata.httpStatusCode).to.equal(200);
52+
chaiExpect(response.Contents).to.be.an("array");
4853
});
4954
});
5055

@@ -56,11 +61,14 @@ describe("SignatureV4a Browser Tests", function () {
5661
});
5762

5863
it("should successfully list rules using global endpoint", async () => {
64+
// Using longer timeout for this test
65+
vi.setConfig({ testTimeout: TIMEOUT });
66+
5967
const command = new ListRulesCommand({});
6068

6169
const response = await ebClient.send(command);
62-
expect(response.$metadata.httpStatusCode).to.equal(200);
63-
expect(response.Rules).to.be.an("array");
70+
chaiExpect(response.$metadata.httpStatusCode).to.equal(200);
71+
chaiExpect(response.Rules).to.be.an("array");
6472
});
6573
});
6674

@@ -72,18 +80,24 @@ describe("SignatureV4a Browser Tests", function () {
7280
});
7381

7482
it("should successfully describe a key-value store", async () => {
83+
// Using longer timeout for this test
84+
vi.setConfig({ testTimeout: TIMEOUT });
85+
7586
const command = new DescribeKeyValueStoreCommand({
7687
KvsARN: cfKvsArn,
7788
});
7889

7990
const response = await cfKvsClient.send(command);
80-
expect(response.$metadata.httpStatusCode).to.equal(200);
81-
expect(response.KvsARN).to.equal(cfKvsArn);
91+
chaiExpect(response.$metadata.httpStatusCode).to.equal(200);
92+
chaiExpect(response.KvsARN).to.equal(cfKvsArn);
8293
});
8394
});
8495

8596
describe("SignatureV4MultiRegion", () => {
8697
it("should use SignatureV4a for '*' region", async () => {
98+
// Using longer timeout for this test
99+
vi.setConfig({ testTimeout: TIMEOUT });
100+
87101
const mockRequest = new HttpRequest({
88102
method: "GET",
89103
protocol: "https:",
@@ -95,8 +109,8 @@ describe("SignatureV4a Browser Tests", function () {
95109
});
96110

97111
const signedRequest = await signer.sign(mockRequest, { signingRegion: "*" });
98-
expect(signedRequest.headers["x-amz-region-set"]).to.equal("*");
99-
expect(signedRequest.headers["authorization"]).to.include("AWS4-ECDSA-P256-SHA256");
112+
chaiExpect(signedRequest.headers["x-amz-region-set"]).to.equal("*");
113+
chaiExpect(signedRequest.headers["authorization"]).to.include("AWS4-ECDSA-P256-SHA256");
100114
});
101115
});
102116
});

0 commit comments

Comments
 (0)