Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(packages): convert to vitest #6588

Merged
merged 2 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 0 additions & 30 deletions lib/lib-storage/karma.conf.js

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"lint:versions": "node scripts/runtime-dependency-version-check/runtime-dep-version-check.js",
"lint:dependencies": "node scripts/runtime-dependency-version-check/check-dependencies.js",
"local-publish": "node ./scripts/verdaccio-publish/index.js",
"test:all": "yarn build:all && jest --coverage --passWithNoTests && lerna run test --scope '@aws-sdk/{fetch-http-handler,hash-blob-browser}' && yarn test:versions && yarn test:integration",
"test:all": "yarn build:all && jest --passWithNoTests && lerna run test --scope '@aws-sdk/{fetch-http-handler,hash-blob-browser}' && yarn test:versions && yarn test:integration",
"test:ci": "lerna run test --since origin/main",
"test:e2e": "node ./scripts/turbo test:e2e && node ./tests/canary/canary",
"test:e2e:legacy": "cucumber-js --fail-fast",
Expand Down
5 changes: 0 additions & 5 deletions packages/middleware-eventstream/jest.config.js

This file was deleted.

5 changes: 0 additions & 5 deletions packages/middleware-sdk-sts/jest.config.js

This file was deleted.

5 changes: 0 additions & 5 deletions packages/polly-request-presigner/jest.config.js

This file was deleted.

3 changes: 2 additions & 1 deletion packages/polly-request-presigner/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"build:types:downlevel": "downlevel-dts dist-types dist-types/ts3.4",
"clean": "rimraf ./dist-* && rimraf *.tsbuildinfo",
"extract:docs": "api-extractor run --local",
"test": "jest"
"test": "vitest run",
"test:watch": "vitest watch"
},
"main": "./dist-cjs/index.js",
"module": "./dist-es/index.js",
Expand Down
44 changes: 22 additions & 22 deletions packages/polly-request-presigner/src/getSignedUrls.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
const mockV4Sign = jest.fn();
const mockPresign = jest.fn();
const mockV4 = jest.fn().mockReturnValue({
presign: mockPresign,
sign: mockV4Sign,
});
jest.mock("@smithy/signature-v4", () => ({
SignatureV4: mockV4,
}));
import { beforeEach, describe, expect, test as it, vi } from "vitest";

vi.mock("@smithy/signature-v4", () => ({
SignatureV4: vi.fn().mockReturnValue({
presign: vi.fn(),
sign: vi.fn(),
}),
}));
import { PollyClient, SynthesizeSpeechCommand } from "@aws-sdk/client-polly";
import { SignatureV4 } from "@smithy/signature-v4";

