Skip to content

Commit c5751fa

Browse files
authored
fix: next dynamic with jest (#26614)
Fixes #19862 Avoid executing `webpack` property on `loadableGenerated` of loadable component compiled from `next/dynamic` when `require.resolveWeak` is unavailable due to jest runtime missing `require.resolveWeak`. ## Bug - [x] Related issues linked using `fixes #number` - [x] unit tests added
1 parent c2f0653 commit c5751fa

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

packages/next/next-server/lib/loadable.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ function createLoadableComponent(loadFn, options) {
9494
if (
9595
!initialized &&
9696
typeof window !== 'undefined' &&
97-
typeof opts.webpack === 'function'
97+
typeof opts.webpack === 'function' &&
98+
typeof require.resolveWeak === 'function'
9899
) {
99100
const moduleIds = opts.webpack()
100101
READY_INITIALIZERS.push((ids) => {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import React from 'react'
2+
3+
export default function Hello() {
4+
return <div>hello</div>
5+
}

test/unit/next-dynamic.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @jest-environment jsdom
3+
*/
4+
import React from 'react'
5+
import { act, render } from '@testing-library/react'
6+
import dynamic from 'next/dynamic'
7+
8+
describe('next/dynamic', () => {
9+
it('test link with unmount', () => {
10+
const App = dynamic(() => import('./fixtures/stub-components/hello'))
11+
act(() => {
12+
const { unmount } = render(<App />)
13+
unmount()
14+
})
15+
})
16+
})

0 commit comments

Comments
 (0)