Skip to content

Commit cd1929b

Browse files
authored
test: convert tests to vitest (#1440)
* 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
1 parent c8d257b commit cd1929b

File tree

440 files changed

+3906
-5198
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

440 files changed

+3906
-5198
lines changed

.changeset/sweet-lions-design.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@smithy/fetch-http-handler": minor
3+
"@smithy/node-http-handler": minor
4+
"@smithy/util-stream": minor
5+
"@smithy/types": minor
6+
---
7+
8+
vitest compatibility

.changeset/violet-ghosts-complain.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
---

Makefile

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,15 @@ generate-protocol-tests:
1111
./gradlew :smithy-typescript-protocol-test-codegen:build
1212
rm -rf ./private/smithy-rpcv2-cbor
1313
cp -r ./smithy-typescript-protocol-test-codegen/build/smithyprojections/smithy-typescript-protocol-test-codegen/smithy-rpcv2-cbor/typescript-codegen ./private/smithy-rpcv2-cbor
14-
cp ./packages/core/jest.config.js ./private/smithy-rpcv2-cbor
14+
cp ./packages/core/vitest.config.ts ./private/smithy-rpcv2-cbor/vitest.config.js
1515
node ./scripts/post-protocol-test-codegen
1616
npx prettier --write ./private/smithy-rpcv2-cbor
1717
yarn
1818

1919
test-protocols:
20-
(cd ./private/smithy-rpcv2-cbor && npx jest)
20+
(cd ./private/smithy-rpcv2-cbor && npx vitest run --globals)
21+
22+
turbo-clean:
23+
@read -p "Are you sure you want to delete your local cache? [y/N]: " ans && [ $${ans:-N} = y ]
24+
@echo "\nDeleted cache folders: \n--------"
25+
@find . -name '.turbo' -type d -prune -print -exec rm -rf '{}' + && echo '\n'

jest.config.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

package.json

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@
2020
"extract:docs": "mkdir -p api-extractor-packages && turbo run extract:docs",
2121
"release": "yarn changeset publish",
2222
"build-test-packages": "./gradlew clean build && node ./scripts/build-generated-test-packages",
23-
"g:jest": "cd $INIT_CWD && jest",
24-
"g:karma": "cd $INIT_CWD && karma",
25-
"g:tsc": "cd $INIT_CWD && tsc"
23+
"g:tsc": "cd $INIT_CWD && tsc",
24+
"g:vitest": "cd $INIT_CWD && vitest"
2625
},
2726
"repository": {
2827
"type": "git",
@@ -39,29 +38,33 @@
3938
"@microsoft/api-extractor": "7.34.4",
4039
"@tsconfig/recommended": "1.0.2",
4140
"@types/jest": "28.1.3",
41+
"@types/jsdom": "20.0.1",
4242
"@typescript-eslint/eslint-plugin": "7.8.0",
4343
"@typescript-eslint/parser": "7.8.0",
4444
"esbuild": "0.19.11",
4545
"eslint": "8.57.0",
4646
"eslint-plugin-simple-import-sort": "7.0.0",
4747
"eslint-plugin-tsdoc": "0.2.17",
48+
"get-port": "^7.1.0",
49+
"happy-dom": "14.12.3",
4850
"husky": "^4.2.3",
4951
"jest": "29.7.0",
50-
"jest-environment-jsdom": "29.7.0",
51-
"karma": "6.4.3",
52-
"karma-chrome-launcher": "3.2.0",
53-
"karma-firefox-launcher": "2.1.3",
54-
"karma-jasmine": "5.1.0",
55-
"karma-typescript": "5.5.4",
56-
"karma-webpack": "5.0.1",
5752
"prettier": "3.2.5",
5853
"puppeteer": "^19.2.0",
5954
"ts-jest": "29.1.2",
6055
"turbo": "latest",
6156
"typescript": "~5.2.2",
57+
"vite": "4.5.5",
58+
"vitest": "0.34.6",
6259
"webpack": "5.91.0",
6360
"yarn": "1.22.22"
6461
},
62+
"overrides": {
63+
"vite": "4.5.5"
64+
},
65+
"resolutions": {
66+
"vite": "4.5.5"
67+
},
6568
"workspaces": [
6669
"packages/*",
6770
"smithy-typescript-ssdk-libs/*",

packages/abort-controller/jest.config.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

packages/abort-controller/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"lint": "eslint -c ../../.eslintrc.js \"src/**/*.ts\"",
1717
"format": "prettier --config ../../prettier.config.js --ignore-path ../.prettierignore --write \"**/*.{ts,md,json}\"",
1818
"extract:docs": "api-extractor run --local",
19-
"test": "yarn g:jest"
19+
"test": "yarn g:vitest run",
20+
"test:watch": "yarn g:vitest watch"
2021
},
2122
"author": {
2223
"name": "AWS SDK for JavaScript Team",

packages/abort-controller/src/AbortController.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { describe, expect, test as it } from "vitest";
2+
13
import { AbortController } from "./AbortController";
24
import { AbortSignal } from "./AbortSignal";
35

packages/abort-controller/src/AbortSignal.spec.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { describe, expect, test as it, vi } from "vitest";
2+
13
import { AbortController } from "./AbortController";
24

35
describe("AbortSignal", () => {
@@ -13,7 +15,7 @@ describe("AbortSignal", () => {
1315
it("should invoke the onabort handler when the signal is aborted", () => {
1416
const controller = new AbortController();
1517
const { signal } = controller;
16-
const abortHandler = jest.fn();
18+
const abortHandler = vi.fn();
1719
signal.onabort = abortHandler;
1820
expect(abortHandler.mock.calls.length).toBe(0);
1921
controller.abort();
@@ -23,7 +25,7 @@ describe("AbortSignal", () => {
2325
it("should not invoke the onabort handler multiple time", () => {
2426
const controller = new AbortController();
2527
const { signal } = controller;
26-
const abortHandler = jest.fn();
28+
const abortHandler = vi.fn();
2729
signal.onabort = abortHandler;
2830
expect(abortHandler.mock.calls.length).toBe(0);
2931
controller.abort();
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { defineConfig } from "vitest/config";
2+
3+
export default defineConfig({
4+
test: {
5+
exclude: ["**/*.{integ,e2e,browser}.spec.ts"],
6+
include: ["**/*.spec.ts"],
7+
environment: "node",
8+
},
9+
});

packages/chunked-blob-reader-native/jest.config.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

packages/chunked-blob-reader-native/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"clean": "rimraf ./dist-* && rimraf *.tsbuildinfo || exit 0",
1212
"lint": "eslint -c ../../.eslintrc.js \"src/**/*.ts\"",
1313
"format": "prettier --config ../../prettier.config.js --ignore-path ../.prettierignore --write \"**/*.{ts,md,json}\"",
14-
"test": "yarn g:jest"
14+
"test": "yarn g:vitest run",
15+
"test:watch": "yarn g:vitest watch"
1516
},
1617
"main": "./dist-cjs/index.js",
1718
"module": "./dist-es/index.js",

packages/chunked-blob-reader-native/src/index.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { describe, expect, test as it } from "vitest";
2+
13
import { blobReader } from "./index";
24

35
describe("blobReader", () => {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { defineConfig } from "vitest/config";
2+
3+
export default defineConfig({
4+
test: {
5+
exclude: ["**/*.{integ,e2e}.spec.ts"],
6+
include: ["**/*.spec.ts"],
7+
environment: "happy-dom",
8+
},
9+
});

packages/chunked-blob-reader/jest.config.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

packages/chunked-blob-reader/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"clean": "rimraf ./dist-* && rimraf *.tsbuildinfo || exit 0",
1212
"lint": "eslint -c ../../.eslintrc.js \"src/**/*.ts\"",
1313
"format": "prettier --config ../../prettier.config.js --ignore-path ../.prettierignore --write \"**/*.{ts,md,json}\"",
14-
"test": "yarn g:jest"
14+
"test": "yarn g:vitest run",
15+
"test:watch": "yarn g:vitest watch"
1516
},
1617
"main": "./dist-cjs/index.js",
1718
"module": "./dist-es/index.js",

packages/chunked-blob-reader/src/index.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Blob as BlobPolyfill } from "buffer";
2+
import { describe, expect, test as it } from "vitest";
23

34
import { blobReader } from "./index";
45

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { defineConfig } from "vitest/config";
2+
3+
export default defineConfig({
4+
test: {
5+
exclude: ["**/*.{integ,e2e,browser}.spec.ts"],
6+
include: ["**/*.spec.ts"],
7+
environment: "node",
8+
},
9+
});

packages/config-resolver/jest.config.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

packages/config-resolver/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
"clean": "rimraf ./dist-* && rimraf *.tsbuildinfo || exit 0",
1212
"lint": "eslint -c ../../.eslintrc.js \"src/**/*.ts\"",
1313
"format": "prettier --config ../../prettier.config.js --ignore-path ../.prettierignore --write \"**/*.{ts,md,json}\"",
14-
"test": "yarn g:jest",
15-
"extract:docs": "api-extractor run --local"
14+
"test": "yarn g:vitest run",
15+
"extract:docs": "api-extractor run --local",
16+
"test:watch": "yarn g:vitest watch"
1617
},
1718
"main": "./dist-cjs/index.js",
1819
"module": "./dist-es/index.js",

packages/config-resolver/src/endpointsConfig/NodeUseDualstackEndpointConfigOptions.spec.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { booleanSelector, SelectorType } from "@smithy/util-config-provider";
2+
import { afterEach, describe, expect, test as it, vi } from "vitest";
23

34
import {
45
CONFIG_USE_DUALSTACK_ENDPOINT,
@@ -7,23 +8,23 @@ import {
78
NODE_USE_DUALSTACK_ENDPOINT_CONFIG_OPTIONS,
89
} from "./NodeUseDualstackEndpointConfigOptions";
910

10-
jest.mock("@smithy/util-config-provider");
11+
vi.mock("@smithy/util-config-provider");
1112

1213
describe("NODE_USE_DUALSTACK_ENDPOINT_CONFIG_OPTIONS", () => {
1314
afterEach(() => {
14-
jest.clearAllMocks();
15+
vi.clearAllMocks();
1516
});
1617

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

2425
it("throws error", () => {
2526
const mockError = new Error("error");
26-
(booleanSelector as jest.Mock).mockImplementationOnce(() => {
27+
vi.mocked(booleanSelector).mockImplementationOnce(() => {
2728
throw mockError;
2829
});
2930
expect(() => {

packages/config-resolver/src/endpointsConfig/NodeUseFipsEndpointConfigOptions.spec.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { booleanSelector, SelectorType } from "@smithy/util-config-provider";
2+
import { afterEach, describe, expect, test as it, vi } from "vitest";
23

34
import {
45
CONFIG_USE_FIPS_ENDPOINT,
@@ -7,23 +8,23 @@ import {
78
NODE_USE_FIPS_ENDPOINT_CONFIG_OPTIONS,
89
} from "./NodeUseFipsEndpointConfigOptions";
910

10-
jest.mock("@smithy/util-config-provider");
11+
vi.mock("@smithy/util-config-provider");
1112

1213
describe("NODE_USE_FIPS_ENDPOINT_CONFIG_OPTIONS", () => {
1314
afterEach(() => {
14-
jest.clearAllMocks();
15+
vi.clearAllMocks();
1516
});
1617

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

2425
it("throws error", () => {
2526
const mockError = new Error("error");
26-
(booleanSelector as jest.Mock).mockImplementationOnce(() => {
27+
vi.mocked(booleanSelector).mockImplementationOnce(() => {
2728
throw mockError;
2829
});
2930
expect(() => {

packages/config-resolver/src/endpointsConfig/resolveCustomEndpointsConfig.spec.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { normalizeProvider } from "@smithy/util-middleware";
2+
import { afterEach, beforeEach, describe, expect, test as it, vi } from "vitest";
23

34
import { resolveCustomEndpointsConfig } from "./resolveCustomEndpointsConfig";
45

5-
jest.mock("@smithy/util-middleware");
6+
vi.mock("@smithy/util-middleware");
67

78
describe(resolveCustomEndpointsConfig.name, () => {
89
const mockEndpoint = {
@@ -13,18 +14,18 @@ describe(resolveCustomEndpointsConfig.name, () => {
1314

1415
const mockInput = {
1516
endpoint: mockEndpoint,
16-
urlParser: jest.fn(() => mockEndpoint),
17+
urlParser: vi.fn(() => mockEndpoint),
1718
useDualstackEndpoint: () => Promise.resolve(false),
1819
} as any;
1920

2021
beforeEach(() => {
21-
(normalizeProvider as jest.Mock).mockImplementation((input) =>
22+
vi.mocked(normalizeProvider).mockImplementation((input) =>
2223
typeof input === "function" ? input : () => Promise.resolve(input)
2324
);
2425
});
2526

2627
afterEach(() => {
27-
jest.clearAllMocks();
28+
vi.clearAllMocks();
2829
});
2930

3031
describe("tls", () => {

packages/config-resolver/src/endpointsConfig/resolveEndpointsConfig.spec.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
/* eslint-disable @typescript-eslint/no-unused-vars */
22
import { normalizeProvider } from "@smithy/util-middleware";
3+
import { afterEach, beforeEach, describe, expect, test as it, vi } from "vitest";
34

45
import { resolveEndpointsConfig } from "./resolveEndpointsConfig";
56
import { getEndpointFromRegion } from "./utils/getEndpointFromRegion";
67

7-
jest.mock("@smithy/util-middleware");
8-
jest.mock("./utils/getEndpointFromRegion");
8+
vi.mock("@smithy/util-middleware");
9+
vi.mock("./utils/getEndpointFromRegion");
910

1011
describe(resolveEndpointsConfig.name, () => {
1112
const mockEndpoint = {
@@ -16,20 +17,20 @@ describe(resolveEndpointsConfig.name, () => {
1617

1718
const mockInput = {
1819
endpoint: mockEndpoint,
19-
urlParser: jest.fn(() => mockEndpoint),
20+
urlParser: vi.fn(() => mockEndpoint),
2021
useDualstackEndpoint: () => Promise.resolve(false),
2122
useFipsEndpoint: () => Promise.resolve(false),
2223
} as any;
2324

2425
beforeEach(() => {
25-
(getEndpointFromRegion as jest.Mock).mockResolvedValueOnce(mockEndpoint);
26-
(normalizeProvider as jest.Mock).mockImplementation((input) =>
26+
vi.mocked(getEndpointFromRegion).mockResolvedValueOnce(mockEndpoint);
27+
vi.mocked(normalizeProvider).mockImplementation((input) =>
2728
typeof input === "function" ? input : () => Promise.resolve(input)
2829
);
2930
});
3031

3132
afterEach(() => {
32-
jest.clearAllMocks();
33+
vi.clearAllMocks();
3334
});
3435

3536
describe("tls", () => {

0 commit comments

Comments
 (0)