jest.mock("@aws-sdk/util-format-url", () => ({
vi.mock("@aws-sdk/util-format-url", () => ({
formatUrl: (url: any) => url,
}));

Expand All @@ -19,6 +18,7 @@ import { AwsCredentialIdentity, RequestPresigningArguments } from "@smithy/types
import { getSignedUrl } from "./getSignedUrls";

describe("getSignedUrl", () => {
const mockInstance = new SignatureV4({} as any);
const credentials: AwsCredentialIdentity = {
secretAccessKey: "unit-test",
accessKeyId: "unit-test",
Expand All @@ -27,12 +27,12 @@ describe("getSignedUrl", () => {
const clientParams = { region: "us-foo-1", credentials };

beforeEach(() => {
mockPresign.mockReset();
vi.mocked(mockInstance).presign.mockReset();
});

it("should call SignatureV4.sign", async () => {
const mockPresigned = "a presigned url";
mockPresign.mockReturnValue(mockPresigned);
vi.mocked(mockInstance).presign.mockReturnValue(mockPresigned as any);
const client = new PollyClient(clientParams);
const command = new SynthesizeSpeechCommand({
Text: "hello world, this is alex",
Expand All @@ -41,15 +41,15 @@ describe("getSignedUrl", () => {
});
const signed = await getSignedUrl(client, command);
expect(signed).toBe(mockPresigned);
expect(mockPresign).toBeCalled();
expect(mockV4Sign).not.toBeCalled();
expect(vi.mocked(mockInstance).presign).toBeCalled();
expect(vi.mocked(mockInstance).sign).not.toBeCalled();
expect(client.middlewareStack.remove("presignInterceptMiddleware")).toBe(false);
expect(command.middlewareStack.remove("presignInterceptMiddleware")).toBe(false);
});

it("should presign with signing region and service in context if exists", async () => {
const mockPresigned = "a presigned url";
mockPresign.mockReturnValue(mockPresigned);
vi.mocked(mockInstance).presign.mockReturnValue(mockPresigned as any);
const signingRegion = "aws-foo-1";
const signingService = "bar";
const client = new PollyClient(clientParams);
Expand All @@ -70,16 +70,16 @@ describe("getSignedUrl", () => {
VoiceId: "Kimberly",
});
await getSignedUrl(client, command);
expect(mockPresign).toBeCalled();
expect(mockPresign.mock.calls[0][1]).toMatchObject({
expect(vi.mocked(mockInstance).presign).toBeCalled();
expect(vi.mocked(mockInstance).presign.mock.calls[0][1]).toMatchObject({
signingRegion,
signingService,
});
});

it("should presign with parameters from presign options if set", async () => {
const mockPresigned = "a presigned url";
mockPresign.mockReturnValue(mockPresigned);
vi.mocked(mockInstance).presign.mockReturnValue(mockPresigned as any);
const options: RequestPresigningArguments = {
signingRegion: "aws-foo-1",
signingService: "bar",
Expand All @@ -95,12 +95,12 @@ describe("getSignedUrl", () => {
VoiceId: "Kimberly",
});
await getSignedUrl(client, command, options);
expect(mockPresign).toBeCalled();
expect(mockPresign.mock.calls[0][1]).toMatchObject(options);
expect(vi.mocked(mockInstance).presign).toBeCalled();
expect(vi.mocked(mockInstance).presign.mock.calls[0][1]).toMatchObject(options);
});
it("should not throw if called concurrently", async () => {
const mockPresigned = "a presigned url";
mockPresign.mockReturnValue(mockPresigned);
vi.mocked(mockInstance).presign.mockReturnValue(mockPresigned as any);
const client = new PollyClient(clientParams);
const command = new SynthesizeSpeechCommand({
Text: "hello world, this is alex",
Expand All @@ -110,6 +110,6 @@ describe("getSignedUrl", () => {
const result = await Promise.all([getSignedUrl(client, command), getSignedUrl(client, command)]);
expect(result).toBeInstanceOf(Array);
expect(result).toHaveLength(2);
expect(mockPresign).toHaveBeenCalledTimes(2);
expect(vi.mocked(mockInstance).presign).toHaveBeenCalledTimes(2);
});
});
9 changes: 9 additions & 0 deletions packages/polly-request-presigner/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { defineConfig } from "vitest/config";

export default defineConfig({
test: {
exclude: ["**/*.{integ,e2e,browser}.spec.ts"],
include: ["**/*.spec.ts"],
environment: "node",
},
});
5 changes: 0 additions & 5 deletions packages/rds-signer/jest.config.js

This file was deleted.

3 changes: 2 additions & 1 deletion packages/rds-signer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"build:types:downlevel": "downlevel-dts dist-types dist-types/ts3.4",
"clean": "rimraf ./dist-* && rimraf *.tsbuildinfo",
"extract:docs": "api-extractor run --local",
"test": "jest"
"test": "vitest run",
"test:watch": "vitest watch"
},
"engines": {
"node": ">=16.0.0"
Expand Down
23 changes: 12 additions & 11 deletions packages/rds-signer/src/Signer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@ import { formatUrl } from "@aws-sdk/util-format-url";
import { loadConfig } from "@smithy/node-config-provider";
import { HttpRequest } from "@smithy/protocol-http";
import { SignatureV4 } from "@smithy/signature-v4";
import { beforeAll, beforeEach, describe, expect, test as it, vi } from "vitest";

import { Signer, SignerConfig } from "./Signer";

const mockPresign = jest.fn();
jest.mock("@smithy/signature-v4", () => {
const mockPresign = vi.fn();
vi.mock("@smithy/signature-v4", () => {
return {
SignatureV4: jest.fn().mockImplementation(() => {
SignatureV4: vi.fn().mockImplementation(() => {
return { presign: mockPresign };
}),
};
});

jest.mock("@smithy/node-config-provider");
jest.mock("@smithy/config-resolver");
jest.mock("@aws-sdk/credential-providers");
jest.mock("@aws-sdk/util-format-url");
vi.mock("@smithy/node-config-provider");
vi.mock("@smithy/config-resolver");
vi.mock("@aws-sdk/credential-providers");
vi.mock("@aws-sdk/util-format-url");

describe("rds-signer", () => {
const minimalParams: SignerConfig = {
Expand All @@ -28,20 +29,20 @@ describe("rds-signer", () => {
};

beforeAll(() => {
(formatUrl as jest.Mock).mockReturnValue("https://testhost:5432?other=url&parameters=here");
vi.mocked(formatUrl).mockReturnValue("https://testhost:5432?other=url&parameters=here");
});

beforeEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});

it("should provide correct parameters to the SigV4 signer", async () => {
const credentials = {
accessKeyId: "xyz",
secretAccessKey: "123",
};
(loadConfig as jest.Mock).mockReturnValue(async () => "us-foo-1");
(fromNodeProviderChain as jest.Mock).mockReturnValue(async () => credentials);
vi.mocked(loadConfig).mockReturnValue(async () => "us-foo-1");
vi.mocked(fromNodeProviderChain).mockReturnValue(async () => credentials);
const signer = new Signer(minimalParams);
await signer.getAuthToken();
//@ts-ignore
Expand Down
2 changes: 2 additions & 0 deletions packages/rds-signer/src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { describe, expect, test as it } from "vitest";

import * as rdsSigner from "./index";

describe("rds-signer", () => {
Expand Down
2 changes: 2 additions & 0 deletions packages/rds-signer/src/runtimeConfig.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { describe, expect, test as it } from "vitest";

import { getRuntimeConfig as getBrowserRuntimeConfig } from "./runtimeConfig.browser";
import { getRuntimeConfig as getRnRuntimeConfig } from "./runtimeConfig.native";
import { SignerConfig } from "./Signer";
Expand Down
9 changes: 9 additions & 0 deletions packages/rds-signer/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { defineConfig } from "vitest/config";

export default defineConfig({
test: {
exclude: ["**/*.{integ,e2e,browser}.spec.ts"],
include: ["**/*.spec.ts"],
environment: "node",
},
});
5 changes: 0 additions & 5 deletions packages/region-config-resolver/jest.config.js

This file was deleted.

3 changes: 2 additions & 1 deletion packages/region-config-resolver/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"build:types:downlevel": "downlevel-dts dist-types dist-types/ts3.4",
"clean": "rimraf ./dist-* && rimraf *.tsbuildinfo",
"extract:docs": "api-extractor run --local",
"test": "jest"
"test": "vitest run",
"test:watch": "vitest watch"
},
"main": "./dist-cjs/index.js",
"module": "./dist-es/index.js",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { describe, expect, test as it } from "vitest";

import {
NODE_REGION_CONFIG_FILE_OPTIONS,
NODE_REGION_CONFIG_OPTIONS,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import { afterEach, beforeEach, describe, expect, test as it, vi } from "vitest";

import { getRealRegion } from "./getRealRegion";
import { isFipsRegion } from "./isFipsRegion";

jest.mock("./isFipsRegion");
vi.mock("./isFipsRegion");

describe(getRealRegion.name, () => {
beforeEach(() => {
(isFipsRegion as jest.Mock).mockReturnValue(true);
vi.mocked(isFipsRegion).mockReturnValue(true);
});

afterEach(() => {
expect(isFipsRegion).toHaveBeenCalledTimes(1);
jest.clearAllMocks();
vi.clearAllMocks();
});

it("returns provided region if it's not FIPS", () => {
const mockRegion = "mockRegion";
(isFipsRegion as jest.Mock).mockReturnValue(false);
vi.mocked(isFipsRegion).mockReturnValue(false);
expect(getRealRegion(mockRegion)).toStrictEqual(mockRegion);
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { describe, expect, test as it } from "vitest";

import { isFipsRegion } from "./isFipsRegion";

describe(isFipsRegion.name, () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
import { Provider } from "@smithy/types";
import { afterEach, beforeEach, describe, expect, test as it, vi } from "vitest";

import { getRealRegion } from "./getRealRegion";
import { isFipsRegion } from "./isFipsRegion";
import { resolveRegionConfig } from "./resolveRegionConfig";

jest.mock("./getRealRegion");
jest.mock("./isFipsRegion");
vi.mock("./getRealRegion");
vi.mock("./isFipsRegion");

describe("RegionConfig", () => {
const mockRegion = "mockRegion";
const mockRealRegion = "mockRealRegion";
const mockUseFipsEndpoint = () => Promise.resolve(false);

beforeEach(() => {
(getRealRegion as jest.Mock).mockReturnValue(mockRealRegion);
(isFipsRegion as jest.Mock).mockReturnValue(false);
vi.mocked(getRealRegion).mockReturnValue(mockRealRegion);
vi.mocked(isFipsRegion).mockReturnValue(false);
});

afterEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});

describe("region", () => {
Expand Down Expand Up @@ -51,8 +52,8 @@ describe("RegionConfig", () => {
let mockUseFipsEndpoint: boolean | Provider<boolean>;

beforeEach(() => {
mockRegionProvider = jest.fn().mockResolvedValueOnce(Promise.resolve(mockRegion));
mockUseFipsEndpoint = jest.fn().mockResolvedValueOnce(Promise.resolve(false));
mockRegionProvider = vi.fn().mockResolvedValueOnce(Promise.resolve(mockRegion));
mockUseFipsEndpoint = vi.fn().mockResolvedValueOnce(Promise.resolve(false));
});

afterEach(() => {
Expand All @@ -70,7 +71,7 @@ describe("RegionConfig", () => {
});

it("returns Provider which returns true for FIPS endpoints", async () => {
(isFipsRegion as jest.Mock).mockReturnValue(true);
vi.mocked(isFipsRegion).mockReturnValue(true);
const resolvedRegionConfig = resolveRegionConfig({
region: mockRegionProvider,
useFipsEndpoint: mockUseFipsEndpoint,
Expand Down
Loading
Loading