Skip to content

Commit 57664b3

Browse files
author
Alex Lohr
committed
- package updates
- new helper: testEffect
1 parent 9042fa0 commit 57664b3

5 files changed

Lines changed: 280 additions & 179 deletions

File tree

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,29 @@ expect(asFragment()).toBe(
135135
);
136136
```
137137

138+
⚠️ Solid.js manages side effects with different variants of `createEffect`. While you can use `waitFor` to test asynchronous effects, it uses polling instead of allowing Solid's reactivity to trigger the next step. In order to simplify testing those asynchronous effects, we have a `testEffect` helper that complements the hooks for directives and hooks:
139+
140+
```ts
141+
testEffect(fn: (done: (result: T) => void) => void, owner?: Owner): Promise<T>
142+
143+
// use it like this:
144+
test("testEffect allows testing an effect asynchronously", () => {
145+
const [value, setValue] = createSignal(0);
146+
return testEffect(done => createEffect((run: number = 0) => {
147+
if (run === 0) {
148+
expect(value()).toBe(0);
149+
setValue(1);
150+
} else if (run === 1) {
151+
expect(value()).toBe(1);
152+
done();
153+
}
154+
return run + 1;
155+
}));
156+
});
157+
```
158+
159+
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.
160+
138161
## Issues
139162

140163
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

0 commit comments

Comments
 (0)