Cannot access ... before initialization
See original GitHub issueVerify canary release
- I verified that the issue exists in the latest Next.js canary release
Provide environment information
yarn next info
Operating System:
Platform: darwin
Arch: arm64
Version: Darwin Kernel Version 21.6.0: Sat Jun 18 17:07:22 PDT 2022; root:xnu-8020.140.41~1/RELEASE_ARM64_T6000
Binaries:
Node: 16.16.0
npm: 8.11.0
Yarn: 3.2.0
pnpm: N/A
Relevant packages:
next: 12.2.4
eslint-config-next: 12.2.0
react: 17.0.2
react-dom: 17.0.2
warn - Latest canary version not detected, detected: "12.2.4", newest: "12.2.6-canary.5".
Please try the latest canary version (`npm install next@canary`) to confirm the issue still exists before creating a new issue.
Read more - https://nextjs.org/docs/messages/opening-an-issue
What browser are you using? (if relevant)
Chrome
How are you deploying your application? (if relevant)
yarn jest
Describe the Bug
Between 12.2.0 and 12.2.1 a big chunk of our unit tests broke. Any test using jest.requireActual('')
to not mock some of the modules fails with an error like this:
ReferenceError: Cannot access 'buildError' before initialization
81 |
> 82 | export const buildError = {
I tried to debug this, but sadly am not able to view the swc compiled source files.
Additionally all tests using jest.mock('next/router')
have to be changed to:
jest.mock('next/router', () => ({
useRouter: jest.fn(),
});
Because that now yielded the following error:
No router instance found.
You should only use "next/router" on the client side of your app.
Expected Behavior
jest.requireActual('')
to keep working.
Link to reproduction
Tried to reproduce in simple repo but wasn’t able to get the same behaviour
To Reproduce
This part of the test that breaks:
jest.mock('./bla', () => ({
...jest.requireActual('./bla'),
useBla: jest.fn(),
});
describe('SomeComponent', () => {
const getComponent = (): ShallowWrapper => shallow(<SomeComponent />);
it('should bla', () => {
const component = getComponent();
expect(component.html()).toBeNull();
});
});
export const SomeComponent: React.FC = () => {
const bla = useBla();
return bla;
};
export const buildError = {
build: () => 'test',
};
export const useBla = () => 'bla';
Compiler options:
compiler: {
styledComponents: {
cssProp: false,
},
emotion:
// Don't compile with jest otherwise all emotion components become EmotionCSSPropInternal
phase === PHASE_TEST
? null
: {
sourceMap: true,
autoLabel: 'dev-only',
labelFormat: 'local',
},
removeConsole:
phase === PHASE_PRODUCTION_BUILD || phase === PHASE_PRODUCTION_SERVER,
},
Issue Analytics
- State:
- Created a year ago
- Reactions:5
- Comments:12 (11 by maintainers)
Top Results From Across the Web
ReferenceError: can't access lexical declaration 'X' before ...
The JavaScript exception "can't access lexical declaration `variable' before initialization" occurs when a lexical variable was accessed ...
Read more >[SOLVED] Cannot Access Before Initialization Error in JavaScript
The “cannot access before initialization” reference error occurs in JavaScript when you try to access a variable before it is declared with ...
Read more >ReferenceError: Cannot access before initialization in JS
The "Cannot access before initialization" error occurs when a variable declared using let or const is accessed before it was initialized in the...
Read more >Cannot access 'variable_name' before initialization
When you assign variables using $: you cannot assign them as part of other variables declared using let , const , or var...
Read more >Cannot access 'XXX' before initialization - r/angular ... - Reddit
Uncaught ReferenceError : Cannot access 'XXX' before initialization. Hello I get this error message when I try to import a component into another ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@balazsorban44 any idea on this issue?
@balazsorban44 this release broke it: https://github.com/vercel/next.js/releases/tag/v12.2.3-canary.1. 12.2.3-canary.0 still worked.