diff --git a/__tests__/full-setup-library-firestore.test.js b/__tests__/full-setup-library-firestore.test.js index e105d56..9c03ad3 100644 --- a/__tests__/full-setup-library-firestore.test.js +++ b/__tests__/full-setup-library-firestore.test.js @@ -1,9 +1,7 @@ import * as FirestoreMock from 'firestore-jest-mock'; import { Timestamp } from '../mocks/timestamp'; -import { mocks } from '../mocks/firestore'; - -const { +import { mockGet, mockSelect, mockAdd, @@ -20,7 +18,7 @@ const { mockOnSnapShot, mockListCollections, mockTimestampNow, -} = mocks; +} from '../mocks/firestore'; describe.each([ { library: '@google-cloud/firestore', mockFunction: 'mockGoogleCloudFirestore' }, diff --git a/__tests__/full-setup.test.js b/__tests__/full-setup.test.js index 917360a..82e63f4 100644 --- a/__tests__/full-setup.test.js +++ b/__tests__/full-setup.test.js @@ -1,40 +1,36 @@ -describe.each` - filters - ${true} - ${false} -`('we can start a firebase application (query filters: $filters)', ({ filters }) => { - // We call `require` inside of a parameterized `describe` so we get - // a fresh mocked Firebase to test cases with query filters turned on and off - +import { mockFirebase } from 'firestore-jest-mock'; +import { Timestamp } from '../mocks/timestamp'; + +import { mockInitializeApp } from '../mocks/firebase'; + +import { + mockGet, + mockSelect, + mockAdd, + mockSet, + mockUpdate, + mockWhere, + mockCollectionGroup, + mockBatch, + mockBatchCommit, + mockBatchDelete, + mockBatchUpdate, + mockBatchSet, + mockSettings, + mockOnSnapShot, + mockUseEmulator, + mockDoc, + mockCollection, + mockWithConverter, + FakeFirestore, + mockQueryOnSnapshot, + mockTimestampNow, +} from '../mocks/firestore'; + +describe('we can start a firebase application (query filters: $filters)', () => { jest.resetModules(); - const { mockFirebase } = require('firestore-jest-mock'); - const { mockInitializeApp } = require('../mocks/firebase'); - const flushPromises = () => new Promise(setImmediate); - const { Timestamp } = require('../mocks/timestamp'); - const { - mockGet, - mockSelect, - mockAdd, - mockSet, - mockUpdate, - mockWhere, - mockCollectionGroup, - mockBatch, - mockBatchCommit, - mockBatchDelete, - mockBatchUpdate, - mockBatchSet, - mockSettings, - mockOnSnapShot, - mockUseEmulator, - mockDoc, - mockCollection, - mockWithConverter, - FakeFirestore, - mockQueryOnSnapshot, - mockTimestampNow, - } = require('../mocks/firestore'); + let firebase; mockFirebase( { @@ -60,14 +56,13 @@ describe.each` ], }, }, - { simulateQueryFilters: filters }, + { simulateQueryFilters: false }, ); - /** @type {import('firebase').default} */ - const firebase = require('firebase'); - - beforeEach(() => { + beforeEach(async () => { jest.resetAllMocks(); + firebase = await import('firebase'); + firebase.initializeApp({ apiKey: '### FIREBASE API KEY ###', authDomain: '### FIREBASE AUTH DOMAIN ###', @@ -75,7 +70,9 @@ describe.each` }); }); - afterEach(() => mockTimestampNow.mockClear()); + afterEach(() => { + mockTimestampNow.mockClear(); + }); test('We can start an application', async () => { const db = firebase.firestore(); @@ -226,9 +223,8 @@ describe.each` expect(mockCollectionGroup).toHaveBeenCalledWith('cities'); expect(mockGet).toHaveBeenCalledTimes(1); expect(mockWhere).toHaveBeenCalledWith('country', '==', 'USA'); - expect(querySnapshot.forEach).toBeTruthy(); - expect(querySnapshot.docs.length).toBe(filters ? 3 : 4); + expect(querySnapshot.docs.length).toBe(4); expect(querySnapshot.size).toBe(querySnapshot.docs.length); querySnapshot.forEach(doc => { diff --git a/__tests__/mock-fieldvalue.test.js b/__tests__/mock-fieldvalue.test.js index 0542e69..15b4cfa 100644 --- a/__tests__/mock-fieldvalue.test.js +++ b/__tests__/mock-fieldvalue.test.js @@ -1,17 +1,18 @@ -const { mockFirebase } = require('firestore-jest-mock'); +import { mockFirebase } from 'firestore-jest-mock'; mockFirebase({ database: {} }); -const firebase = require('firebase'); -const { +import { mockArrayRemoveFieldValue, mockArrayUnionFieldValue, mockDeleteFieldValue, mockIncrementFieldValue, mockServerTimestampFieldValue, -} = require('../mocks/firestore'); +} from '../mocks/firestore'; describe('Single values transformed by field sentinels', () => { - beforeEach(() => { + let firebase; + beforeEach(async () => { + firebase = await import('firebase'); jest.resetModules(); jest.clearAllMocks(); }); diff --git a/__tests__/mock-firestore-mutable.test.js b/__tests__/mock-firestore-mutable.test.js index 51f1efa..7594dcd 100644 --- a/__tests__/mock-firestore-mutable.test.js +++ b/__tests__/mock-firestore-mutable.test.js @@ -1,5 +1,5 @@ -const { FakeFirestore } = require('firestore-jest-mock'); -const { mockCollection, mockDoc } = require('firestore-jest-mock/mocks/firestore'); +import { FakeFirestore } from 'firestore-jest-mock'; +import { mockCollection, mockDoc } from 'firestore-jest-mock/mocks/firestore'; describe('database mutations', () => { beforeEach(() => { diff --git a/__tests__/mock-firestore.test.js b/__tests__/mock-firestore.test.js index d346b66..6edfcf3 100644 --- a/__tests__/mock-firestore.test.js +++ b/__tests__/mock-firestore.test.js @@ -1,5 +1,5 @@ -const { FakeFirestore } = require('firestore-jest-mock'); -const { mockCollection, mockDoc } = require('firestore-jest-mock/mocks/firestore'); +import { FakeFirestore } from 'firestore-jest-mock'; +import { mockCollection, mockDoc } from 'firestore-jest-mock/mocks/firestore'; describe('Queries', () => { beforeEach(() => { diff --git a/__tests__/mock-timestamp.test.js b/__tests__/mock-timestamp.test.js index 076ed81..4d468e5 100644 --- a/__tests__/mock-timestamp.test.js +++ b/__tests__/mock-timestamp.test.js @@ -1,10 +1,10 @@ -const { FakeFirestore } = require('firestore-jest-mock'); -const { +import { FakeFirestore } from 'firestore-jest-mock'; +import { mockTimestampToDate, mockTimestampToMillis, mockTimestampNow, -} = require('firestore-jest-mock/mocks/firestore'); -const admin = require('firebase-admin'); +} from 'firestore-jest-mock/mocks/firestore'; +import admin from 'firebase-admin'; const ref = admin.firestore.Timestamp; describe('Timestamp mock', () => { diff --git a/__tests__/options.test.js b/__tests__/options.test.js index d8d2cc5..54e69a6 100644 --- a/__tests__/options.test.js +++ b/__tests__/options.test.js @@ -1,5 +1,5 @@ -const { FakeFirestore } = require('firestore-jest-mock'); -const { mockCollection, mockDoc } = require('firestore-jest-mock/mocks/firestore'); +import { FakeFirestore } from 'firestore-jest-mock'; +import { mockCollection, mockDoc } from 'firestore-jest-mock/mocks/firestore'; describe('Firestore options', () => { beforeEach(() => { diff --git a/__tests__/path.test.js b/__tests__/path.test.js index 8b8ccf2..d961017 100644 --- a/__tests__/path.test.js +++ b/__tests__/path.test.js @@ -1,9 +1,14 @@ -const { mockFirebase } = require('firestore-jest-mock'); +import { mockFirebase } from 'firestore-jest-mock'; mockFirebase({ database: {} }); -const firebase = require('firebase'); -const path = require('../mocks/path'); +import { Path } from '../mocks/path'; describe('Single values transformed by field sentinels', () => { + let firebase; + + beforeEach(async () => { + firebase = await import('firebase'); + }); + test('isEqual', () => { const path1 = new firebase.firestore.FieldPath('collection', 'doc1'); const path2 = new firebase.firestore.FieldPath('collection', 'doc2'); @@ -13,16 +18,16 @@ describe('Single values transformed by field sentinels', () => { }); test('compareTo', () => { - const path1 = new path.Path(['abc', 'def', 'ghij']); - const path2 = new path.Path(['abc', 'def', 'ghik']); + const path1 = new Path(['abc', 'def', 'ghij']); + const path2 = new Path(['abc', 'def', 'ghik']); expect(path1.compareTo(path2)).toEqual(-1); - const path3 = new path.Path(['abc', 'def', 'ghi']); + const path3 = new Path(['abc', 'def', 'ghi']); expect(path1.compareTo(path3)).toEqual(1); - const path4 = new path.Path(['abc', 'def']); - const path5 = new path.Path(['abc', 'def']); + const path4 = new Path(['abc', 'def']); + const path5 = new Path(['abc', 'def']); expect(path1.compareTo(path4)).toEqual(1); expect(path4.compareTo(path5)).toEqual(0); - const path6 = new path.Path(['abc', 'def', 'ghi', 'klm']); + const path6 = new Path(['abc', 'def', 'ghi', 'klm']); expect(path6.compareTo(path1)).toEqual(-1); expect(path3.compareTo(path6)).toEqual(-1); }); diff --git a/__tests__/query.test.js b/__tests__/query.test.js index efb84da..1837c54 100644 --- a/__tests__/query.test.js +++ b/__tests__/query.test.js @@ -1,14 +1,16 @@ -const { +import { mockCollection, mockDoc, mockGet, mockWhere, mockOffset, FakeFirestore, -} = require('../mocks/firestore'); -const { mockFirebase } = require('firestore-jest-mock'); +} from '../mocks/firestore'; +import { mockFirebase } from 'firestore-jest-mock'; describe('Queries', () => { + let firebase, db; + mockFirebase( { database: { @@ -137,15 +139,17 @@ describe('Queries', () => { { simulateQueryFilters: true }, ); - const firebase = require('firebase'); - firebase.initializeApp({ - apiKey: '### FIREBASE API KEY ###', - authDomain: '### FIREBASE AUTH DOMAIN ###', - projectId: '### CLOUD FIRESTORE PROJECT ID ###', + beforeEach(async () => { + firebase = await import('firebase'); + firebase.initializeApp({ + apiKey: '### FIREBASE API KEY ###', + authDomain: '### FIREBASE AUTH DOMAIN ###', + projectId: '### CLOUD FIRESTORE PROJECT ID ###', + }); + + db = firebase.firestore(); }); - const db = firebase.firestore(); - test('it can query a single document', async () => { const monkey = await db .collection('animals') diff --git a/__tests__/references.test.js b/__tests__/references.test.js index 5dc311d..ea896c4 100644 --- a/__tests__/references.test.js +++ b/__tests__/references.test.js @@ -1,5 +1,5 @@ -const { FakeFirestore } = require('firestore-jest-mock'); -const { +import { FakeFirestore } from 'firestore-jest-mock'; +import { mockCollection, mockDoc, mockDelete, @@ -8,7 +8,7 @@ const { mockOrderBy, mockStartAfter, mockStartAt, -} = require('firestore-jest-mock/mocks/firestore'); +} from 'firestore-jest-mock/mocks/firestore'; describe('Reference Sentinels', () => { beforeEach(() => { diff --git a/__tests__/transaction.test.js b/__tests__/transaction.test.js index fab825f..f66f23c 100644 --- a/__tests__/transaction.test.js +++ b/__tests__/transaction.test.js @@ -1,5 +1,5 @@ -const { mockFirebase, FakeFirestore } = require('firestore-jest-mock'); -const { +import { mockFirebase, FakeFirestore } from 'firestore-jest-mock'; +import { mockRunTransaction, mockDelete, mockDeleteTransaction, @@ -12,23 +12,24 @@ const { mockGetAll, mockGetAllTransaction, mockCreateTransaction, -} = require('firestore-jest-mock/mocks/firestore'); +} from 'firestore-jest-mock/mocks/firestore'; describe('Transactions', () => { + let firebase, db; mockFirebase({ database: {}, }); - const firebase = require('firebase'); - firebase.initializeApp({ - apiKey: '### FIREBASE API KEY ###', - authDomain: '### FIREBASE AUTH DOMAIN ###', - projectId: '### CLOUD FIRESTORE PROJECT ID ###', - }); - const db = firebase.firestore(); - - beforeEach(() => { + + beforeEach(async () => { jest.resetModules(); jest.clearAllMocks(); + firebase = await import('firebase'); + firebase.initializeApp({ + apiKey: '### FIREBASE API KEY ###', + authDomain: '### FIREBASE AUTH DOMAIN ###', + projectId: '### CLOUD FIRESTORE PROJECT ID ###', + }); + db = firebase.firestore(); }); test('it returns a Promise', () => { diff --git a/mocks/firestore.js b/mocks/firestore.js index 7c4c396..ddf2916 100644 --- a/mocks/firestore.js +++ b/mocks/firestore.js @@ -1,24 +1,24 @@ -const mockCollectionGroup = jest.fn(); -const mockBatch = jest.fn(); -const mockRunTransaction = jest.fn(); - -const mockSettings = jest.fn(); -const mockUseEmulator = jest.fn(); -const mockCollection = jest.fn(); -const mockDoc = jest.fn(); -const mockUpdate = jest.fn(); -const mockSet = jest.fn(); -const mockAdd = jest.fn(); -const mockDelete = jest.fn(); -const mockListDocuments = jest.fn(); -const mockListCollections = jest.fn(); - -const mockBatchDelete = jest.fn(); -const mockBatchCommit = jest.fn(); -const mockBatchUpdate = jest.fn(); -const mockBatchSet = jest.fn(); - -const mockOnSnapShot = jest.fn(); +export const mockCollectionGroup = jest.fn(); +export const mockBatch = jest.fn(); +export const mockRunTransaction = jest.fn(); + +export const mockSettings = jest.fn(); +export const mockUseEmulator = jest.fn(); +export const mockCollection = jest.fn(); +export const mockDoc = jest.fn(); +export const mockUpdate = jest.fn(); +export const mockSet = jest.fn(); +export const mockAdd = jest.fn(); +export const mockDelete = jest.fn(); +export const mockListDocuments = jest.fn(); +export const mockListCollections = jest.fn(); + +export const mockBatchDelete = jest.fn(); +export const mockBatchCommit = jest.fn(); +export const mockBatchUpdate = jest.fn(); +export const mockBatchSet = jest.fn(); + +export const mockOnSnapShot = jest.fn(); import * as timestamp from './timestamp'; import * as fieldValue from './fieldValue'; @@ -33,9 +33,9 @@ const _randomId = () => Math.floor(Math.random() * Number.MAX_SAFE_INTEGER).toSt export class FakeFirestore { constructor(stubbedDatabase = {}, options = {}) { + this.options = options; this.database = timestamp.convertTimestamps(stubbedDatabase); this.query = new query.Query('', this); - this.options = options; } set collectionName(collectionName) { @@ -534,27 +534,42 @@ FakeFirestore.CollectionReference = class extends FakeFirestore.Query { } }; -export const mocks = { - mockBatch, - mockRunTransaction, - mockCollection, - mockCollectionGroup, - mockDoc, - mockAdd, - mockDelete, - mockUpdate, - mockSet, - mockSettings, - mockUseEmulator, - mockBatchDelete, - mockBatchCommit, - mockBatchUpdate, - mockBatchSet, - mockOnSnapShot, - mockListDocuments, - mockListCollections, - ...query.mocks, - ...transaction.mocks, - ...fieldValue.mocks, - ...timestamp.mocks, -}; +// Temporary for backwards compatability +export const { + mockGet, + mockSelect, + mockWhere, + mockLimit, + mockOrderBy, + mockOffset, + mockStartAfter, + mockStartAt, + mockQueryOnSnapshot, + mockWithConverter, +} = query.mocks; + +export const { + mockGetAll, + mockGetAllTransaction, + mockGetTransaction, + mockSetTransaction, + mockUpdateTransaction, + mockDeleteTransaction, + mockCreateTransaction, +} = transaction.mocks; + +export const { + mockArrayUnionFieldValue, + mockArrayRemoveFieldValue, + mockDeleteFieldValue, + mockIncrementFieldValue, + mockServerTimestampFieldValue, +} = fieldValue.mocks; + +export const { + mockTimestampToDate, + mockTimestampToMillis, + mockTimestampFromDate, + mockTimestampFromMillis, + mockTimestampNow, +} = timestamp.mocks;