Skip to content

Commit

Permalink
test: update vitest to 2.1.8 (#6780)
Browse files Browse the repository at this point in the history
* test: update vitest to 2.1.8

* test: compatibility with vitest v2

* test: s3 debugging

* test(client-s3): browser compat
  • Loading branch information
kuhe authored Jan 8, 2025
1 parent 2cc72eb commit 6858c7e
Show file tree
Hide file tree
Showing 10 changed files with 765 additions and 127 deletions.
4 changes: 2 additions & 2 deletions clients/client-s3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
"extract:docs": "api-extractor run --local",
"generate:client": "node ../../scripts/generate-clients/single-service --solo s3",
"test": "yarn g:vitest run",
"test:browser": "node ./test/browser-build/esbuild && vitest run -c vitest.config.browser.ts --mode development",
"test:browser": "node ./test/browser-build/esbuild && yarn g:vitest run -c vitest.config.browser.ts",
"test:browser:watch": "node ./test/browser-build/esbuild && yarn g:vitest watch -c vitest.config.browser.ts",
"test:e2e": "yarn g:vitest run -c vitest.config.e2e.ts --mode development && yarn test:browser",
"test:e2e": "yarn g:vitest run -c vitest.config.e2e.ts && yarn test:browser",
"test:e2e:watch": "yarn g:vitest watch -c vitest.config.e2e.ts",
"test:watch": "yarn g:vitest watch"
},
Expand Down
45 changes: 36 additions & 9 deletions clients/client-s3/test/e2e/S3.browser.e2e.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { S3, SelectObjectContentEventStream, waitUntilObjectExists } from "@aws-sdk/client-s3";
import { fromNodeProviderChain } from "@aws-sdk/credential-providers";
import { FetchHttpHandler } from "@smithy/fetch-http-handler";
import { Browser } from "happy-dom";
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, onTestFailed, test as it } from "vitest";

