Skip to content

Commit

Permalink
Make esm default interpolation work with jest mock
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn committed May 13, 2022
1 parent 092346d commit 35743ca
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/next/taskfile-swc.js
Expand Up @@ -120,6 +120,7 @@ module.exports = function (task) {
if (typeof exports.default === 'function' || (typeof exports.default === 'object' && exports.default !== null)) {
Object.assign(exports.default, exports);
module.exports = exports.default;
Object.defineProperty(module.exports, '__esModule', { value: true });
}
`
}
Expand Down
14 changes: 14 additions & 0 deletions test/unit/esm-interpolate/esm-interpolate.test.tsx
@@ -0,0 +1,14 @@
import React from 'react'
import { renderToString } from 'react-dom/server'
import * as nextRouter from 'next/router'

import { Foo } from './fixture'

// @ts-expect-error
jest.spyOn(nextRouter, 'useRouter').mockReturnValue({
pathname: 'Hello',
})

test('mock the interpolated modules should work', () => {
expect(renderToString(<Foo />)).toBe(`<div>Hello</div>`)
})
8 changes: 8 additions & 0 deletions test/unit/esm-interpolate/fixture.tsx
@@ -0,0 +1,8 @@
import React from 'react'
import { useRouter } from 'next/router'

export const Foo = () => {
const router = useRouter()

return <div>{router.pathname}</div>
}

0 comments on commit 35743ca

Please sign in to comment.