Converting Jasmine SpyObj Mock to Jest Mock

Viewed 667

I'm attempting to migrate my angular project tests from Jasmine to Jest. While attempting to mock the Angularfire firestore class using the TS-Jest mocked method I can't seem to get this right.

An example of how I have achieved this many times using jasmine's createSpyObj method.

describe('My Example', () => {
    let firestoreSpy: SpyObj<AngularFirestore>;
    let firestoreCollectionSpy: SpyObj<AngularFirestoreCollection>;

    beforeEach(() => {
        firestoreSpy = jasmine.createSpyObj('firestoreSpy', ['collection']);
        firestoreCollectionSpy= jasmine.createSpyObj('firestoreCollectionSpy', ['snapshotChanges']);
        firestoreSpy.collection.and.returnValue(firestoreCollectionSpy);
    });

    it('My Test', () => {
        // Set the return value of the snapshotChanges method
        collectionSpy.snapshotChanges.and.returnValue(of());
    };
});

Trying to implement this using jest mocks I have tried.

import { mocked } from 'ts-jest/utils';
import { AngularFirestore } from '@angular/fire/firestore';
import { of } from 'rxjs';

describe('Jest Tests', () => {
  let firestoreMock; // I Want to be able to type this as well

  beforeEach(() => {
    jest.mock('@angular/fire/firestore');
    firestoreMock = mocked(AngularFirestore, true);
  });

  it('My Test', () => {
    firestoreMock.collection.mockImplementation(() => {
      return {
        snapshotChanges: jest.fn().mockReturnValue(of()),
      };
    });
  });
});

I've been getting an undefined value for collection which is very much a method of the class AngularFirestore

TypeError: Cannot read property 'mockImplementation' of undefined

Any insights that can get me on the right track would be appreciated.

0 Answers
Related