Skip to content

Commit b1ba1ea

Browse files
author
Alex Lohr
committed
update dependencies, fix cleanup, bump to 0.8.5
1 parent c999ef5 commit b1ba1ea

File tree

12 files changed

+1405
-1775
lines changed

12 files changed

+1405
-1775
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ const results = render(() => <YourComponent />, options);
8181

8282
Solid.js reactive changes are pretty instantaneous, so there is rarely need to use `waitFor(…)`, `await findByRole(…)` and other asynchronous queries to test the rendered result, except for transitions, suspense, resources and router navigation.
8383

84-
⚠️ In extension of the original API, the render function of this testing library supports convenient `location` and `routeDataFunc` options that will set up a router with memory integration pointing at a certain path if `location` is given and primed with the `routeDataFunc` as data. Since this setup is not instantaneous, you need to first use asynchronous queries (`findBy`) after employing it:
84+
⚠️ In extension of the original API, the render function of this testing library supports a convenient `location` option that will set up an in-memory router pointing at the specified location. Since this setup is not instantaneous, you need to first use asynchronous queries (`findBy`) after employing it:
8585

8686
```tsx
8787
it('uses params', async () => {

package-lock.json

Lines changed: 1348 additions & 1698 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@solidjs/testing-library",
3-
"version": "0.8.4",
3+
"version": "0.8.5",
44
"description": "Simple and complete Solid testing utilities that encourage good testing practices.",
55
"type": "module",
66
"main": "./dist/index.cjs",
@@ -60,19 +60,19 @@
6060
"@testing-library/dom": "^9.3.1"
6161
},
6262
"devDependencies": {
63-
"@solidjs/router": "^0.8.2",
64-
"@testing-library/jest-dom": "^5.16.5",
65-
"@testing-library/user-event": "^14.4.3",
66-
"@vitest/coverage-v8": "^0.32.2",
63+
"@solidjs/router": "^0.10.5",
64+
"@testing-library/jest-dom": "^6.1.5",
65+
"@testing-library/user-event": "^14.5.1",
66+
"@vitest/coverage-v8": "^1.1.0",
6767
"coveralls": "^3.1.1",
68-
"jsdom": "^22.1.0",
69-
"prettier": "^2.8.8",
70-
"pretty-format": "^29.5.0",
71-
"solid-js": "^1.7.7",
72-
"tsup": "7.1.0",
73-
"typescript": "^5.1.6",
74-
"vite-plugin-solid": "^2.7.0",
75-
"vitest": "^0.32.2"
68+
"jsdom": "^23.0.1",
69+
"prettier": "^3.1.1",
70+
"pretty-format": "^29.7.0",
71+
"solid-js": "^1.8.7",
72+
"tsup": "8.0.1",
73+
"typescript": "^5.3.3",
74+
"vite-plugin-solid": "^2.8.0",
75+
"vitest": "^1.1.0"
7676
},
7777
"peerDependencies": {
7878
"@solidjs/router": ">=0.6.0",

src/__tests__/basic.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import "@testing-library/jest-dom/extend-expect";
1+
import "@testing-library/jest-dom/vitest";
22
import {
33
createSignal,
44
createEffect,
@@ -8,7 +8,6 @@ import {
88
Accessor,
99
getOwner,
1010
createRoot,
11-
runWithOwner
1211
} from "solid-js";
1312
import type { JSX } from "solid-js";
1413
import { render, renderDirective, renderHook, screen, testEffect } from "..";

src/__tests__/broken-cleanup.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
vi.mock('solid-js', () => ({ hydrate: () => {}, render: () => {} }));
2+
import { render } from '..';
3+
4+
const errors: Event[] = [];
5+
const handler = (ev: Event) => errors.push(ev);
6+
globalThis.addEventListener('error', handler);
7+
8+
test('trigger auto-cleanup', () => {
9+
render(() => <div>Hi</div>);
10+
});
11+
12+
test('check if auto-cleanup threw an error', () => {
13+
expect(errors).toEqual([]);
14+
globalThis.removeEventListener('error', handler);
15+
});
16+

src/__tests__/cleanup.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import "@testing-library/jest-dom/extend-expect";
2-
import { onCleanup } from "solid-js";
1+
import "@testing-library/jest-dom/vitest";
2+
import { onCleanup, render as soildRender } from "solid-js";
33
import { cleanup, render } from "..";
44

55
test("cleans up the document", () => {
@@ -24,3 +24,4 @@ test("cleanup does not error when an element is not a child", () => {
2424
render(() => <div />, { container: document.createElement("div") });
2525
cleanup();
2626
});
27+

src/__tests__/debug.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import "@testing-library/jest-dom/extend-expect";
1+
import "@testing-library/jest-dom/vitest";
22
import { screen, render } from "..";
33

44
beforeEach(() => {

src/__tests__/end-to-end.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import "@testing-library/jest-dom/extend-expect";
1+
import "@testing-library/jest-dom/vitest";
22
import { createResource, Show } from "solid-js";
33
import { screen, render, waitForElementToBeRemoved } from "..";
44

src/__tests__/routes.tsx

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
import { createSignal, catchError } from "solid-js";
22
import { render } from "..";
3-
import { Routes, Route, useParams, useRouteData } from "@solidjs/router";
3+
import { Route, useParams } from "@solidjs/router";
44

55
describe("location option", () => {
66
const App = () => (
7-
<Routes>
7+
<>
88
<Route path="/ids/:id" component={() => <p>Id: {useParams()?.id}</p>} />
99
<Route path="/" component={() => <p>Start</p>} />
10-
</Routes>
10+
</>
1111
);
1212

13-
const App2 = () => <p>{useRouteData() || "loading"}</p>;
14-
1513
it("can render the main route", async () => {
1614
const { findByText } = render(() => <App />, { location: "/" });
1715
expect(await findByText("Start")).not.toBeFalsy();
@@ -22,26 +20,6 @@ describe("location option", () => {
2220
expect(await findByText("Id: 1234")).not.toBeFalsy();
2321
});
2422

25-
it("provides route data without a location", async () => {
26-
const { findByText } = render(() => <App2 />, { routeDataFunc: () => "works" });
27-
expect(await findByText("works")).not.toBeFalsy();
28-
});
29-
30-
it("provides route data with a location", async () => {
31-
const { findByText } = render(
32-
() => (
33-
<Routes>
34-
<Route path="/test" component={() => <App2 />} />
35-
</Routes>
36-
),
37-
{
38-
routeDataFunc: () => "good",
39-
location: "/test"
40-
}
41-
);
42-
expect(await findByText("good")).not.toBeFalsy();
43-
});
44-
4523
it("does not use a router without the location option", async () => {
4624
const NoRouter = () => {
4725
const [state, setState] = createSignal("loading");

src/index.ts

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,8 @@ import type {
2626

2727
/* istanbul ignore next */
2828
if (!process.env.STL_SKIP_AUTO_CLEANUP) {
29-
if (typeof afterEach === "function") {
30-
afterEach(async () => {
31-
await cleanup();
32-
});
33-
}
29+
//@ts-ignore
30+
if (typeof afterEach === "function") { afterEach(cleanup); }
3431
}
3532

3633
const mountedContainers = new Set<Ref>();
@@ -85,31 +82,17 @@ function render(ui: Ui, options: Options = {}): Result {
8582
: ui;
8683

8784
const routedUi: Ui =
88-
typeof location === "string" || typeof options.routeDataFunc === "function"
85+
typeof location === "string"
8986
? lazy(async () => {
9087
try {
91-
const { memoryIntegration, useNavigate, Router } = await import("@solidjs/router");
88+
const { createMemoryHistory, MemoryRouter } = await import("@solidjs/router");
89+
const history = createMemoryHistory();
90+
location && history.set({ value: location, scroll: false, replace: true });
9291
return {
9392
default: () =>
94-
createComponent(Router, {
95-
get children() {
96-
return [
97-
typeof location === "string"
98-
? createComponent(
99-
() => (
100-
useNavigate()(location as string, { replace: true, scroll: false }),
101-
null
102-
),
103-
{}
104-
)
105-
: null,
106-
createComponent(wrappedUi, {})
107-
];
108-
},
109-
data: options.routeDataFunc,
110-
get source() {
111-
return memoryIntegration();
112-
}
93+
createComponent(MemoryRouter, {
94+
history,
95+
get children() { return createComponent(wrappedUi, {}); }
11396
})
11497
};
11598
} catch (e: unknown) {
@@ -279,7 +262,11 @@ export function testEffect<T extends any = void>(
279262

280263
function cleanupAtContainer(ref: Ref) {
281264
const { container, dispose } = ref;
282-
dispose();
265+
if (typeof dispose === 'function') {
266+
dispose();
267+
} else {
268+
console.warn('solid-testing-library: dispose is not a function - maybe your tests include multiple solid versions!');
269+
}
283270

284271
if (container?.parentNode === document.body) {
285272
document.body.removeChild(container);

0 commit comments

Comments
 (0)