Skip to content

Commit

Permalink
Switch to Vitest and remove UMD build format
Browse files Browse the repository at this point in the history
  • Loading branch information
JoseLion committed Jul 8, 2024
1 parent ca420b7 commit 09082e9
Show file tree
Hide file tree
Showing 53 changed files with 1,045 additions and 1,868 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
"eslint-plugin-sonarjs": "^1.0.3",
"globals": "^15.8.0",
"semantic-release": "^24.0.0",
"ts-node": "^10.9.2",
"tslib": "^2.6.3",
"turbo": "^2.0.6",
"typedoc": "^0.26.3",
Expand Down
15 changes: 6 additions & 9 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"source": "./src/main.ts",
"main": "./dist/main.cjs",
"module": "./dist/main.js",
"unpkg": "./dist/main.umd.cjs",
"types": "./dist/main.d.ts",
"exports": {
".": {
Expand All @@ -40,11 +39,11 @@
},
"scripts": {
"build": "vite build",
"check": "yarn compile && yarn test",
"check": "yarn compile && yarn test --run",
"compile": "tsc -p tsconfig.json",
"docs": "typedoc",
"release": "semantic-release",
"test": "NODE_ENV=test mocha",
"test": "vitest",
"test:silent": "DEBUG_PRINT_LIMIT=0 yarn test",
"test:verbose": "DEBUG_PRINT_LIMIT=1000000 yarn test"
},
Expand All @@ -57,17 +56,14 @@
},
"devDependencies": {
"@assertive-ts/core": "^2.1.0",
"@assertive-ts/sinon": "^1.0.0",
"@testing-library/dom": "^10.3.1",
"@testing-library/react": "^16.0.0",
"@testing-library/user-event": "^14.5.2",
"@types/mocha": "^10.0.7",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"@types/sinon": "^17.0.3",
"expect-type": "^0.19.0",
"jsdom": "^24.1.0",
"jsdom-global": "^3.0.2",
"mocha": "^10.6.0",
"happy-dom": "^14.12.3",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"semantic-release": "^24.0.0",
Expand All @@ -76,7 +72,8 @@
"tslib": "^2.6.3",
"typescript": "^5.5.3",
"vite": "^5.3.3",
"vite-plugin-dts": "^3.9.1"
"vite-plugin-dts": "^3.9.1",
"vitest": "^1.6.0"
},
"optionalDependencies": {
"yup": "^1.4.0",
Expand Down
9 changes: 0 additions & 9 deletions packages/core/test/hooks.ts

This file was deleted.

7 changes: 4 additions & 3 deletions packages/core/test/integration/lib/Form.context.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { expect } from "@assertive-ts/core";
import { renderHook } from "@testing-library/react";
import { describe, it, suite } from "vitest";

import { useForm, useFormSelector } from "../../../src/lib/Form.context";

describe("[Integration] Form.context.test.ts", () => {
suite("[Integration] Form.context.test.ts", () => {
describe(".useForm", () => {
context("when the hook is rendered", () => {
describe("when the hook is rendered", () => {
it("returns the complete Form context", () => {
const { result } = renderHook(() => useForm());
const { current } = result;
Expand All @@ -24,7 +25,7 @@ describe("[Integration] Form.context.test.ts", () => {
});

describe(".useFormSelector", () => {
context("when the hook is rendered", () => {
describe("when the hook is rendered", () => {
it("returns only the selected value from the Form context", () => {
const { result } = renderHook(() => useFormSelector(ctxt => ctxt.values));

Expand Down
44 changes: 25 additions & 19 deletions packages/core/test/integration/lib/Form.provider.test.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { expect } from "@assertive-ts/core";
import { render, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { userEvent } from "@testing-library/user-event";
import { type ReactElement, useCallback, useState } from "react";
import Sinon from "sinon";
import { describe, it, suite } from "vitest";
import { type ObjectSchema, boolean, isSchema, number, object, string } from "yup";
import { z } from "zod";

Expand Down Expand Up @@ -94,8 +95,8 @@ function WasSubmitted(): ReactElement {
);
}

describe("[Integration] Form.provider.test.tsx", () => {
context("when the provider children is a function", () => {
suite("[Integration] Form.provider.test.tsx", () => {
describe("when the provider children is a function", () => {
it("renders the result passing context as parameters", async () => {
const { getByText } = render(
<FormProvider<Foo>
Expand All @@ -121,7 +122,7 @@ describe("[Integration] Form.provider.test.tsx", () => {
});
});

context("when the provider children is not a function", () => {
describe("when the provider children is not a function", () => {
it("renders the children elements", async () => {
const { getByText } = render(
<FormProvider<Foo>
Expand All @@ -137,7 +138,7 @@ describe("[Integration] Form.provider.test.tsx", () => {
});
});

context("when the values prop changes", () => {
describe("when the values prop changes", () => {
it("updates the from values in the context", async () => {
const { getByText, findByRole } = render(<TestApp />);

Expand All @@ -159,12 +160,12 @@ describe("[Integration] Form.provider.test.tsx", () => {
});
});

context("when the submit function is called from the context", () => {
describe("when the submit function is called from the context", () => {
it("calls the onSubmit callback once", async () => {
const spySubmit = Sinon.spy<(v: Foo) => void>(() => undefined);
const submitSpy = Sinon.spy<(v: Foo) => void>(() => undefined);
const { findByRole } = render(
<FormProvider<Foo>
onSubmit={spySubmit}
onSubmit={submitSpy}
validation={yupSchema}
values={{ x: 1, y: "foo", z: true }}
>
Expand All @@ -180,19 +181,23 @@ describe("[Integration] Form.provider.test.tsx", () => {

await userEvent.click(submitButton);

Sinon.assert.calledOnceWithExactly(spySubmit, { x: 1, y: "foo", z: true });
await waitFor(() => {
expect(submitSpy)
.toBeCalledOnce()
.toHaveArgs({ x: 1, y: "foo", z: true });
});
});
});

[yupSchema, zodSchema].forEach(schema => {
const schemaName = isSchema(schema) ? "Yup" : "Zod";

context("when the validation fails", () => {
describe("when the validation fails", () => {
it("sets all the violations, does not call the onSubmit callback, sets the form as submitted", async () => {
const spySubmit = Sinon.spy<(v: Foo) => void>(() => undefined);
const submitSpy = Sinon.spy<(v: Foo) => void>(() => undefined);
const { findByRole, getByText } = render(
<TestApp
onSubmit={spySubmit}
onSubmit={submitSpy}
schema={schema}
/>,
);
Expand All @@ -205,19 +210,19 @@ describe("[Integration] Form.provider.test.tsx", () => {

await waitFor(() => getByText("Errors: x, y, z"));

Sinon.assert.notCalled(spySubmit);
expect(submitSpy).toNeverBeCalled();

await waitFor(() => getByText("Submitted? true"));
});
});

describe(`[${schemaName}] validate`, () => {
context("when the validation success", () => {
describe("when the validation success", () => {
it("clears the errors, calls the onSubmit callback once, and sets the form as submitted", async () => {
const spySubmit = Sinon.spy<(v: Foo) => void>(() => undefined);
const submitSpy = Sinon.spy<(v: Foo) => void>(() => undefined);
const { findByRole, getByText, queryByText } = render(
<TestApp
onSubmit={spySubmit}
onSubmit={submitSpy}
schema={schema}
/>,
);
Expand All @@ -244,11 +249,12 @@ describe("[Integration] Form.provider.test.tsx", () => {

await waitFor(() => {
expect(queryByText("Errors: x, y, z")).toBeNull();
getByText("Submitted? true");
});

Sinon.assert.calledWithExactly(spySubmit, { x: 5, y: "foo", z: true });

await waitFor(() => getByText("Submitted? true"));
expect(submitSpy)
.toBeCalledOnce()
.toHaveArgs({ x: 5, y: "foo", z: true });
});
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { render, waitFor } from "@testing-library/react";
import Sinon from "sinon";
import { describe, it, suite } from "vitest";
import { type ObjectSchema, array, number, object, string } from "yup";

import { FormProvider } from "../../../../src/lib/Form.provider";
Expand All @@ -17,8 +18,8 @@ const schema: ObjectSchema<Foo> = object({
name: string().required(),
});

describe("[Integration] ArrayField.component.test.tsx", () => {
context("when the array field is rendered", () => {
suite("[Integration] ArrayField.component.test.tsx", () => {
describe("when the array field is rendered", () => {
it("renders the result of the function in the children", async () => {
const { getByRole } = render(
<FormProvider<Foo>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { render, waitFor } from "@testing-library/react";
import Sinon from "sinon";
import { describe, it, suite } from "vitest";
import { type ObjectSchema, boolean, object, string } from "yup";

import { FormProvider } from "../../../../src/lib/Form.provider";
Expand All @@ -17,8 +18,8 @@ const schema: ObjectSchema<Foo> = object({
other: boolean().required(),
});

describe("[Integration] Field.component.test.tsx", () => {
context("when the field is rendered", () => {
suite("[Integration] Field.component.test.tsx", () => {
describe("when the field is rendered", () => {
it("renders the result of the function in the children", async () => {
const { getByDisplayValue } = render(
<FormProvider<Foo>
Expand Down
21 changes: 11 additions & 10 deletions packages/core/test/integration/lib/hooks/useArrayField.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expect } from "@assertive-ts/core";
import { type RenderHookResult, renderHook } from "@testing-library/react";
import Sinon from "sinon";
import { describe, it, suite } from "vitest";
import { type ObjectSchema, array, object, string } from "yup";

import { FormProvider } from "../../../../src/lib/Form.provider";
Expand Down Expand Up @@ -28,8 +29,8 @@ function renderWith(...roles: string[]): RenderHookResult<UseArrayField<string>,
});
}

describe("[Integration] useArrayField.test.tsx", () => {
context("when the hook is rendered", () => {
suite("[Integration] useArrayField.test.tsx", () => {
describe("when the hook is rendered", () => {
it("returns the array items and helpers", () => {
const { result } = renderWith("foo", "bar", "baz");
const { current } = result;
Expand Down Expand Up @@ -89,7 +90,7 @@ describe("[Integration] useArrayField.test.tsx", () => {
});

describe(".insert", () => {
context("when the index in inbounds", () => {
describe("when the index in inbounds", () => {
it("insters the item at the index", () => {
const { result, rerender } = renderWith("foo", "baz");

Expand All @@ -103,7 +104,7 @@ describe("[Integration] useArrayField.test.tsx", () => {
});
});

context("when the index is greater than the array size", () => {
describe("when the index is greater than the array size", () => {
it("insert the item at the end of the array", () => {
const { result, rerender } = renderWith("foo", "bar");

Expand All @@ -117,7 +118,7 @@ describe("[Integration] useArrayField.test.tsx", () => {
});
});

context("when the index is negative", () => {
describe("when the index is negative", () => {
it("treats the index as an offset", () => {
const { result, rerender } = renderWith("foo", "baz");

Expand All @@ -133,7 +134,7 @@ describe("[Integration] useArrayField.test.tsx", () => {
});

describe(".keygen", () => {
context("when a key is not provided", () => {
describe("when a key is not provided", () => {
it("returns a generated uuid", () => {
const pattern = /[A-F0-9]{8}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{12}/i;
const { result } = renderWith("foo", "bar");
Expand All @@ -147,7 +148,7 @@ describe("[Integration] useArrayField.test.tsx", () => {
});
});

context("when the key is provided", () => {
describe("when the key is provided", () => {
it("prefers the key over the generated uuid", () => {
const today = new Date();
const { result } = renderWith("foo", "bar");
Expand Down Expand Up @@ -190,7 +191,7 @@ describe("[Integration] useArrayField.test.tsx", () => {
});

describe(".replace", () => {
context("when the index is inbounds", () => {
describe("when the index is inbounds", () => {
it("replaces the item at the index", () => {
const { result, rerender } = renderWith("foo", "bar", "baz");

Expand All @@ -204,7 +205,7 @@ describe("[Integration] useArrayField.test.tsx", () => {
});
});

context("when the index is greater than the array size", () => {
describe("when the index is greater than the array size", () => {
it("insert the item at the end of the array", () => {
const { result, rerender } = renderWith("foo", "bar", "baz");

Expand All @@ -218,7 +219,7 @@ describe("[Integration] useArrayField.test.tsx", () => {
});
});

context("when the index is negative", () => {
describe("when the index is negative", () => {
it("treats the index as na offset", () => {
const { result, rerender } = renderWith("foo", "bar", "baz");

Expand Down
Loading

0 comments on commit 09082e9

Please sign in to comment.