Skip to content

Commit

Permalink
test: fixing unit tests in vitest
Browse files Browse the repository at this point in the history
  • Loading branch information
kuhe committed Oct 24, 2024
1 parent 2147c57 commit bec8c35
Show file tree
Hide file tree
Showing 36 changed files with 720 additions and 109 deletions.
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@ generate-protocol-tests:
yarn

test-protocols:
(cd ./private/smithy-rpcv2-cbor && npx vitest run --globals)
(cd ./private/smithy-rpcv2-cbor && npx vitest run --globals)

turbo-clean:
@read -p "Are you sure you want to delete your local cache? [y/N]: " ans && [ $${ans:-N} = y ]
@echo "\nDeleted cache folders: \n--------"
@find . -name '.turbo' -type d -prune -print -exec rm -rf '{}' + && echo '\n'
7 changes: 0 additions & 7 deletions jest.config.js

This file was deleted.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"extract:docs": "mkdir -p api-extractor-packages && turbo run extract:docs",
"release": "yarn changeset publish",
"build-test-packages": "./gradlew clean build && node ./scripts/build-generated-test-packages",
"g:jest": "cd $INIT_CWD && jest",
"g:tsc": "cd $INIT_CWD && tsc",
"g:vitest": "cd $INIT_CWD && vitest"
},
Expand Down
76 changes: 39 additions & 37 deletions packages/credential-provider-imds/src/fromInstanceMetadata.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe("fromInstanceMetadata", () => {

beforeEach(() => {
vi.mocked(staticStabilityProvider).mockImplementation((input) => input);
vi.mocked(getInstanceMetadataEndpoint).mockResolvedValue({ hostname });
vi.mocked(getInstanceMetadataEndpoint).mockResolvedValue({ hostname } as any);
(isImdsCredentials as unknown as any).mockReturnValue(true);
vi.mocked(providerConfigFromInit).mockReturnValue({
timeout: mockTimeout,
Expand All @@ -74,9 +74,9 @@ describe("fromInstanceMetadata", () => {

it("gets token and profile name to fetch credentials", async () => {
vi.mocked(httpRequest)
.mockResolvedValueOnce(mockToken)
.mockResolvedValueOnce(mockProfile)
.mockResolvedValueOnce(JSON.stringify(mockImdsCreds));
.mockResolvedValueOnce(mockToken as any)
.mockResolvedValueOnce(mockProfile as any)
.mockResolvedValueOnce(JSON.stringify(mockImdsCreds) as any);

vi.mocked(retry).mockImplementation((fn: any) => fn());
vi.mocked(fromImdsCredentials).mockReturnValue(mockCreds);
Expand All @@ -93,9 +93,9 @@ describe("fromInstanceMetadata", () => {

it("trims profile returned name from IMDS", async () => {
vi.mocked(httpRequest)
.mockResolvedValueOnce(mockToken)
.mockResolvedValueOnce(" " + mockProfile + " ")
.mockResolvedValueOnce(JSON.stringify(mockImdsCreds));
.mockResolvedValueOnce(mockToken as any)
.mockResolvedValueOnce((" " + mockProfile + " ") as any)
.mockResolvedValueOnce(JSON.stringify(mockImdsCreds) as any);

vi.mocked(retry).mockImplementation((fn: any) => fn());
vi.mocked(fromImdsCredentials).mockReturnValue(mockCreds);
Expand Down Expand Up @@ -135,9 +135,9 @@ describe("fromInstanceMetadata", () => {

it("throws CredentialsProviderError if credentials returned are incorrect", async () => {
vi.mocked(httpRequest)
.mockResolvedValueOnce(mockToken)
.mockResolvedValueOnce(mockProfile)
.mockResolvedValueOnce(JSON.stringify(mockImdsCreds));
.mockResolvedValueOnce(mockToken as any)
.mockResolvedValueOnce(mockProfile as any)
.mockResolvedValueOnce(JSON.stringify(mockImdsCreds) as any);

vi.mocked(retry).mockImplementation((fn: any) => fn());
(isImdsCredentials as unknown as any).mockReturnValueOnce(false);
Expand All @@ -154,7 +154,9 @@ describe("fromInstanceMetadata", () => {

it("throws Error if httpRequest for profile fails", async () => {
const mockError = new Error("profile not found");
vi.mocked(httpRequest).mockResolvedValueOnce(mockToken).mockRejectedValueOnce(mockError);
vi.mocked(httpRequest)
.mockResolvedValueOnce(mockToken as any)
.mockRejectedValueOnce(mockError);
vi.mocked(retry).mockImplementation((fn: any) => fn());

await expect(fromInstanceMetadata()()).rejects.toEqual(mockError);
Expand All @@ -165,8 +167,8 @@ describe("fromInstanceMetadata", () => {
it("throws Error if httpRequest for credentials fails", async () => {
const mockError = new Error("creds not found");
vi.mocked(httpRequest)
.mockResolvedValueOnce(mockToken)
.mockResolvedValueOnce(mockProfile)
.mockResolvedValueOnce(mockToken as any)
.mockResolvedValueOnce(mockProfile as any)
.mockRejectedValueOnce(mockError);
vi.mocked(retry).mockImplementation((fn: any) => fn());

Expand All @@ -178,9 +180,9 @@ describe("fromInstanceMetadata", () => {

it("throws SyntaxError if httpRequest returns unparseable creds", async () => {
vi.mocked(httpRequest)
.mockResolvedValueOnce(mockToken)
.mockResolvedValueOnce(mockProfile)
.mockResolvedValueOnce(".");
.mockResolvedValueOnce(mockToken as any)
.mockResolvedValueOnce(mockProfile as any)
.mockResolvedValueOnce("." as any);
vi.mocked(retry).mockImplementation((fn: any) => fn());

await expect(fromInstanceMetadata()()).rejects.toThrow("Unexpected token");
Expand All @@ -200,9 +202,9 @@ describe("fromInstanceMetadata", () => {

it("should call staticStabilityProvider with the credential loader", async () => {
vi.mocked(httpRequest)
.mockResolvedValueOnce(mockToken)
.mockResolvedValueOnce(mockProfile)
.mockResolvedValueOnce(JSON.stringify(mockImdsCreds));
.mockResolvedValueOnce(mockToken as any)
.mockResolvedValueOnce(mockProfile as any)
.mockResolvedValueOnce(JSON.stringify(mockImdsCreds) as any);

vi.mocked(retry).mockImplementation((fn: any) => fn());
vi.mocked(fromImdsCredentials).mockReturnValue(mockCreds);
Expand All @@ -222,10 +224,10 @@ describe("fromInstanceMetadata", () => {

vi.mocked(httpRequest)
.mockRejectedValueOnce(tokenError)
.mockResolvedValueOnce(mockProfile)
.mockResolvedValueOnce(JSON.stringify(mockImdsCreds))
.mockResolvedValueOnce(mockProfile)
.mockResolvedValueOnce(JSON.stringify(mockImdsCreds));
.mockResolvedValueOnce(mockProfile as any)
.mockResolvedValueOnce(JSON.stringify(mockImdsCreds) as any)
.mockResolvedValueOnce(mockProfile as any)
.mockResolvedValueOnce(JSON.stringify(mockImdsCreds) as any);

const fromInstanceMetadataFunc = fromInstanceMetadata();
await expect(fromInstanceMetadataFunc()).resolves.toEqual(mockCreds);
Expand All @@ -238,10 +240,10 @@ describe("fromInstanceMetadata", () => {

vi.mocked(httpRequest)
.mockRejectedValueOnce(tokenError)
.mockResolvedValueOnce(mockProfile)
.mockResolvedValueOnce(JSON.stringify(mockImdsCreds))
.mockResolvedValueOnce(mockProfile)
.mockResolvedValueOnce(JSON.stringify(mockImdsCreds));
.mockResolvedValueOnce(mockProfile as any)
.mockResolvedValueOnce(JSON.stringify(mockImdsCreds) as any)
.mockResolvedValueOnce(mockProfile as any)
.mockResolvedValueOnce(JSON.stringify(mockImdsCreds) as any);

const fromInstanceMetadataFunc = fromInstanceMetadata();
await expect(fromInstanceMetadataFunc()).resolves.toEqual(mockCreds);
Expand All @@ -255,11 +257,11 @@ describe("fromInstanceMetadata", () => {

vi.mocked(httpRequest)
.mockRejectedValueOnce(tokenError)
.mockResolvedValueOnce(mockProfile)
.mockResolvedValueOnce(JSON.stringify(mockImdsCreds))
.mockResolvedValueOnce(mockToken)
.mockResolvedValueOnce(mockProfile)
.mockResolvedValueOnce(JSON.stringify(mockImdsCreds));
.mockResolvedValueOnce(mockProfile as any)
.mockResolvedValueOnce(JSON.stringify(mockImdsCreds) as any)
.mockResolvedValueOnce(mockToken as any)
.mockResolvedValueOnce(mockProfile as any)
.mockResolvedValueOnce(JSON.stringify(mockImdsCreds) as any);

vi.mocked(retry).mockImplementation((fn: any) => fn());
vi.mocked(fromImdsCredentials).mockReturnValue(mockCreds);
Expand All @@ -274,11 +276,11 @@ describe("fromInstanceMetadata", () => {

vi.mocked(httpRequest)
.mockRejectedValueOnce(tokenError)
.mockResolvedValueOnce(mockProfile)
.mockResolvedValueOnce(JSON.stringify(mockImdsCreds))
.mockResolvedValueOnce(mockToken)
.mockResolvedValueOnce(mockProfile)
.mockResolvedValueOnce(JSON.stringify(mockImdsCreds));
.mockResolvedValueOnce(mockProfile as any)
.mockResolvedValueOnce(JSON.stringify(mockImdsCreds) as any)
.mockResolvedValueOnce(mockToken as any)
.mockResolvedValueOnce(mockProfile as any)
.mockResolvedValueOnce(JSON.stringify(mockImdsCreds) as any);

vi.mocked(retry).mockImplementation((fn: any) => fn());
vi.mocked(fromImdsCredentials).mockReturnValue(mockCreds);
Expand Down
8 changes: 4 additions & 4 deletions packages/experimental-identity-and-auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
"clean": "rimraf ./dist-* && rimraf *.tsbuildinfo || exit 0",
"lint": "eslint -c ../../.eslintrc.js \"src/**/*.ts\"",
"format": "prettier --config ../../prettier.config.js --ignore-path ../.prettierignore --write \"**/*.{ts,md,json}\"",
"test": "vitest run",
"test:integration": "vitest run -c vitest.config.integ.ts",
"test:watch": "vitest watch",
"test:integration:watch": "vitest watch -c vitest.config.integ.ts"
"test": "yarn g:vitest run --passWithNoTests",
"test:integration": "yarn g:vitest run -c vitest.config.integ.ts",
"test:watch": "yarn g:vitest watch --passWithNoTests",
"test:integration:watch": "yarn g:vitest watch -c vitest.config.integ.ts"
},
"main": "./dist-cjs/index.js",
"module": "./dist-es/index.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import {
OnlyHttpApiKeyAuthOptionalCommand,
SameAsServiceCommand,
} from "@smithy/identity-and-auth-http-api-key-auth-service";
import { requireRequestsFrom } from "@smithy/util-test";
import { describe, expect,test as it } from "vitest";

import { requireRequestsFrom } from "../../../../private/util-test/src/index";

describe("@httpApiKeyAuth integration tests", () => {
// Match `HttpApiKeyAuthService` `@httpApiKeyAuth` trait
const MOCK_API_KEY_NAME = "Authorization";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import {
OnlyHttpBearerAuthOptionalCommand,
SameAsServiceCommand,
} from "@smithy/identity-and-auth-http-bearer-auth-service";
import { requireRequestsFrom } from "@smithy/util-test";
import { describe, expect,test as it } from "vitest";

import { requireRequestsFrom } from "../../../../private/util-test/src/index";

describe("@httpBearerAuth integration tests", () => {
// Arbitrary mock token
const MOCK_TOKEN = "TOKEN_123";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { afterEach, describe, expect, test as it, vi } from "vitest";

import { FetchHttpHandler } from "./fetch-http-handler";

// TODO(vitest): fix this test.
describe.skip(FetchHttpHandler.name, () => {
interface MockHttpRequestOptions {
method?: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { requireRequestsFrom } from "@smithy/util-test";
import { requireRequestsFrom } from "../../../private/util-test/src/index";
import { describe, expect, test as it } from "vitest";
import { Weather } from "weather";

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

// @jest-environment jsdom
import { AsyncGzip } from "fflate";
import { ReadableStream } from "web-streams-polyfill";

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

// @jest-environment jsdom
import { toUint8Array } from "@smithy/util-utf8";
import { gzip } from "fflate";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { requireRequestsFrom } from "@smithy/util-test";
import { describe, expect, test as it } from "vitest";
import { Weather } from "weather";

import { requireRequestsFrom } from "../../../private/util-test/src/index";

describe("middleware-content-length", () => {
describe(Weather.name, () => {
it("should not add content-length if no body", async () => {
Expand Down Expand Up @@ -41,4 +42,4 @@ describe("middleware-content-length", () => {
expect.assertions(1);
});
});
});
});
4 changes: 2 additions & 2 deletions packages/middleware-retry/src/StandardRetryStrategy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ vi.mock("@smithy/protocol-http");
vi.mock("uuid");

describe("defaultStrategy", () => {
let next: vi.Mock; // variable for next mock function in utility methods
let next: any; // variable for next mock function in utility methods
const maxAttempts = 3;

const mockDefaultRetryQuota = {
Expand Down Expand Up @@ -76,7 +76,7 @@ describe("defaultStrategy", () => {
output: { $metadata: {} },
};

next = jest
next = vi
.fn()
.mockRejectedValueOnce(mockError)
.mockRejectedValueOnce(mockError)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { requireRequestsFrom } from "@smithy/util-test";
import { requireRequestsFrom } from "../../../private/util-test/src/index";
import { describe, expect,test as it } from "vitest";
import { Weather } from "weather";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { requireRequestsFrom } from "@smithy/util-test";
import { requireRequestsFrom } from "../../../private/util-test/src/index";
import { describe, test as it } from "vitest";
import { Weather } from "weather";

Expand Down
2 changes: 1 addition & 1 deletion packages/node-config-provider/src/fromEnv.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe("fromEnv", () => {
});

describe("CredentialsProviderError", () => {
it("is behaving as expected cross-package in jest", () => {
it("is behaving as expected cross-package in vitest", () => {
expect(new CredentialsProviderError("msg", {}).message).toEqual("msg");
expect(new CredentialsProviderError("msg", {}).name).toEqual("CredentialsProviderError");
});
Expand Down
Loading

0 comments on commit bec8c35

Please sign in to comment.