Skip to content

Commit d73a536

Browse files
committed
v0.1.0
1 parent 365c1de commit d73a536

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

README.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,47 @@
33
Unlike [react-loadable](https://github.com/jamiebuilds/react-loadable), the
44
[dynamic import](https://github.com/zeit/next.js#dynamic-import) support
55
offered by Next.js does not expose a way to preload the dynamically imported
6-
components in Jest’s environment (which mimics a DOM environment).
6+
components in Jest’s environment (which mimics a DOM environment). Even if you
7+
get a handle on the `Loadable` component bundled with Next.js, it skips queuing
8+
up the components in DOM-like environments. Bummer!
79

8-
This module can be imported in your tests or setup file to make sure any
10+
This module can be imported in your test or setup files to make sure any
911
component created with `next/dynamic` is added to a queue, which can then be
1012
flushed with the exported `preloadAll` function.
1113

14+
Using this, your component tests (including snapshots) can render the full
15+
component tree instead of the loading placeholders.
16+
17+
## Setup
18+
19+
In order to transform `import()` successfully in Jest’s environment, you must
20+
use a different transform than the one provided by `next/babel` (which expects
21+
to be built with webpack).
22+
23+
Both of these work fine:
24+
25+
- [babel-plugin-dynamic-import-node](https://www.npmjs.com/package/babel-plugin-dynamic-import-node)
26+
- [babel-plugin-transform-dynamic-import](https://www.npmjs.com/package/babel-plugin-transform-dynamic-import)
27+
28+
```json
29+
{
30+
"plugins": ["babel-plugin-dynamic-import-node]"]
31+
}
32+
```
33+
1234
## Usage
1335

1436
```js
1537
// This will mock `next/dynamic`, so make sure to import it before any of your
1638
// components.
1739
import preloadAll from "jest-next-dynamic";
40+
// This component can have dynamic imports anywhere in its rendered tree,
41+
// including nested dynamic imports.
42+
import SomeComponent from "./SomeComponent";
1843

1944
beforeAll(async () => {
2045
await preloadAll();
2146
});
47+
48+
// Your tests here!
2249
```

0 commit comments

Comments
 (0)