Skip to content

Commit 9367ac7

Browse files
committed
Improve support of other testing frameworks
1 parent 6c018a5 commit 9367ac7

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

+4855
-1665
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: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,52 @@ yarn add -D jsdom-testing-mocks
3333
[Resize Observer](#mock-resizeobserver),
3434
[Web Animations API](#mock-web-animations-api)
3535

36+
## Testing framework support
37+
38+
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.
39+
40+
## Usage notes
41+
42+
### With `react`
43+
44+
To avoid having to wrap everything in `act` calls, you can pass `act` to `configMocks`:
45+
46+
```jsx
47+
import { configMocks } from 'jsdom-testing-mocks';
48+
import { act } from '...';
49+
50+
configMocks({ act });
51+
```
52+
53+
It can be done in a setup file, or in a test file, before rendering the component.
54+
55+
### With `vitest`
56+
57+
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`.
58+
59+
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:
60+
61+
```js
62+
vi.useFakeTimers({
63+
toFake: [
64+
// vitests defaults
65+
'setTimeout',
66+
'clearTimeout',
67+
'setInterval',
68+
'clearInterval',
69+
'setImmediate',
70+
'clearImmediate',
71+
'Date',
72+
// required for mocks
73+
'performance.now',
74+
'requestAnimationFrame',
75+
'cancelAnimationFrame',
76+
],
77+
});
78+
```
79+
80+
[vitest defaults](https://github.com/vitest-dev/vitest/blob/190c25f902a8c32859c9f62407040dedf5a72cb9/packages/vitest/src/defaults.ts)
81+
3682
## Mock viewport
3783

3884
Mocks `matchMedia`, allows testing of component's behavior depending on the viewport description (supports all of the [Media Features](http://www.w3.org/TR/css3-mediaqueries/#media1)). `mockViewport` must be called before rendering the component
@@ -362,11 +408,11 @@ it('adds an element into the dom and fades it in', async () => {
362408
});
363409
```
364410

365-
### Using with fake timers
411+
## Using with fake timers
366412

367413
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.
368414

369-
### Current issues
415+
## Current issues
370416

371417
- Needs more tests
372418

@@ -389,5 +435,4 @@ It's perfectly usable with fake timers, except for the [issue with promises](htt
389435
[twitter-badge]: https://img.shields.io/twitter/url?style=social&url=https%3A%2F%2Fgithub.com%2Ftrurl-master%2Fjsdom-testing-mocks
390436
[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
391437

392-
393438
<!-- 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)