Skip to content

test(core): install vitest #6581

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/pre-commit-hooks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: set up Node.js
uses: actions/setup-node@v3
with:
node-version: '16'
node-version: '18'
cache: 'yarn'

- name: install dependencies
run: yarn install --frozen-lockfile

- name: run pre-commit hooks
run: |
yarn lint-staged
Expand Down
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,19 @@
"turbo": "2.1.2",
"typescript": "~4.9.5",
"verdaccio": "5.25.0",
"vite": "4.5.5",
"vitest": "0.34.6",
"webpack": "5.76.0",
"webpack-cli": "4.10.0",
"yargs": "17.5.1",
"yarn": "1.22.13"
},
"overrides": {
"vite": "4.5.5"
},
"resolutions": {
"vite": "4.5.5"
},
"workspaces": {
"packages": [
"clients/*",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { test as it, describe, expect } from "vitest";

import { Kinesis } from "@aws-sdk/client-kinesis";
import { S3 } from "@aws-sdk/client-s3";
import { TranscribeStreaming } from "@aws-sdk/client-transcribe-streaming";
Expand Down
4 changes: 0 additions & 4 deletions packages/core/jest.config.integ.js

This file was deleted.

6 changes: 0 additions & 6 deletions packages/core/jest.config.js

This file was deleted.

4 changes: 2 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"lint": "node ./scripts/lint.js",
"clean": "rimraf ./dist-* && rimraf *.tsbuildinfo",
"extract:docs": "api-extractor run --local",
"test": "jest",
"test:integration": "jest -c jest.config.integ.js"
"test": "vitest run",
"test:integration": "vitest run -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
@@ -1,3 +1,5 @@
import { describe, expect, test as it } from "vitest";

import { validateAccountIdEndpointMode } from "./AccountIdEndpointModeConstants";

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

import { DEFAULT_ACCOUNT_ID_ENDPOINT_MODE } from "./AccountIdEndpointModeConstants";
import {
CONFIG_ACCOUNT_ID_ENDPOINT_MODE,
Expand All @@ -9,7 +11,7 @@ describe("NODE_ACCOUNT_ID_ENDPOINT_MODE_CONFIG_OPTIONS", () => {
const originalEnv = process.env;

beforeEach(() => {
jest.resetModules();
vi.resetModules();
process.env = { ...originalEnv };
});

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

import { emitWarningIfUnsupportedVersion, state } from "./emitWarningIfUnsupportedVersion";

describe("emitWarningIfUnsupportedVersion", () => {
let emitWarningIfUnsupportedVersion: any;
const emitWarning = process.emitWarning;
const supportedVersion = "18.0.0";

beforeEach(() => {
const module = require("./emitWarningIfUnsupportedVersion");
emitWarningIfUnsupportedVersion = module.emitWarningIfUnsupportedVersion;
});
beforeEach(() => {});

afterEach(() => {
jest.clearAllMocks();
jest.resetModules();
vi.clearAllMocks();
vi.resetModules();
process.emitWarning = emitWarning;
state.warningEmitted = false;
});

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

// Verify that the warning was emitted.
Expand Down Expand Up @@ -62,7 +63,7 @@ More information can be found at: https://a.co/74kJMmI`
[major + 1, 0, 0],
].map((arr) => `v${arr.join(".")}`)
)(`%s`, async (unsupportedVersion) => {
process.emitWarning = jest.fn();
process.emitWarning = vi.fn() as any;
emitWarningIfUnsupportedVersion(unsupportedVersion);
expect(process.emitWarning).not.toHaveBeenCalled();
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Stores whether the warning was already emitted.
let warningEmitted = false;
export const state = {
warningEmitted: false,
};

/**
* @internal
Expand All @@ -10,8 +12,8 @@ let warningEmitted = false;
* @param version - The Node.js version string.
*/
export const emitWarningIfUnsupportedVersion = (version: string) => {
if (version && !warningEmitted && parseInt(version.substring(1, version.indexOf("."))) < 18) {
warningEmitted = true;
if (version && !state.warningEmitted && parseInt(version.substring(1, version.indexOf("."))) < 18) {
state.warningEmitted = true;
process.emitWarning(
`NodeDeprecationWarning: The AWS SDK for JavaScript (v3) will
no longer support Node.js 16.x on January 6, 2025.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AttributedAwsCredentialIdentity } from "@aws-sdk/types";
import { describe, expect, test as it } from "vitest";

import { setCredentialFeature } from "./setCredentialFeature";

Expand Down
1 change: 1 addition & 0 deletions packages/core/src/submodules/client/setFeature.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AwsHandlerExecutionContext } from "@aws-sdk/types";
import { describe, test as it } from "vitest";

import { setFeature } from "./setFeature";

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

import { AwsSdkSigV4Signer } from "./AwsSdkSigV4Signer";

describe(AwsSdkSigV4Signer.name, () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { describe, expect, test as it } from "vitest";

import { resolveAwsSdkSigV4AConfig } from "./resolveAwsSdkSigV4AConfig";

describe(resolveAwsSdkSigV4AConfig.name, () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { afterEach, beforeEach, describe, expect, test as it, vi } from "vitest";

import { getSkewCorrectedDate } from "./getSkewCorrectedDate";

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

beforeEach(() => {
jest.spyOn(Date, "now").mockReturnValue(mockDateNow);
vi.spyOn(Date, "now").mockReturnValue(mockDateNow);
});

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

it.each([-100000, -100, 0, 100, 100000])("systemClockOffset: %d", (systemClockOffset) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
import { afterEach, beforeEach, describe, expect, test as it, vi } from "vitest";

import { getUpdatedSystemClockOffset } from "./getUpdatedSystemClockOffset";
import { isClockSkewed } from "./isClockSkewed";

jest.mock("./isClockSkewed");
vi.mock("./isClockSkewed");

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

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

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

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

beforeEach(() => {
(isClockSkewed as jest.Mock).mockReturnValue(true);
jest.spyOn(Date, "now").mockReturnValueOnce(mockClockTime.getTime());
vi.mocked(isClockSkewed).mockReturnValue(true);
vi.spyOn(Date, "now").mockReturnValueOnce(mockClockTime.getTime());
});

afterEach(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import { afterEach, beforeEach, describe, expect, test as it, vi } from "vitest";

import { getSkewCorrectedDate } from "./getSkewCorrectedDate";
import { isClockSkewed } from "./isClockSkewed";

jest.mock("./getSkewCorrectedDate");
vi.mock("./getSkewCorrectedDate");

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

beforeEach(() => {
(getSkewCorrectedDate as jest.Mock).mockReturnValue(mockSkewCorrectedDate);
vi.mocked(getSkewCorrectedDate).mockReturnValue(mockSkewCorrectedDate);
});

afterEach(() => {
expect(getSkewCorrectedDate).toHaveBeenCalledWith(mockSystemClockOffset);
jest.clearAllMocks();
vi.clearAllMocks();
});

describe("returns true for time difference >=300000", () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { afterAll, beforeAll, describe, expect, test as it } from "vitest";

import { _toBool, _toNum, _toStr } from "./coercing-serializers";

const consoleWarn = console.warn;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { describe, expect, test as it } from "vitest";

import { awsExpectUnion } from "./awsExpectUnion";

describe(awsExpectUnion.name, () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { SerdeContext } from "@smithy/types";
import { toUtf8 } from "@smithy/util-utf8";
import { describe, expect, test as it } from "vitest";

import { parseXmlBody } from "./parseXmlBody";

Expand Down
8 changes: 8 additions & 0 deletions packages/core/vitest.config.integ.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { defineConfig } from "vitest/config";

export default defineConfig({
test: {
include: ["**/*.integ.spec.{ts,js}"],
environment: "node",
},
});
9 changes: 9 additions & 0 deletions packages/core/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,js}"],
include: ["**/*.spec.{ts,js}"],
environment: "node",
},
});
102 changes: 102 additions & 0 deletions scripts/validation/vitest-validation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
const fs = require("fs");
const path = require("path");
const walk = require("../utils/walk");

const paths = [path.join(__dirname, "..", "..", "packages", "core")];

(async () => {
for (const folder of paths) {
const pkgJson = require(path.join(folder, "package.json"));

if (pkgJson.scripts.test) {
if (pkgJson.scripts.test.includes("jest")) {
console.log("setting unit test to vitest");

pkgJson.scripts.test = "vitest run";
fs.rmSync(path.join(folder, "jest.config.js"));
fs.writeFileSync(
path.join(folder, "vitest.config.ts"),
`import { defineConfig } from "vitest/config";

export default defineConfig({
test: {
exclude: ["**/*.{integ,e2e,browser}.spec.{ts,js}"],
include: ["**/*.spec.{ts,js}"],
environment: "node",
},
});
`
);
}
}

for (const testType of ["integ", "e2e"]) {
const script = testType === "integ" ? "integration" : testType;
if (pkgJson.scripts[`test:${script}`]) {
if (pkgJson.scripts[`test:${script}`].includes("jest")) {
console.log(`setting ${testType} test to vitest`);

pkgJson.scripts[`test:${script}`] = `vitest run -c vitest.config.${testType}.ts`;
fs.rmSync(path.join(folder, `jest.config.${testType}.js`));
fs.writeFileSync(
path.join(folder, `vitest.config.${testType}.ts`),
`import { defineConfig } from "vitest/config";

export default defineConfig({
test: {
include: ["**/*.${testType}.spec.{ts,js}"],
environment: "node",
},
});
`
);
}
}
}

fs.writeFileSync(path.join(folder, "package.json"), JSON.stringify(pkgJson, null, 2) + "\n");

for await (const file of walk(path.join(folder))) {
if (file.endsWith(".spec.ts")) {
let contents = fs.readFileSync(file, "utf-8");

const namespaces = {
jest: "vi",
};

const functionCalls = {
beforeEach: "beforeEach",
beforeAll: "beforeAll",
afterEach: "afterEach",
afterAll: "afterAll",
describe: "describe",
expect: "expect",
};

const imports = ["test as it"];

contents = contents.replace(/\((\w+) as (jest|vi).Mock\)/g, "vi.mocked($1)");

for (const [old, _new] of Object.entries(namespaces)) {
if (contents.includes(old + ".") || contents.includes(_new + ".")) {
imports.push(_new);
contents = contents.replace(new RegExp(old + "\\.", "g"), _new + ".");
}
}

for (const [old, _new] of Object.entries(functionCalls)) {
if (contents.includes(old + "(") || contents.includes(_new + "(")) {
if (!_new.includes(".")) {
imports.push(_new);
}
}
}

contents = contents.replace(/import {(.*?)} from "(vitest|vtestest)";/g, "");
contents = `import { ${imports.join(", ")} } from "vitest";\n\n` + contents;

fs.writeFileSync(file, contents);
}
}
}
})();
Loading
Loading