import { getIntegTestResources } from "../../../../tests/e2e/get-integ-test-resources";
Expand All @@ -21,18 +22,26 @@ describe("@aws-sdk/client-s3", () => {
};

beforeAll(async () => {
const browser = new Browser();
browser.settings.fetch.disableSameOriginPolicy = true;

const integTestResourcesEnv = await getIntegTestResources();
Object.assign(process.env, integTestResourcesEnv);

region = process?.env?.AWS_SMOKE_TEST_REGION as string;
Bucket = process?.env?.AWS_SMOKE_TEST_BUCKET as string;
mrapArn = (globalThis as any)?.window?.__env__?.AWS_SMOKE_TEST_MRAP_ARN || process?.env?.AWS_SMOKE_TEST_MRAP_ARN;

const provider = fromNodeProviderChain();
const credentials = await provider();

client = new S3Impl(
getRuntimeConfig({
region,
credentials: fromNodeProviderChain(),
requestHandler: new FetchHttpHandler(),
credentials,
requestHandler: FetchHttpHandler.create({
credentials: "include",
}),
logger: {
trace() {},
debug() {},
Expand All @@ -44,6 +53,24 @@ describe("@aws-sdk/client-s3", () => {
},
})
) as unknown as S3;

client.middlewareStack.addRelativeTo(
(next: any) => async (args: any) => {
const result = await next(args);
const { response } = result;
for (const [key, value] of Object.entries(response.headers)) {
delete response.headers[key];
response.headers[String(key).toLowerCase()] = value;
}
return result;
},
{
toMiddleware: "deserializerMiddleware",
name: "header-casing-middleware",
override: true,
relation: "after",
}
);
});

afterAll(() => {
Expand All @@ -66,10 +93,7 @@ describe("@aws-sdk/client-s3", () => {

const buf = createBuffer("1KB");

// TODO(vitest)
// Caused by: RequestContentLengthMismatchError: Request body length does not match content-length header
// only in vitest + happy-dom.
it.skip("should succeed with blob body", async () => {
it("should succeed with blob body", async () => {
onTestFailed(setTestFailed);
const blob = new Blob([buf]);
const result = await client.putObject({
Expand All @@ -91,7 +115,10 @@ describe("@aws-sdk/client-s3", () => {
expect(result.$metadata.httpStatusCode).toEqual(200);
});

it("should succeed with ReadableStream body", async () => {
// TODO(vitest)
// Caused by: SignatureDoesNotMatch
// only in vitest + happy-dom.
it.skip("should succeed with ReadableStream body", async () => {
onTestFailed(setTestFailed);
const length = 10 * 1000; // 10KB
const chunkSize = 10;
Expand Down Expand Up @@ -170,9 +197,9 @@ describe("@aws-sdk/client-s3", () => {
expect(result.Contents).toBeInstanceOf(Array);
});

it("should throw with invalid bucket", () => {
it("should throw with invalid bucket", async () => {
onTestFailed(setTestFailed);
expect(() => client.listObjects({ Bucket: "invalid-bucket" })).rejects.toThrow();
await expect(() => client.listObjects({ Bucket: "invalid-bucket" })).rejects.toThrow();
});
});

Expand Down
1 change: 1 addition & 0 deletions clients/client-s3/vitest.config.browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export default defineConfig({
include: ["**/*.browser.e2e.spec.ts", "test/unit/**/*.spec.ts"],
environment: "happy-dom",
},
mode: "development",
});
1 change: 1 addition & 0 deletions clients/client-s3/vitest.config.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export default defineConfig({
include: ["**/*.e2e.spec.ts"],
environment: "node",
},
mode: "development",
});
7 changes: 2 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
"fs-extra": "^9.0.0",
"generate-changelog": "^1.7.1",
"glob": "7.1.6",
"happy-dom": "14.12.3",
"happy-dom": "16.3.0",
"husky": "^4.2.3",
"jest": "29.7.0",
"jmespath": "^0.15.0",
Expand All @@ -108,18 +108,15 @@
"turbo": "2.1.2",
"typescript": "~5.2.2",
"verdaccio": "5.25.0",
"vite": "4.5.5",
"vitest": "0.34.6",
"vitest": "2.1.8",
"webpack": "5.76.0",
"webpack-cli": "4.10.0",
"yargs": "17.5.1"
},
"overrides": {
"vite": "4.5.5",
"typescript": "~5.2.2"
},
"resolutions": {
"vite": "4.5.5",
"typescript": "~5.2.2"
},
"workspaces": {
Expand Down
8 changes: 4 additions & 4 deletions packages/credential-provider-ini/src/fromIni.integ.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ describe("fromIni region search order", () => {

await sts.getCallerIdentity({});
const credentials = await sts.config.credentials();
expect(credentials).toContain({
expect(credentials).toMatchObject({
accessKeyId: "STS_AR_ACCESS_KEY_ID",
secretAccessKey: "STS_AR_SECRET_ACCESS_KEY",
sessionToken: "STS_AR_SESSION_TOKEN_ap-northeast-1",
Expand All @@ -192,7 +192,7 @@ describe("fromIni region search order", () => {

await sts.getCallerIdentity({});
const credentials = await sts.config.credentials();
expect(credentials).toContain({
expect(credentials).toMatchObject({
accessKeyId: "STS_AR_ACCESS_KEY_ID",
secretAccessKey: "STS_AR_SECRET_ACCESS_KEY",
sessionToken: "STS_AR_SESSION_TOKEN_us-stsar-1",
Expand All @@ -214,7 +214,7 @@ describe("fromIni region search order", () => {

await sts.getCallerIdentity({});
const credentials = await sts.config.credentials();
expect(credentials).toContain({
expect(credentials).toMatchObject({
accessKeyId: "STS_AR_ACCESS_KEY_ID",
secretAccessKey: "STS_AR_SECRET_ACCESS_KEY",
sessionToken: "STS_AR_SESSION_TOKEN_ap-northeast-2",
Expand All @@ -238,7 +238,7 @@ describe("fromIni region search order", () => {

await sts.getCallerIdentity({});
const credentials = await sts.config.credentials();
expect(credentials).toContain({
expect(credentials).toMatchObject({
accessKeyId: "STS_AR_ACCESS_KEY_ID",
secretAccessKey: "STS_AR_SECRET_ACCESS_KEY",
sessionToken: "STS_AR_SESSION_TOKEN_us-east-1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ describe("bucketHostname", () => {
clientRegion: region,
isCustomEndpoint: false,
})
).toThrow("");
).toThrow();
});

it('should populate endpoint from MRAP ARN with access point name "myendpoint"', () => {
Expand Down
8 changes: 4 additions & 4 deletions packages/s3-presigned-post/src/createPresignedPost.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ describe("createPresignedPost", () => {
Bucket,
Key: "path/to/${filename}",
});
const { conditions } = JSON.parse(mockS3Client.config.utf8Decoder.mock.calls[0]);
const { conditions } = JSON.parse(mockS3Client.config.utf8Decoder.mock.calls[0] as any);
expect(conditions).toContainEqual(["starts-with", "$key", "path/to/"]);
});

Expand All @@ -112,7 +112,7 @@ describe("createPresignedPost", () => {
Bucket,
Key,
});
const policy = JSON.parse(mockS3Client.config.utf8Decoder.mock.calls[0]);
const policy = JSON.parse(mockS3Client.config.utf8Decoder.mock.calls[0] as any);
expect(policy).toMatchObject({
expiration: "2020-10-28T23:56:49Z",
});
Expand All @@ -125,7 +125,7 @@ describe("createPresignedPost", () => {
Key,
Expires: 7200,
});
expect(JSON.parse(mockS3Client.config.utf8Decoder.mock.calls[0])).toMatchObject({
expect(JSON.parse(mockS3Client.config.utf8Decoder.mock.calls[0] as any)).toMatchObject({
expiration: "2020-10-29T00:56:49Z",
});
});
Expand All @@ -139,7 +139,7 @@ describe("createPresignedPost", () => {
Fields: { acl: "public-read" },
});
expect(fields).toMatchObject({ bucket: Bucket, key: Key, acl: "public-read" });
const { conditions } = JSON.parse(mockS3Client.config.utf8Decoder.mock.calls[0]);
const { conditions } = JSON.parse(mockS3Client.config.utf8Decoder.mock.calls[0] as any);
expect(conditions).toContainEqual({ acl: "public-read" });
});
});
16 changes: 16 additions & 0 deletions tests/e2e/get-integ-test-resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { join } = require("path");
const { STSClient, GetCallerIdentityCommand } = require("@aws-sdk/client-sts");
const { CloudFormationClient, DescribeStackResourcesCommand } = require("@aws-sdk/client-cloudformation");
const { S3ControlClient, ListMultiRegionAccessPointsCommand } = require("@aws-sdk/client-s3-control");
const { S3 } = require("@aws-sdk/client-s3");
const { ensureTestStack } = require("./ensure-test-stack");
const { deleteStaleChangesets } = require("./delete-stale-changesets");

Expand Down Expand Up @@ -42,6 +43,21 @@ exports.getIntegTestResources = async () => {
const { Alias } = AccessPoints.find((accesspoint) => accesspoint.Name === multiRegionAccessPointName);
const mrapArn = `arn:aws:s3::${AccountId}:accesspoint/${Alias}`;

const s3 = new S3({ region });
await s3.putBucketCors({
Bucket: bucketName,
CORSConfiguration: {
CORSRules: [
{
AllowedOrigins: ["*"],
AllowedMethods: ["GET", "PUT", "POST", "DELETE", "HEAD"],
AllowedHeaders: ["*"],
ExposeHeaders: ["ETag"],
},
],
},
});

return {
AWS_SMOKE_TEST_REGION: region,
AWS_SMOKE_TEST_IDENTITY_POOL_ID: identityPoolId,
Expand Down
Loading

0 comments on commit 6858c7e

Please sign in to comment.