question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot access ... before initialization

See original GitHub issue

Verify 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:open
  • Created a year ago
  • Reactions:5
  • Comments:12 (11 by maintainers)

github_iconTop GitHub Comments

2reactions
PepijnSenderscommented, Sep 9, 2022

@balazsorban44 any idea on this issue?

1reaction
PepijnSenderscommented, Aug 26, 2022

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found