Skip to content

Commit

Permalink
test: convert tests to vitest (#1440)
Browse files Browse the repository at this point in the history
* test: convert tests to vitest

* remove karma

* test: fixing unit tests in vitest

* fetch browser unit tests

* fix node http tests

* formatting

* convert nock to vitest mock in imds

* fix fetch browser tests

* fix empty test in readablestream

* use jsdom types temporarily

* skip test on node16

* formatting
  • Loading branch information
kuhe authored Oct 30, 2024
1 parent c8d257b commit cd1929b
Show file tree
Hide file tree
Showing 440 changed files with 3,906 additions and 5,198 deletions.
8 changes: 8 additions & 0 deletions .changeset/sweet-lions-design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@smithy/fetch-http-handler": minor
"@smithy/node-http-handler": minor
"@smithy/util-stream": minor
"@smithy/types": minor
---

vitest compatibility
2 changes: 2 additions & 0 deletions .changeset/violet-ghosts-complain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
9 changes: 7 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@ generate-protocol-tests:
./gradlew :smithy-typescript-protocol-test-codegen:build
rm -rf ./private/smithy-rpcv2-cbor
cp -r ./smithy-typescript-protocol-test-codegen/build/smithyprojections/smithy-typescript-protocol-test-codegen/smithy-rpcv2-cbor/typescript-codegen ./private/smithy-rpcv2-cbor
cp ./packages/core/jest.config.js ./private/smithy-rpcv2-cbor
cp ./packages/core/vitest.config.ts ./private/smithy-rpcv2-cbor/vitest.config.js
node ./scripts/post-protocol-test-codegen
npx prettier --write ./private/smithy-rpcv2-cbor
yarn

test-protocols:
(cd ./private/smithy-rpcv2-cbor && npx jest)
(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.

23 changes: 13 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@
"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:karma": "cd $INIT_CWD && karma",
"g:tsc": "cd $INIT_CWD && tsc"
"g:tsc": "cd $INIT_CWD && tsc",
"g:vitest": "cd $INIT_CWD && vitest"
},
"repository": {
"type": "git",
Expand All @@ -39,29 +38,33 @@
"@microsoft/api-extractor": "7.34.4",
"@tsconfig/recommended": "1.0.2",
"@types/jest": "28.1.3",
"@types/jsdom": "20.0.1",
"@typescript-eslint/eslint-plugin": "7.8.0",
"@typescript-eslint/parser": "7.8.0",
"esbuild": "0.19.11",
"eslint": "8.57.0",
"eslint-plugin-simple-import-sort": "7.0.0",
"eslint-plugin-tsdoc": "0.2.17",
"get-port": "^7.1.0",
"happy-dom": "14.12.3",
"husky": "^4.2.3",
"jest": "29.7.0",
"jest-environment-jsdom": "29.7.0",
"karma": "6.4.3",
"karma-chrome-launcher": "3.2.0",
"karma-firefox-launcher": "2.1.3",
"karma-jasmine": "5.1.0",
"karma-typescript": "5.5.4",
"karma-webpack": "5.0.1",
"prettier": "3.2.5",
"puppeteer": "^19.2.0",
"ts-jest": "29.1.2",
"turbo": "latest",
"typescript": "~5.2.2",
"vite": "4.5.5",
"vitest": "0.34.6",
"webpack": "5.91.0",
"yarn": "1.22.22"
},
"overrides": {
"vite": "4.5.5"
},
"resolutions": {
"vite": "4.5.5"
},
"workspaces": [
"packages/*",
"smithy-typescript-ssdk-libs/*",
Expand Down
5 changes: 0 additions & 5 deletions packages/abort-controller/jest.config.js

This file was deleted.

3 changes: 2 additions & 1 deletion packages/abort-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"lint": "eslint -c ../../.eslintrc.js \"src/**/*.ts\"",
"format": "prettier --config ../../prettier.config.js --ignore-path ../.prettierignore --write \"**/*.{ts,md,json}\"",
"extract:docs": "api-extractor run --local",
"test": "yarn g:jest"
"test": "yarn g:vitest run",
"test:watch": "yarn g:vitest watch"
},
"author": {
"name": "AWS SDK for JavaScript Team",
Expand Down
2 changes: 2 additions & 0 deletions packages/abort-controller/src/AbortController.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 { AbortController } from "./AbortController";
import { AbortSignal } from "./AbortSignal";

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

import { AbortController } from "./AbortController";

describe("AbortSignal", () => {
Expand All @@ -13,7 +15,7 @@ describe("AbortSignal", () => {
it("should invoke the onabort handler when the signal is aborted", () => {
const controller = new AbortController();
const { signal } = controller;
const abortHandler = jest.fn();
const abortHandler = vi.fn();
signal.onabort = abortHandler;
expect(abortHandler.mock.calls.length).toBe(0);
controller.abort();
Expand All @@ -23,7 +25,7 @@ describe("AbortSignal", () => {
it("should not invoke the onabort handler multiple time", () => {
const controller = new AbortController();
const { signal } = controller;
const abortHandler = jest.fn();
const abortHandler = vi.fn();
signal.onabort = abortHandler;
expect(abortHandler.mock.calls.length).toBe(0);
controller.abort();
Expand Down
9 changes: 9 additions & 0 deletions packages/abort-controller/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",
},
});
6 changes: 0 additions & 6 deletions packages/chunked-blob-reader-native/jest.config.js

This file was deleted.

3 changes: 2 additions & 1 deletion packages/chunked-blob-reader-native/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"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": "yarn g:jest"
"test": "yarn g:vitest run",
"test:watch": "yarn g:vitest watch"
},
"main": "./dist-cjs/index.js",
"module": "./dist-es/index.js",
Expand Down
2 changes: 2 additions & 0 deletions packages/chunked-blob-reader-native/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 { blobReader } from "./index";

describe("blobReader", () => {
Expand Down
9 changes: 9 additions & 0 deletions packages/chunked-blob-reader-native/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}.spec.ts"],
include: ["**/*.spec.ts"],
environment: "happy-dom",
},
});
6 changes: 0 additions & 6 deletions packages/chunked-blob-reader/jest.config.js

This file was deleted.

3 changes: 2 additions & 1 deletion packages/chunked-blob-reader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"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": "yarn g:jest"
"test": "yarn g:vitest run",
"test:watch": "yarn g:vitest watch"
},
"main": "./dist-cjs/index.js",
"module": "./dist-es/index.js",
Expand Down
1 change: 1 addition & 0 deletions packages/chunked-blob-reader/src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Blob as BlobPolyfill } from "buffer";
import { describe, expect, test as it } from "vitest";

import { blobReader } from "./index";

Expand Down
9 changes: 9 additions & 0 deletions packages/chunked-blob-reader/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/config-resolver/jest.config.js

This file was deleted.

5 changes: 3 additions & 2 deletions packages/config-resolver/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
"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": "yarn g:jest",
"extract:docs": "api-extractor run --local"
"test": "yarn g:vitest run",
"extract:docs": "api-extractor run --local",
"test:watch": "yarn g: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,4 +1,5 @@
import { booleanSelector, SelectorType } from "@smithy/util-config-provider";
import { afterEach, describe, expect, test as it, vi } from "vitest";

import {
CONFIG_USE_DUALSTACK_ENDPOINT,
Expand All @@ -7,23 +8,23 @@ import {
NODE_USE_DUALSTACK_ENDPOINT_CONFIG_OPTIONS,
} from "./NodeUseDualstackEndpointConfigOptions";

jest.mock("@smithy/util-config-provider");
vi.mock("@smithy/util-config-provider");

describe("NODE_USE_DUALSTACK_ENDPOINT_CONFIG_OPTIONS", () => {
afterEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});

const test = (func: Function, obj: Record<string, string>, key: string, type: SelectorType) => {
it.each([true, false, undefined])("returns %s", (output) => {
(booleanSelector as jest.Mock).mockReturnValueOnce(output);
vi.mocked(booleanSelector).mockReturnValueOnce(output);
expect(func(obj)).toEqual(output);
expect(booleanSelector).toBeCalledWith(obj, key, type);
});

it("throws error", () => {
const mockError = new Error("error");
(booleanSelector as jest.Mock).mockImplementationOnce(() => {
vi.mocked(booleanSelector).mockImplementationOnce(() => {
throw mockError;
});
expect(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { booleanSelector, SelectorType } from "@smithy/util-config-provider";
import { afterEach, describe, expect, test as it, vi } from "vitest";

import {
CONFIG_USE_FIPS_ENDPOINT,
Expand All @@ -7,23 +8,23 @@ import {
NODE_USE_FIPS_ENDPOINT_CONFIG_OPTIONS,
} from "./NodeUseFipsEndpointConfigOptions";

jest.mock("@smithy/util-config-provider");
vi.mock("@smithy/util-config-provider");

describe("NODE_USE_FIPS_ENDPOINT_CONFIG_OPTIONS", () => {
afterEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});

const test = (func: Function, obj: Record<string, string>, key: string, type: SelectorType) => {
it.each([true, false, undefined])("returns %s", (output) => {
(booleanSelector as jest.Mock).mockReturnValueOnce(output);
vi.mocked(booleanSelector).mockReturnValueOnce(output);
expect(func(obj)).toEqual(output);
expect(booleanSelector).toBeCalledWith(obj, key, type);
});

it("throws error", () => {
const mockError = new Error("error");
(booleanSelector as jest.Mock).mockImplementationOnce(() => {
vi.mocked(booleanSelector).mockImplementationOnce(() => {
throw mockError;
});
expect(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { normalizeProvider } from "@smithy/util-middleware";
import { afterEach, beforeEach, describe, expect, test as it, vi } from "vitest";

import { resolveCustomEndpointsConfig } from "./resolveCustomEndpointsConfig";

jest.mock("@smithy/util-middleware");
vi.mock("@smithy/util-middleware");

describe(resolveCustomEndpointsConfig.name, () => {
const mockEndpoint = {
Expand All @@ -13,18 +14,18 @@ describe(resolveCustomEndpointsConfig.name, () => {

const mockInput = {
endpoint: mockEndpoint,
urlParser: jest.fn(() => mockEndpoint),
urlParser: vi.fn(() => mockEndpoint),
useDualstackEndpoint: () => Promise.resolve(false),
} as any;

beforeEach(() => {
(normalizeProvider as jest.Mock).mockImplementation((input) =>
vi.mocked(normalizeProvider).mockImplementation((input) =>
typeof input === "function" ? input : () => Promise.resolve(input)
);
});

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

describe("tls", () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import { normalizeProvider } from "@smithy/util-middleware";
import { afterEach, beforeEach, describe, expect, test as it, vi } from "vitest";

import { resolveEndpointsConfig } from "./resolveEndpointsConfig";
import { getEndpointFromRegion } from "./utils/getEndpointFromRegion";

jest.mock("@smithy/util-middleware");
jest.mock("./utils/getEndpointFromRegion");
vi.mock("@smithy/util-middleware");
vi.mock("./utils/getEndpointFromRegion");

describe(resolveEndpointsConfig.name, () => {
const mockEndpoint = {
Expand All @@ -16,20 +17,20 @@ describe(resolveEndpointsConfig.name, () => {

const mockInput = {
endpoint: mockEndpoint,
urlParser: jest.fn(() => mockEndpoint),
urlParser: vi.fn(() => mockEndpoint),
useDualstackEndpoint: () => Promise.resolve(false),
useFipsEndpoint: () => Promise.resolve(false),
} as any;

beforeEach(() => {
(getEndpointFromRegion as jest.Mock).mockResolvedValueOnce(mockEndpoint);
(normalizeProvider as jest.Mock).mockImplementation((input) =>
vi.mocked(getEndpointFromRegion).mockResolvedValueOnce(mockEndpoint);
vi.mocked(normalizeProvider).mockImplementation((input) =>
typeof input === "function" ? input : () => Promise.resolve(input)
);
});

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

describe("tls", () => {
Expand Down
Loading

0 comments on commit cd1929b

Please sign in to comment.