Skip to content

Commit d930c21

Browse files
authored
test(core): install vitest (#6581)
* chore: install vitest * test: set vite to 4.x
1 parent 6810a1a commit d930c21

25 files changed

+310
-44
lines changed

.github/workflows/pre-commit-hooks.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ jobs:
1111
runs-on: ubuntu-latest
1212
steps:
1313
- uses: actions/checkout@v4
14-
14+
1515
- name: set up Node.js
1616
uses: actions/setup-node@v3
1717
with:
18-
node-version: '16'
18+
node-version: '18'
1919
cache: 'yarn'
20-
20+
2121
- name: install dependencies
2222
run: yarn install --frozen-lockfile
23-
23+
2424
- name: run pre-commit hooks
2525
run: |
2626
yarn lint-staged

package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,19 @@
121121
"turbo": "2.1.2",
122122
"typescript": "~4.9.5",
123123
"verdaccio": "5.25.0",
124+
"vite": "4.5.5",
125+
"vitest": "0.34.6",
124126
"webpack": "5.76.0",
125127
"webpack-cli": "4.10.0",
126128
"yargs": "17.5.1",
127129
"yarn": "1.22.13"
128130
},
131+
"overrides": {
132+
"vite": "4.5.5"
133+
},
134+
"resolutions": {
135+
"vite": "4.5.5"
136+
},
129137
"workspaces": {
130138
"packages": [
131139
"clients/*",

packages/core/integ/request-handlers/request-handlers.integ.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { test as it, describe, expect } from "vitest";
2+
13
import { Kinesis } from "@aws-sdk/client-kinesis";
24
import { S3 } from "@aws-sdk/client-s3";
35
import { TranscribeStreaming } from "@aws-sdk/client-transcribe-streaming";

packages/core/jest.config.integ.js

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

packages/core/jest.config.js

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

packages/core/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
"lint": "node ./scripts/lint.js",
1313
"clean": "rimraf ./dist-* && rimraf *.tsbuildinfo",
1414
"extract:docs": "api-extractor run --local",
15-
"test": "jest",
16-
"test:integration": "jest -c jest.config.integ.js"
15+
"test": "vitest run",
16+
"test:integration": "vitest run -c vitest.config.integ.ts"
1717
},
1818
"main": "./dist-cjs/index.js",
1919
"module": "./dist-es/index.js",

packages/core/src/submodules/account-id-endpoint/AccountIdEndpointModeConstants.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 { validateAccountIdEndpointMode } from "./AccountIdEndpointModeConstants";
24

35
describe("validateAccountIdEndpointMode", () => {

packages/core/src/submodules/account-id-endpoint/NodeAccountIdEndpointModeConfigOptions.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { afterEach, beforeEach, describe, expect, test as it, vi } from "vitest";
2+
13
import { DEFAULT_ACCOUNT_ID_ENDPOINT_MODE } from "./AccountIdEndpointModeConstants";
24
import {
35
CONFIG_ACCOUNT_ID_ENDPOINT_MODE,
@@ -9,7 +11,7 @@ describe("NODE_ACCOUNT_ID_ENDPOINT_MODE_CONFIG_OPTIONS", () => {
911
const originalEnv = process.env;
1012

1113
beforeEach(() => {
12-
jest.resetModules();
14+
vi.resetModules();
1315
process.env = { ...originalEnv };
1416
});
1517

packages/core/src/submodules/client/emitWarningIfUnsupportedVersion.spec.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1+
import { afterEach, beforeEach, describe, expect, test as it, vi } from "vitest";
2+
3+
import { emitWarningIfUnsupportedVersion, state } from "./emitWarningIfUnsupportedVersion";
4+
15
describe("emitWarningIfUnsupportedVersion", () => {
2-
let emitWarningIfUnsupportedVersion: any;
36
const emitWarning = process.emitWarning;
47
const supportedVersion = "18.0.0";
58

6-
beforeEach(() => {
7-
const module = require("./emitWarningIfUnsupportedVersion");
8-
emitWarningIfUnsupportedVersion = module.emitWarningIfUnsupportedVersion;
9-
});
9+
beforeEach(() => {});
1010

1111
afterEach(() => {
12-
jest.clearAllMocks();
13-
jest.resetModules();
12+
vi.clearAllMocks();
13+
vi.resetModules();
1414
process.emitWarning = emitWarning;
15+
state.warningEmitted = false;
1516
});
1617

1718
describe(`emits warning for Node.js <${supportedVersion}`, () => {
@@ -31,7 +32,7 @@ describe("emitWarningIfUnsupportedVersion", () => {
3132
[getPreviousMajorVersion(major), 0, 0],
3233
].map((arr) => `v${arr.join(".")}`)
3334
)(`%s`, async (unsupportedVersion) => {
34-
process.emitWarning = jest.fn();
35+
process.emitWarning = vi.fn() as any;
3536
emitWarningIfUnsupportedVersion(unsupportedVersion);
3637

3738
// Verify that the warning was emitted.
@@ -62,7 +63,7 @@ More information can be found at: https://a.co/74kJMmI`
6263
[major + 1, 0, 0],
6364
].map((arr) => `v${arr.join(".")}`)
6465
)(`%s`, async (unsupportedVersion) => {
65-
process.emitWarning = jest.fn();
66+
process.emitWarning = vi.fn() as any;
6667
emitWarningIfUnsupportedVersion(unsupportedVersion);
6768
expect(process.emitWarning).not.toHaveBeenCalled();
6869
});

packages/core/src/submodules/client/emitWarningIfUnsupportedVersion.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// Stores whether the warning was already emitted.
2-
let warningEmitted = false;
2+
export const state = {
3+
warningEmitted: false,
4+
};
35

46
/**
57
* @internal
@@ -10,8 +12,8 @@ let warningEmitted = false;
1012
* @param version - The Node.js version string.
1113
*/
1214
export const emitWarningIfUnsupportedVersion = (version: string) => {
13-
if (version && !warningEmitted && parseInt(version.substring(1, version.indexOf("."))) < 18) {
14-
warningEmitted = true;
15+
if (version && !state.warningEmitted && parseInt(version.substring(1, version.indexOf("."))) < 18) {
16+
state.warningEmitted = true;
1517
process.emitWarning(
1618
`NodeDeprecationWarning: The AWS SDK for JavaScript (v3) will
1719
no longer support Node.js 16.x on January 6, 2025.

packages/core/src/submodules/client/setCredentialFeature.spec.ts

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

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

packages/core/src/submodules/client/setFeature.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { AwsHandlerExecutionContext } from "@aws-sdk/types";
2+
import { describe, test as it } from "vitest";
23

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

packages/core/src/submodules/httpAuthSchemes/aws_sdk/AwsSdkSigV4Signer.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 { AwsSdkSigV4Signer } from "./AwsSdkSigV4Signer";
24

35
describe(AwsSdkSigV4Signer.name, () => {

packages/core/src/submodules/httpAuthSchemes/aws_sdk/resolveAwsSdkSigV4AConfig.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 { resolveAwsSdkSigV4AConfig } from "./resolveAwsSdkSigV4AConfig";
24

35
describe(resolveAwsSdkSigV4AConfig.name, () => {

packages/core/src/submodules/httpAuthSchemes/utils/getSkewCorrectedDate.spec.ts

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

35
describe(getSkewCorrectedDate.name, () => {
46
const mockDateNow = Date.now();
57

68
beforeEach(() => {
7-
jest.spyOn(Date, "now").mockReturnValue(mockDateNow);
9+
vi.spyOn(Date, "now").mockReturnValue(mockDateNow);
810
});
911

1012
afterEach(() => {
11-
jest.clearAllMocks();
13+
vi.clearAllMocks();
1214
});
1315

1416
it.each([-100000, -100, 0, 100, 100000])("systemClockOffset: %d", (systemClockOffset) => {

packages/core/src/submodules/httpAuthSchemes/utils/getUpdatedSystemClockOffset.spec.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
1+
import { afterEach, beforeEach, describe, expect, test as it, vi } from "vitest";
2+
13
import { getUpdatedSystemClockOffset } from "./getUpdatedSystemClockOffset";
24
import { isClockSkewed } from "./isClockSkewed";
35

4-
jest.mock("./isClockSkewed");
6+
vi.mock("./isClockSkewed");
57

68
describe(getUpdatedSystemClockOffset.name, () => {
79
// Mock ServerTime is accurate to last second, to remove milliseconds information.
810
const mockClockTime = new Date(Math.floor(Date.now() / 1000) * 1000);
911
const mockSystemClockOffset = 100;
1012

1113
afterEach(() => {
12-
jest.clearAllMocks();
14+
vi.clearAllMocks();
1315
});
1416

1517
it("returns passed systemClockOffset when clock is not skewed", () => {
16-
(isClockSkewed as jest.Mock).mockReturnValue(false);
18+
vi.mocked(isClockSkewed).mockReturnValue(false);
1719
expect(getUpdatedSystemClockOffset(mockClockTime.toString(), mockSystemClockOffset)).toEqual(mockSystemClockOffset);
1820
});
1921

2022
describe("returns difference between serverTime and current time when clock is skewed", () => {
2123
const dateDotNowFn = Date.now;
2224

2325
beforeEach(() => {
24-
(isClockSkewed as jest.Mock).mockReturnValue(true);
25-
jest.spyOn(Date, "now").mockReturnValueOnce(mockClockTime.getTime());
26+
vi.mocked(isClockSkewed).mockReturnValue(true);
27+
vi.spyOn(Date, "now").mockReturnValueOnce(mockClockTime.getTime());
2628
});
2729

2830
afterEach(() => {

packages/core/src/submodules/httpAuthSchemes/utils/isClockSkewed.spec.ts

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

4-
jest.mock("./getSkewCorrectedDate");
6+
vi.mock("./getSkewCorrectedDate");
57

68
describe(isClockSkewed.name, () => {
79
const mockSystemClockOffset = 100;
810
const mockSkewCorrectedDate = new Date();
911

1012
beforeEach(() => {
11-
(getSkewCorrectedDate as jest.Mock).mockReturnValue(mockSkewCorrectedDate);
13+
vi.mocked(getSkewCorrectedDate).mockReturnValue(mockSkewCorrectedDate);
1214
});
1315

1416
afterEach(() => {
1517
expect(getSkewCorrectedDate).toHaveBeenCalledWith(mockSystemClockOffset);
16-
jest.clearAllMocks();
18+
vi.clearAllMocks();
1719
});
1820

1921
describe("returns true for time difference >=300000", () => {

packages/core/src/submodules/protocols/coercing-serializers.spec.ts

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

35
const consoleWarn = console.warn;

packages/core/src/submodules/protocols/json/awsExpectUnion.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 { awsExpectUnion } from "./awsExpectUnion";
24

35
describe(awsExpectUnion.name, () => {

packages/core/src/submodules/protocols/xml/parseXmlBody.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { SerdeContext } from "@smithy/types";
22
import { toUtf8 } from "@smithy/util-utf8";
3+
import { describe, expect, test as it } from "vitest";
34

45
import { parseXmlBody } from "./parseXmlBody";
56

packages/core/vitest.config.integ.ts

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

packages/core/vitest.config.ts

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,js}"],
6+
include: ["**/*.spec.{ts,js}"],
7+
environment: "node",
8+
},
9+
});
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
const fs = require("fs");
2+
const path = require("path");
3+
const walk = require("../utils/walk");
4+
5+
const paths = [path.join(__dirname, "..", "..", "packages", "core")];
6+
7+
(async () => {
8+
for (const folder of paths) {
9+
const pkgJson = require(path.join(folder, "package.json"));
10+
11+
if (pkgJson.scripts.test) {
12+
if (pkgJson.scripts.test.includes("jest")) {
13+
console.log("setting unit test to vitest");
14+
15+
pkgJson.scripts.test = "vitest run";
16+
fs.rmSync(path.join(folder, "jest.config.js"));
17+
fs.writeFileSync(
18+
path.join(folder, "vitest.config.ts"),
19+
`import { defineConfig } from "vitest/config";
20+
21+
export default defineConfig({
22+
test: {
23+
exclude: ["**/*.{integ,e2e,browser}.spec.{ts,js}"],
24+
include: ["**/*.spec.{ts,js}"],
25+
environment: "node",
26+
},
27+
});
28+
`
29+
);
30+
}
31+
}
32+
33+
for (const testType of ["integ", "e2e"]) {
34+
const script = testType === "integ" ? "integration" : testType;
35+
if (pkgJson.scripts[`test:${script}`]) {
36+
if (pkgJson.scripts[`test:${script}`].includes("jest")) {
37+
console.log(`setting ${testType} test to vitest`);
38+
39+
pkgJson.scripts[`test:${script}`] = `vitest run -c vitest.config.${testType}.ts`;
40+
fs.rmSync(path.join(folder, `jest.config.${testType}.js`));
41+
fs.writeFileSync(
42+
path.join(folder, `vitest.config.${testType}.ts`),
43+
`import { defineConfig } from "vitest/config";
44+
45+
export default defineConfig({
46+
test: {
47+
include: ["**/*.${testType}.spec.{ts,js}"],
48+
environment: "node",
49+
},
50+
});
51+
`
52+
);
53+
}
54+
}
55+
}
56+
57+
fs.writeFileSync(path.join(folder, "package.json"), JSON.stringify(pkgJson, null, 2) + "\n");
58+
59+
for await (const file of walk(path.join(folder))) {
60+
if (file.endsWith(".spec.ts")) {
61+
let contents = fs.readFileSync(file, "utf-8");
62+
63+
const namespaces = {
64+
jest: "vi",
65+
};
66+
67+
const functionCalls = {
68+
beforeEach: "beforeEach",
69+
beforeAll: "beforeAll",
70+
afterEach: "afterEach",
71+
afterAll: "afterAll",
72+
describe: "describe",
73+
expect: "expect",
74+
};
75+
76+
const imports = ["test as it"];
77+
78+
contents = contents.replace(/\((\w+) as (jest|vi).Mock\)/g, "vi.mocked($1)");
79+
80+
for (const [old, _new] of Object.entries(namespaces)) {
81+
if (contents.includes(old + ".") || contents.includes(_new + ".")) {
82+
imports.push(_new);
83+
contents = contents.replace(new RegExp(old + "\\.", "g"), _new + ".");
84+
}
85+
}
86+
87+
for (const [old, _new] of Object.entries(functionCalls)) {
88+
if (contents.includes(old + "(") || contents.includes(_new + "(")) {
89+
if (!_new.includes(".")) {
90+
imports.push(_new);
91+
}
92+
}
93+
}
94+
95+
contents = contents.replace(/import {(.*?)} from "(vitest|vtestest)";/g, "");
96+
contents = `import { ${imports.join(", ")} } from "vitest";\n\n` + contents;
97+
98+
fs.writeFileSync(file, contents);
99+
}
100+
}
101+
}
102+
})();

0 commit comments

Comments
 (0)