Skip to content

Commit 684f710

Browse files
authored
Merge pull request #47 from trurl-master/config
Improve support of other testing frameworks
2 parents 6c018a5 + f5bb8bd commit 684f710

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+4860
-1672
lines changed

.eslintrc.cjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ module.exports = {
1818
],
1919
'react/jsx-uses-react': 'off',
2020
'react/react-in-jsx-scope': 'off',
21+
'@typescript-eslint/ban-ts-comment': 'off',
2122
},
2223
};

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
runs-on: ${{ matrix.os }}
1010
strategy:
1111
matrix:
12-
node: ['14.x', '16.x', '18.x']
12+
node: ['16.x', '18.x', '20.x']
1313
os: [ubuntu-latest, windows-latest, macOS-latest]
1414

1515
steps:

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2021 Ivan Galiatin
3+
Copyright (c) 2023 Ivan Galiatin
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,68 @@ A set of tools for emulating browser behavior in jsdom environment
1414

1515
[![Stand With Ukraine](https://raw.githubusercontent.com/vshymanskyy/StandWithUkraine/main/banner2-direct.svg)](https://stand-with-ukraine.pp.ua)
1616

17+
### Mocks
18+
19+
[matchMedia](#mock-viewport)
20+
[Intersection Observer](#mock-intersectionobserver)
21+
[Resize Observer](#mock-resizeobserver)
22+
[Web Animations API](#mock-web-animations-api)
23+
1724
## Installation
1825

1926
```sh
2027
npm i -D jsdom-testing-mocks
2128
```
2229

23-
or
24-
2530
```sh
2631
yarn add -D jsdom-testing-mocks
2732
```
2833

29-
## Mocks
34+
## Setup
3035

31-
[matchMedia](#mock-viewport),
32-
[Intersection Observer](#mock-intersectionobserver),
33-
[Resize Observer](#mock-resizeobserver),
34-
[Web Animations API](#mock-web-animations-api)
36+
### With `react`
37+
38+
To avoid having to wrap everything in `act` calls, you can pass `act` to `configMocks`:
39+
40+
```jsx
41+
import { configMocks } from 'jsdom-testing-mocks';
42+
import { act } from '...';
43+
44+
configMocks({ act });
45+
```
46+
47+
It can be done in a setup file, or in a test file, before rendering the component.
48+
49+
### With `vitest`
50+
51+
Some mocks require lifecycle hooks to be defined on the global object. To make it work with vitest, you need to [enable globals in your config](https://vitest.dev/config/#globals). If you don't want to do that you can pass it manually using `configMocks`.
52+
53+
Also, if you're using fake timers, at the time of writing this, vitest doesn't enable faking `performance.now`, `requestAnimationFrame` and `cancelAnimationFrame` by default, so you need to do it manually:
54+
55+
```js
56+
vi.useFakeTimers({
57+
toFake: [
58+
// vitests defaults
59+
'setTimeout',
60+
'clearTimeout',
61+
'setInterval',
62+
'clearInterval',
63+
'setImmediate',
64+
'clearImmediate',
65+
'Date',
66+
// required for mocks
67+
'performance',
68+
'requestAnimationFrame',
69+
'cancelAnimationFrame',
70+
],
71+
});
72+
```
73+
74+
[vitest defaults](https://github.com/vitest-dev/vitest/blob/190c25f902a8c32859c9f62407040dedf5a72cb9/packages/vitest/src/defaults.ts)
75+
76+
## Testing framework support
77+
78+
We aim to support all major testing frameworks that support jsdom. Internally, there are no dependencies on any of them, so it's likely that it will work out of the box. Currently tested and confirmed to work with [jest](https://jestjs.io/) and [vitest](https://vitest.dev/). If you encounter any problems with other testing frameworks, please open an issue.
3579

3680
## Mock viewport
3781

@@ -362,11 +406,11 @@ it('adds an element into the dom and fades it in', async () => {
362406
});
363407
```
364408

365-
### Using with fake timers
409+
## Using with fake timers
366410

367411
It's perfectly usable with fake timers, except for the [issue with promises](https://github.com/facebook/jest/issues/2157). Also note that you would need to manually advance timers by the duration of the animation taking frame duration (which currently is set to 16ms in `jest`/`sinon.js`) into account. So if you, say, have an animation with a duration of `300ms`, you will need to advance your timers by the value that is at least the closest multiple of the frame duration, which in this case is `304ms` (`19` frames \* `16ms`). Otherwise the last frame may not fire and the animation won't finish.
368412

369-
### Current issues
413+
## Current issues
370414

371415
- Needs more tests
372416

@@ -389,5 +433,4 @@ It's perfectly usable with fake timers, except for the [issue with promises](htt
389433
[twitter-badge]: https://img.shields.io/twitter/url?style=social&url=https%3A%2F%2Fgithub.com%2Ftrurl-master%2Fjsdom-testing-mocks
390434
[twitter]: https://twitter.com/intent/tweet?text=Check%20out%20jsdom-testing-mocks%20by%20@ivangaliatin%20https%3A%2F%2Fgithub.com%2Ftrurl-master%2Fjsdom-testing-mocks
391435

392-
393436
<!-- prettier-ignore-end -->

examples/global.d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export {};
2+
3+
declare global {
4+
var runner: {
5+
name: 'vi' | 'jest';
6+
useFakeTimers: () => void;
7+
useRealTimers: () => void;
8+
advanceTimersByTime: (time: number) => Promise<void>;
9+
fn: () => jest.Mock<any, any>;
10+
};
11+
}

0 commit comments

Comments
 (0)