sbatson5 / firestore-jest-mock

Jest Helper library for mocking Cloud Firestore
https://www.npmjs.com/package/firestore-jest-mock
178 stars 59 forks source link

Separated file usage #73

Closed appsbytom closed 3 years ago

appsbytom commented 3 years ago

Summary

I have my firestore services split into their own files to make mocking the responses the UI needs easier. I have been trying to use this library but can't seem to get these separated files to use the mocked firebase. If I use the db calls directly as shown in the example tests the mocks are called but that doesn't test the files making the calls.

Relevant information

service.js

import firebase from 'firebase';

const firestore = firebase.firestore();

export const getData = () => firestore.collection('data').orderBy('value');

service.test.js

import { getData } from './service';

//works
it('should get ordered data', () => {
    const firebase = require('firebase');
    const db = firebase.firestore();

    db.collection('data').orderBy('value');

    expect(mockCollection).toHaveBeenNthCalledWith(1, 'data');
    expect(mockOrderBy).toHaveBeenNthCalledWith(1, 'value');
  });

//doesn't work
it('should get ordered data', () => {
    getData();

    expect(mockCollection).toHaveBeenNthCalledWith(1, 'data');
    expect(mockOrderBy).toHaveBeenNthCalledWith(1, 'value');
  });

Environment (if relevant)

N/A

appsbytom commented 3 years ago

Issues solved, switched to not use the import syntax after seeing the use of jest.doMock

it('should get ordered data', () => {
    const service = require('./service');

    service.getData();

    expect(mockCollection).toHaveBeenNthCalledWith(1, 'data');
    expect(mockOrderBy).toHaveBeenNthCalledWith(1, 'value');
  });