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 cdd09c3
Show file tree
Hide file tree
Showing 150 changed files with 909 additions and 298 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
2 changes: 1 addition & 1 deletion packages/abort-controller/src/AbortController.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect,test as it } from "vitest";
import { describe, expect, test as it } from "vitest";

import { AbortController } from "./AbortController";
import { AbortSignal } from "./AbortSignal";
Expand Down
2 changes: 1 addition & 1 deletion packages/abort-controller/src/AbortSignal.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect,test as it, vi } from "vitest";
import { describe, expect, test as it, vi } from "vitest";

import { AbortController } from "./AbortController";

Expand Down
2 changes: 1 addition & 1 deletion packages/chunked-blob-reader-native/src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect,test as it } from "vitest";
import { describe, expect, test as it } from "vitest";

import { blobReader } from "./index";

Expand Down
2 changes: 1 addition & 1 deletion packages/chunked-blob-reader/src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Blob as BlobPolyfill } from "buffer";
import { describe, expect,test as it } from "vitest";
import { describe, expect, test as it } from "vitest";

import { blobReader } from "./index";

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/normalizeProvider.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect,test as it } from "vitest";
import { describe, expect, test as it } from "vitest";

import { normalizeProvider } from "./normalizeProvider";

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/pagination/createPaginator.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PaginationConfiguration } from "@smithy/types";
import { describe, expect,test as it } from "vitest";
import { describe, expect, test as it } from "vitest";

import { createPaginator } from "./createPaginator";

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/setFeature.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { HandlerExecutionContext } from "@smithy/types";
import { describe, expect,test as it } from "vitest";
import { describe, expect, test as it } from "vitest";

import { setFeature } from "./setFeature";

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/submodules/cbor/cbor.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as fs from "fs";
import JSONbig from "json-bigint";
import * as path from "path";
import { describe, expect,test as it } from "vitest";
import { describe, expect, test as it } from "vitest";

import { cbor } from "./cbor";
import { bytesToFloat16 } from "./cbor-decode";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Uint8ArrayBlobAdapter } from "@smithy/util-stream";
import { describe, expect,test as it } from "vitest";
import { describe, expect, test as it } from "vitest";

import { collectBody } from "./collect-stream-body";

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

import { extendedEncodeURIComponent } from "./extended-encode-uri-component";

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

import { requestBuilder } from "./requestBuilder";

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
2 changes: 1 addition & 1 deletion packages/eventstream-codec/src/EventStreamCodec.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { fromUtf8, toUtf8 } from "@smithy/util-utf8";
import { describe, expect,test as it } from "vitest";
import { describe, expect, test as it } from "vitest";

import { EventStreamCodec } from "./EventStreamCodec";
import { vectors } from "./TestVectors.fixture";
Expand Down
2 changes: 1 addition & 1 deletion packages/eventstream-codec/src/HeaderMarshaller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MessageHeaders } from "@smithy/types";
import { fromUtf8, toUtf8 } from "@smithy/util-utf8";
import { describe, expect,test as it } from "vitest";
import { describe, expect, test as it } from "vitest";

import { HeaderMarshaller } from "./HeaderMarshaller";
import { Int64 } from "./Int64";
Expand Down
2 changes: 1 addition & 1 deletion packages/eventstream-codec/src/Int64.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect,test as it } from "vitest";
import { describe, expect, test as it } from "vitest";

import { Int64 } from "./Int64";

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

import { MessageDecoderStream } from "./MessageDecoderStream";

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

import { MessageEncoderStream } from "./MessageEncoderStream";

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

import { SmithyMessageEncoderStream } from "./SmithyMessageEncoderStream";

Expand Down
2 changes: 1 addition & 1 deletion packages/eventstream-codec/src/splitMessage.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect,test as it } from "vitest";
import { describe, expect, test as it } from "vitest";

import { splitMessage } from "./splitMessage";

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,8 +4,9 @@ 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 { describe, expect, test as it } from "vitest";

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

describe("@httpApiKeyAuth integration tests", () => {
// Match `HttpApiKeyAuthService` `@httpApiKeyAuth` trait
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ 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 { describe, expect, test as it } from "vitest";

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

describe("@httpBearerAuth integration tests", () => {
// Arbitrary mock token
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
2 changes: 1 addition & 1 deletion packages/fetch-http-handler/src/stream-collector.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { StreamCollector } from "@smithy/types";

export const streamCollector: StreamCollector = async (stream: Blob | ReadableStream): Promise<Uint8Array> => {
if (typeof Blob === "function" && stream instanceof Blob || stream.constructor?.name === 'Blob') {
if ((typeof Blob === "function" && stream instanceof Blob) || stream.constructor?.name === "Blob") {
return new Uint8Array(await (stream as Blob).arrayBuffer());
}

Expand Down
2 changes: 1 addition & 1 deletion packages/hash-blob-browser/src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { toHex } from "@smithy/util-hex-encoding";
import { describe, expect,test as it } from "vitest";
import { describe, expect, test as it } from "vitest";

import { blobHasher } from "./index";

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-apply-body-checksum", () => {
describe(Weather.name, () => {
it("should add body-checksum", async () => {
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
Loading

0 comments on commit cdd09c3

Please sign in to comment.