Skip to content
This repository has been archived by the owner on Jan 16, 2025. It is now read-only.

"Cannot access 'aVariable' before initialization" with React #97

Open
alexis-regnaud opened this issue May 17, 2022 · 5 comments
Open

Comments

@alexis-regnaud
Copy link

alexis-regnaud commented May 17, 2022

On a React project, I switched ts-jest + babel-jest to @swc/jest
Few tests are now failing with the same type of error : Cannot access 'aVariable' before initialization which always happen in a specific situation.

The error is thrown when the variable is defined further in the component, but used before by a React function :

function AComponent() {
  const aFunction = () => { //Case 1
    return aVariable;
  };

  const [selected, setSelected] = useState(() => { //Case 2
    return aVariable;
  });

  const otherTest = useMemo(() => aVariable, []); //Case 3

  const aVariable = 'Ceci est un test';
}
  • Case 1 work
  • Case 2 fail
  • Case 3 fail
@alexis-regnaud alexis-regnaud changed the title Cannot access 'aVariable' before initialization "Cannot access 'aVariable' before initialization" with React May 17, 2022
@yasirrafiq786
Copy link

Any update around this?

@erfanium
Copy link

@kdy1

I have time to work on it.
Any hint about this issue and where should I start?

@kdy1
Copy link
Member

kdy1 commented Dec 22, 2022

@erfanium You can add tests to https://github.com/swc-project/swc/tree/ebebd608750bf63d6f715350835a1a5349efdb68/crates/swc/tests/fixture/jest and do UPDATE=1 cargo test to investigate

@bryanjtc
Copy link

any update on fixing this issue?

@C-o-d-e-C-o-w-b-o-y
Copy link

bump

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

6 participants