Skip to content
This repository has been archived by the owner on Jun 21, 2023. It is now read-only.

Commit

Permalink
fix: next dynamic with jest (vercel#26614)
Browse files Browse the repository at this point in the history
Fixes vercel#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
  • Loading branch information
huozhi authored Jun 28, 2021
1 parent 39dd0f5 commit dd12f04
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/next/next-server/lib/loadable.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ function createLoadableComponent(loadFn, options) {
if (
!initialized &&
typeof window !== 'undefined' &&
typeof opts.webpack === 'function'
typeof opts.webpack === 'function' &&
typeof require.resolveWeak === 'function'
) {
const moduleIds = opts.webpack()
READY_INITIALIZERS.push((ids) => {
Expand Down
5 changes: 5 additions & 0 deletions test/unit/fixtures/stub-components/hello.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import React from 'react'

export default function Hello() {
return <div>hello</div>
}
16 changes: 16 additions & 0 deletions test/unit/next-dynamic.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* @jest-environment jsdom
*/
import React from 'react'
import { act, render } from '@testing-library/react'
import dynamic from 'next/dynamic'

describe('next/dynamic', () => {
it('test link with unmount', () => {
const App = dynamic(() => import('./fixtures/stub-components/hello'))
act(() => {
const { unmount } = render(<App />)
unmount()
})
})
})

0 comments on commit dd12f04

Please sign in to comment.