Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make esm default interpolation work with jest mock #36877

Merged
merged 3 commits into from May 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/next/taskfile-swc.js
Expand Up @@ -118,6 +118,7 @@ module.exports = function (task) {
if (interopClientDefaultExport) {
output.code += `
if (typeof exports.default === 'function' || (typeof exports.default === 'object' && exports.default !== null)) {
Object.defineProperty(exports.default, '__esModule', { value: true });
Object.assign(exports.default, exports);
module.exports = exports.default;
}
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>
}