import { render, screen, fireEvent, waitFor } from '@testing-library/react';
import { BrowserRouter } from 'react-router-dom';
import { vi } from 'vitest';
import LobbyScreen from './LobbyScreen';
import useLobbyData from './useLobbyData';
// Mock the useLobbyData hook
vi.mock('./useLobbyData');
import { useParams } from 'react-router-dom';
import PlayerList from './PLayerList';
import TeamManagement from './TeamManagement';
import GameSettings from './GameSettings';
import useLobbyData from './useLobbyData';
❯ src/pages/lobbyScreen/LobbyScreen.test.tsx (7)
❯ LobbyScreen Component (7)
× renders lobby screen correctly
⠸ [ beforeEach ]
× calls leaveGame function when Leave Game button is clicked
⠸ [ beforeEach ]
× does not render Game Settings for non-host players
⠸ [ beforeEach ]
× displays loading state when game data is not available
⠸ [ beforeEach ]
× calls startGame function when Start Game button is clicked by host
⠸ [ beforeEach ]
× updates player status when ready button is clicked
⠸ [ beforeEach ]
× renders correct number of players and teams
⠸ [ beforeEach ]
❯ src/pages/lobbyScreen/useLobbyData.test.tsx (0)
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Failed Suites 1 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
FAIL src/pages/lobbyScreen/useLobbyData.test.tsx [ src/pages/lobbyScreen/useLobbyData.test.tsx ]
Error: [vitest] No "getFirestore" export is defined on the "firebase/firestore" mock. Did you forget to return it from "vi.mock"?
If you need to partially mock a module, you can use "importOriginal" helper inside:
vi.mock(import("firebase/firestore"), async (importOriginal) => {
const actual = await importOriginal()
return {
...actual,
// your mocked methods
}
})
The tests are failing due to incorrect mocking of the Firebase modules, specifically the firebase/firestore module. The error message indicates that the getFirestore export is not defined in the mocked module:
code
Error: [vitest] No "getFirestore" export is defined on the "firebase/firestore" mock. Did you forget to return it from "vi.mock"?
Explanation of the Failure:
In your test setup, you are trying to mock Firebase modules using vi.mock and vi.importActual. However, the way vi.importActual is being used is causing the exports not to be correctly defined in the mocked modules. Specifically, the mocked modules are not correctly exporting functions like getFirestore, which leads to the error when the code attempts to import them.
The issue here is that vi.importActual is not being used correctly. Vitest expects the importOriginal function to be passed as an argument to the module factory function. Using vi.importActual directly inside the callback does not correctly resolve the original module exports, leading to missing exports in the mocked module.
How to Fix the Tests:
To fix the tests, you need to adjust the mocking code to correctly use the importOriginal helper function provided by Vitest. This function should be passed as an argument to the module factory function in vi.mock and then used to import the actual module. This will ensure that the exports from the actual module are correctly included in your mock, and you can then override specific functions as needed.
By passing importOriginal as an argument to the module factory function in vi.mock, you ensure that Vitest correctly resolves the original module exports. Calling await importOriginal() inside the function imports the actual module, and then you can spread its exports and override specific functions as needed. This approach makes sure that all the expected exports are present in your mocked module.
With the corrected mocking code, your tests should now be able to import getFirestore and other Firebase functions without errors, and the tests should pass.
Additional Notes:
Ensure that you are mocking modules consistently throughout your test files. If you mock a module in setupTests.ts, you shouldn't need to mock it again in your individual test files unless you need to override the mock for specific tests.
If you need to mock react-router-dom functions like useParams in your test file, you can do so by adjusting your mock accordingly, but remember to use the importOriginal pattern.
Here's how you might adjust the react-router-dom mock in your test file:
The tests failed because the getFirestore function was not correctly exported in the mocked firebase/firestore module. By adjusting the mocking code to use the importOriginal helper function provided by Vitest and properly re-exporting necessary functions, you can fix the error and allow the tests to proceed.
With this fix, your tests should run without the previous errors.
By understanding how Vitest's module mocking works and ensuring that the original module's exports are correctly included, you can prevent such issues and make your tests reliable.