jest icon indicating copy to clipboard operation
jest copied to clipboard

"Cannot access 'aVariable' before initialization" with React

Open alexis-regnaud opened this issue 2 years ago • 5 comments

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 avatar May 17 '22 21:05 alexis-regnaud

Any update around this?

yasirrafiq786 avatar Sep 14 '22 22:09 yasirrafiq786

@kdy1

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

erfanium avatar Dec 21 '22 20:12 erfanium

@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

kdy1 avatar Dec 22 '22 01:12 kdy1

any update on fixing this issue?

bryanjtc avatar Dec 31 '22 02:12 bryanjtc

bump

C-o-d-e-C-o-w-b-o-y avatar Aug 28 '23 03:08 C-o-d-e-C-o-w-b-o-y