Skip to content

Commit 805c3f7

Browse files
authored
router: error message + docs (#45)
* router: error message + docs
1 parent bd3f76f commit 805c3f7

File tree

6 files changed

+85
-37
lines changed

6 files changed

+85
-37
lines changed

README.md

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ practices.</p>
99
1010
[![Coverage Status](https://coveralls.io/repos/github/ryansolid/solid-testing-library/badge.svg?branch=main)](https://coveralls.io/github/ryansolid/solid-testing-library?branch=main)
1111
[![NPM Version](https://img.shields.io/npm/v/@solidjs/testing-library.svg?style=flat)](https://www.npmjs.com/package/@solidjs/testing-library)
12-
[![](https://img.shields.io/npm/dm/solid-testing-library.svg?style=flat)](https://www.npmjs.com/package/solid-testing-library)
12+
[![NPM Downloads](https://img.shields.io/npm/dm/solid-testing-library.svg?style=flat)](https://www.npmjs.com/package/solid-testing-library)
1313
[![Discord](https://img.shields.io/discord/722131463138705510)](https://discord.com/invite/solidjs)
1414

1515
</div>
@@ -27,10 +27,11 @@ practices.</p>
2727

2828
---
2929

30+
3031
## The Problem
3132

32-
You want to write tests for your Solid components so that they avoid including implementation
33-
details, and are maintainable in the long run.
33+
You want to write tests for your Solid components so that they avoid including implementation details, and are maintainable in the long run.
34+
3435

3536
## The Solution
3637

@@ -52,10 +53,12 @@ If you using Jest we recommend using [solid-jest](https://github.com/solidjs/sol
5253
💡 If you are using Jest or vitest, you may also be interested in installing `@testing-library/jest-dom` so you can use
5354
[the custom jest matchers](https://github.com/testing-library/jest-dom).
5455

56+
5557
## Integration with Vite
5658

5759
A working Vite template setup with `solid-testing-library` and TypeScript support can be found [here](https://github.com/solidjs/solid-start/tree/main/examples/with-vitest).
5860

61+
5962
## Docs
6063

6164
See the [docs](https://testing-library.com/docs/preact-testing-library/intro) over at the Testing Library website.
@@ -175,6 +178,7 @@ test("testEffect allows testing an effect asynchronously", () => {
175178

176179
It allows running the effect inside a defined owner that is received as an optional second argument. This can be useful in combination with `renderHook`, which gives you an owner field in its result. The return value is a Promise with the value given to the `done()` callback. You can either await the result for further assertions or return it to your test runner.
177180

181+
178182
## Issues
179183

180184
If you find any issues, please [check on the issues page](https://github.com/solidjs/solid-testing-library/issues) if they are already known. If not, opening an issue will be much appreciated, even more so if it contains a
@@ -185,6 +189,38 @@ If you find any issues, please [check on the issues page](https://github.com/sol
185189

186190
If you think you can fix an issue yourself, feel free to [open a pull-request](https://github.com/solidjs/solid-testing-library/pulls). If functionality changes, please don't forget to add or adapt tests.
187191

192+
193+
### Known issues
194+
195+
If you are using [`vitest`](https://vitest.dev/), then tests might fail, because the packages `solid-js`, and `@solidjs/router` (if used) need to be loaded only once, and they could be loaded both through the internal `vite` server and through node. Typical bugs that happen because of this is that dispose is supposedly undefined, or the router could not be loaded.
196+
197+
There are three ways you could attempt to work around this problem. If they work depends on your version of `vitest`, which version of `node-js` and what package manager you use (some of them change the way node resolves modules):
198+
199+
```ts
200+
// this is inside your vite(st) config:
201+
{
202+
test: {
203+
deps: {
204+
// 1nd way: remove the @solidjs/router part if you do not use it:
205+
inline: [/solid-js/, /@solidjs\/router/],
206+
// 2st way
207+
registerNodeLoader: false,
208+
},
209+
// 3rd way: alias the resolution
210+
resolve: {
211+
alias: {
212+
"solid-js": "node_modules/solid-js/dist/dev.js",
213+
// only needed if the router is used:
214+
"@solidjs/router": "node_modules/@solidjs/router/index.jsx",
215+
},
216+
},
217+
},
218+
}
219+
```
220+
221+
At the moment of writing this, the 1st way seems to be the most reliable for the default solid template. Solid-start's vite plugin might need a different configuration.
222+
223+
188224
## Acknowledgement
189225

190226
Thanks goes to [Kent C. Dodds](https://kentcdodds.com/) and his colleagues for creating testing-library and to the creators of [preact-testing-library](https://github.com/testing-library/preact-testing-library).

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@solidjs/testing-library",
3-
"version": "0.8.3",
3+
"version": "0.8.4",
44
"description": "Simple and complete Solid testing utilities that encourage good testing practices.",
55
"type": "module",
66
"main": "./dist/index.cjs",

src/__tests__/basic.tsx

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -185,38 +185,49 @@ test("renderDirective works for directives with argument", () => {
185185

186186
test("testEffect allows testing an effect asynchronously", () => {
187187
const [value, setValue] = createSignal(0);
188-
return testEffect(done => createEffect((run: number = 0) => {
189-
if (run === 0) {
190-
expect(value()).toBe(0);
191-
setValue(1);
192-
} else if (run === 1) {
193-
expect(value()).toBe(1);
194-
done();
195-
}
196-
return run + 1;
197-
}));
188+
return testEffect(done =>
189+
createEffect((run: number = 0) => {
190+
if (run === 0) {
191+
expect(value()).toBe(0);
192+
setValue(1);
193+
} else if (run === 1) {
194+
expect(value()).toBe(1);
195+
done();
196+
}
197+
return run + 1;
198+
})
199+
);
198200
});
199201

200202
test("testEffect catches errors", () => {
201203
const [value, setValue] = createSignal<{ error: string } | null>({ error: "not yet" });
202-
return testEffect(done => createEffect((run: number = 0) => {
203-
value()!.error;
204-
if (run === 0) {
205-
setValue(null);
206-
} if (run === 1) {
207-
done();
208-
}
209-
return run + 1;
210-
}))
211-
.then(() => { throw new Error("Error swallowed by testEffect!")})
204+
return testEffect(done =>
205+
createEffect((run: number = 0) => {
206+
value()!.error;
207+
if (run === 0) {
208+
setValue(null);
209+
}
210+
if (run === 1) {
211+
done();
212+
}
213+
return run + 1;
214+
})
215+
)
216+
.then(() => {
217+
throw new Error("Error swallowed by testEffect!");
218+
})
212219
.catch((e: Error) => expect(e.name).toBe("TypeError"));
213220
});
214221

215222
test("testEffect runs with owner", () => {
216-
const [owner, dispose] = createRoot((dispose) => [getOwner(), dispose]);
217-
return testEffect(done => createEffect(() => {
218-
expect(getOwner()!.owner).toBe(owner);
219-
dispose();
220-
done();
221-
}), owner!);
223+
const [owner, dispose] = createRoot(dispose => [getOwner(), dispose]);
224+
return testEffect(
225+
done =>
226+
createEffect(() => {
227+
expect(getOwner()!.owner).toBe(owner);
228+
dispose();
229+
done();
230+
}),
231+
owner!
232+
);
222233
});

src/__tests__/no-router.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import { render } from "..";
22

3-
describe("router functionality without a router", () => {
3+
describe("router functionality without a router", () => {
44
it("emits a helpful console.error message if you attempt to use the router without having it installed", async () => {
5-
vi.mock('@solidjs/router', () => ({ default: {} }));
5+
vi.mock("@solidjs/router", () => ({ default: {} }));
66
const errorMock = vi.spyOn(console, "error");
77
let errorMessage = "";
8-
errorMock.mockImplementation((message) => { errorMessage = message; });
8+
errorMock.mockImplementation(message => {
9+
errorMessage = message;
10+
});
911
const { findByText } = render(() => <p>Anyhoo</p>, { location: "/" });
1012
await findByText("Anyhoo");
1113
expect(errorMessage).toMatch("@solidjs/router");
12-
});
14+
});
1315
});
14-

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ function render(ui: Ui, options: Options = {}): Result {
109109
};
110110
} catch (e) {
111111
console.error(
112-
"It appears you want to use the location option without having @solidjs/router installed."
112+
`Error attempting to initialize @solidjs/router:\n"${e.message || "unknown error"}"`
113113
);
114114
return { default: () => createComponent(wrappedUi, {}) };
115115
}

vitest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ export default defineConfig({
1818
deps: { registerNodeLoader: false }
1919
},
2020
resolve: {
21-
conditions: ["browser", "development"],
21+
conditions: ["browser", "development"]
2222
}
2323
});

0 commit comments

Comments
 (0)