-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mocking the Backend with node & Codi 🐶 #1884
Open
RobAndrewHurst
wants to merge
37
commits into
GEOLYTIX:minor
Choose a base branch
from
RobAndrewHurst:backend-tests
base: minor
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Mocking with node & Codi 🐶
Note
Codi has implemented mock modules/functions that are only available in the nodeJS environment.
Module mocking is a strange and weird concept to get your head around. It is essentially is a way to replace modules or functions with stubs to simulate or 'mock' external entities.
What a mocking module will do is replace the reference of a module in memory with a 'mocked' version of it. This mocked version can then be called from non-test code and receive an output. You can then reset that version of the mocked module to the original export with a reset/restore function.
Codi can mock:
function (fn) mocking
Just like any function, mocking a function needs to have context. The context can be the scope of a test, imported module or global.
To mock a function you can call the
codi.mock.fn()
function.This creates a mock function which you can interface with.
You can also mock functions/methods with the
codi.mock.method()
function that will take an object as a reference and implement a mock function to that objects method.Important
typically in tests written this methodology isn't used and favoured for the
codi.mock.mockImplementation()/mockImplementationOnce()
function which can mock a function given to a mocked module. An example of this will be provided in the mock module section.module mocking
You can mock modules with the
codi.mock.module()
function which takes a path and options to mock a module.The typical practice is that you provide a mocked function that you can implement mocks ontop of making the mocked module more reusable.
Caution
the
codi.mock.module()
function is still in early development as it comes from the node:test runner, which is still in further developmentoptions you can provide mocked module:
Bellow is an example of a mocked module referencing a mocked function
Important
Ensure that your module mocks are top level, as to import the module before the dynamic import to the module we are testing.
module & function restore/reset
if you want to return the functionality of a mocked function/module you will want to call the
restore
function on a mocked module.Important
You will want to call these restores on mocked modules at the end of a test so that other tests can also mock the same module. If you don't an error will be returned.
http mocks
codi has exported functions to help aid in mocking http requests.
codi.mockHttp
helps createreq
&res
objects that can be passed to functions in order to simulate a call to the function via an api. You can call thecreateRequest
&createResponse
functions respectively. You can also call thecreateMocks
function and perform a destructured assignment on thereq
&res
.You can also mock the response from the global fetch function by making use of the
MockAgent
&setGlobalDispatcher
interfaces.The
MockAgent
class is used to create a mockpool which can intercept different paths to certain URLs. And based on these paths we can specify a return.The
setGlobalDispatcher
will assign the Agent on a global scope so that calls to thefetch
function in non-test modules will be intercepted.Here is an example of this:
Running the Tests
The codi test suit will iterate through the tests directory [ignoring the folders specified in codi.json] and log the results from each test suit.
node --run test
You can also call the tests in watch mode where changes to the xyz codebase will retrigger the tests on save.
Running the watched tests will only show the tests that fail.
Note
It's better to call the scripts in the package.json with the
node --run
command as it's faster than npm.This is part of node v22+