fix: test failures and improved test structure #82
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hello everyone, I tried to solve the error in the test and attempted to create an environment for adding new tests in the future 🕺 fyi : @kayraberktuncer
First Small Steps for tests
First, I ran the command
yarn test
and encountered an error:The error message indicated that the module specified in the
src/modules/Dashboard/index.spec.tsx
test file could not be found. I thought I needed to define a path transformation using themoduleNameMapper
property in the Jest configuration. So I updated thejest.config.js
file as follows:Updated config:
Afterwards, the "Cannot find module" error was resolved, and test failures started to appear. It felt great to see that I was getting closer.
The error output in the
src/modules/Dashboard/index.spec.tsx
file was as follows:After this output, I decided to follow the steps below:
jest.mock('next/router', ...)
: I used the Jestmock
function to mock thenext/router
module and emulate the hook.useRouter: jest.fn().mockReturnValue({ ... })
: I replaced theuseRouter
hook with a mocked function provided by Jest. This mocked function contains typical properties and values of theuseRouter
object.expect(screen.getByTestId("home")).toBeInTheDocument()
: By using thescreen
object and callinggetByTestId
function, you are checking whether an element withdata-testid
attribute set to "home-dashboard" exists on the screen. This way, I confirmed that the "home-dashboard" component was rendered correctly. You can remove this line if it's not needed.the error has been resolved and each of the subfiles can have a test within itself