skovhus / jest-codemods

Codemods for migrating to Jest https://github.com/facebook/jest 👾
MIT License
877 stars 81 forks source link

`jasmine-globals`: unable to transform re-used `spyOn` that uses `.and.callThrough()` #579

Closed puglyfe closed 3 months ago

puglyfe commented 3 months ago

Given a test suite that re-uses a jasmine spy and modifies the spy behavior:

import SomeLib from 'some-lib';

describe('My use case', () => {
  let mySpy: jasmine.Spy;
  beforeEach(() => {
    mySpy = spyOn(SomeLib, 'doTheThing');
  });

  // test cases with default `callFake` behavior...

  it('does something with modified spy behavior', () => {
    mySpy.and.callThrough();
    // ...
  });
});

The jasmine-globals transformer will fail with a rather crpytic error Transformation error (Cannot read property 'length' of undefined), with the stack trace pointing to some recast internals.

The easiest reproduction is to add existingSpy.and.callThrough(); -> existingSpy; to this test case.

I will work on a fix.