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
router client file is transpiled incorrectly #36794
Comments
Actually it will keep everything on Since we are discussing |
It doesn't. Look closely at the new code if (typeof exports.default === 'function' || (typeof exports.default === 'object' && exports.default !== null)) {
Object.assign(exports.default, exports);
module.exports = exports.default;
} The first line in the That's why import * as routerModule from 'next/router';
routerModule.useRouter // is now null but import routerModule from 'next/router';
routerModule.useRouter // works but shouldn't This does not match the esm structure of this module, causing code to break. As for configuration, we have this in our Jest config transform: {
'^.+\\.(js|jsx|mjs|cjs|ts|tsx)$': require.resolve(
'@mrkt/jest/transforms/babel',
),
'^.+\\.css$': require.resolve('@mrkt/jest/transforms/css'),
'^(?!.*\\.(js|jsx|mjs|cjs|ts|tsx|css|json)$)': require.resolve(
'@mrkt/jest/transforms/file',
),
}, and we have babel-jest version |
The reason why import * as routerModule from 'next/router';
routerModule.useRouter // is now null is because of the transformer, not the code itself. If you are using native const routerModule = require('next/router');
console.log(routerModule.createRouter) // [Function: createRouter] const { createRouter } = require('next/router');
console.log(createRouter) // [Function: createRouter] |
I understand it's the transformer. That's what this entire issue is about. The transformer is breaking Jest code that babel-jest is transpiling (and probably other transpilation mechanisms). The problem is, I'm trying to access useRouter in a way that will allow me to mock it with Jest. A very common way is like this: import * as router from 'next/router';
jest.spyOn(router, 'useRouter').mockReturnValue(({
pathname: '/artist/[artistId]/audience',
push: pushMockFn,
}; see comment here: #7479 (comment) This transformer is fundamentally breaking the ability for that to work, and for no ostensible reason.
|
I am running into a similar problem. As of 12.1.6 all of the tests I have which mock |
@Bonitis in a pinch, you can change your test code to
and then mock The only real solution here is for Next to just remove it. |
@arackaf unfortunately for now I have had to roll back to |
I've created a stand-alone repro: clone this repo, The test file has a commented out line that causes the test to pass, even though it should't. Edit: link is here: |
@arackaf looks like your link didn't get copy/pasta'd - this one I assume? https://github.com/arackaf/next-repro |
@leerob yes indeed - thank you! |
Might be related https://twitter.com/gearsdigital/status/1524852522443325466 |
@arackaf thanks for your repro, I've found the root cause of this issue, it's because the mocked methods on the original @leerob The error thrown in cypress-io/cypress#19382 is |
@Brooooooklyn is there no way to just remove this code? if (typeof exports.default === 'function' || (typeof exports.default === 'object' && exports.default !== null)) {
Object.assign(exports.default, exports);
module.exports = exports.default;
} that was the original cause of the mismatch between what’s exported from the module, and what’s on the actual module.exports object that jest is processing |
No, these codes are intended to fix #34412 . It's indeed an ugly workaround that should be removed in the future, but I think it's a straightforward way to make |
@Brooooooklyn ohhhh I see. The disastrous Node / esm interop debacle continues. Thanks for the context. |
Hi, this has been updated in |
This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Verify canary release
Provide environment information
What browser are you using? (if relevant)
n/a
How are you deploying your application? (if relevant)
n/a
Describe the Bug
The dist/client/router.js file had this code added in version 12.1.5
This essentially erases all named exports from the CJS exports object, and instead hangs them all on the default export. This breaks Jest code that's mocking the useRouter hook.
now breaks, since
routerModule.useRouter
is null, requiring insteadThis fixes the test, but is wrong. useRouter is not a property on the default export (or shouldn't be) but is rather a named export in that module.
Expected Behavior
CJS exports should match ESM.
To Reproduce
See above
The text was updated successfully, but these errors were encountered: