@villedemontreal/concurrent-api-tests
v2.0.1
Published
Provides the core functions required to implement concurrent API tests with vitest.
Downloads
9
Readme
concurrent-api-tests
Concurrent-api-tests provides the core functions required to implement Concurrent API Tests with Vitest.
To Install
In your project, run this npm command:
npm install @villedemontreal/concurrent-api-tests
Functions
defineCopyTemplate(template)
Define a function that provide a default payload template and that allow to specify only the parts of the payload that are meaningful for the test case. This way, test cases are easier to read since only the parts that matter are specified. Moreover, if a change in a payload is required, only the default payload template and the related tests need to be changed.
Arguments
- template: A default payload template.
Returns
A function that provide a default payload template and allow to specify only the parts of the payload that are meaningful for the test case.
Example
See concurrent-api-tests example.
defineCopyTemplateVariation(originalCopyTemplate, variation)
Define a copy template variation to avoid duplication when the same template is used in many test cases.
Arguments
- originalCopyTemplate: The original copy template function. See defineCopyTemplate.
- variation: A function that specifies only the parts of the payload that are meaningful for the variation.
Returns
A function that provide a default payload template and allow to specify only the parts of the payload that are meaningful for the test case.
Example
See concurrent-api-tests example.
shouldThrow(act, customAssert)
Assert against an API request that is expected to throw an error.
Arguments
- act: A function that send the API request.
- customAssert: A function that assert against the error.
Returns
void
Example
See concurrent-api-tests example.
aFewSeconds(delayInSeconds)
Some test cases must rely on the timing between API requests. These test cases are likely to be flaky if the timing is not managed with care.
If the precision of the timing has to be less than a second, then concurrent-api-tests is not the right tool for this test case. For more guidance, see Concurrent API Tests.
Arguments
- delayInSeconds: The number of seconds to wait for. (Ex: 5).
Returns
void
Example
await aFewSeconds(5);
defineGetSharedFixture(createSharedFixture)
Define a function that perform lazy initialization of a fixture. This allow to share the same fixture between many tests cases and to initialize it only once.
A fixture may be shared between the test cases of the same test run if
- the attribute of the user are not meaningful for the test
- the fixture is immutable
Although shared fixture can speed up test runs and reduce the amount of data created on the server, they must be used with care since they can produce flaky test cases if the two points above are not respected.
In doubt, create a new fixture for each test case.
Fast tests are important, but reliable tests are even more important.
Arguments
- createSharedFixture: A function that initialize the shared fixture.
Returns
A function that perform lazy initialization of the shared fixture.
Example
See concurrent-api-tests example.
defineGetSharedFixtureByKey(createSharedFixtureByKey)
Same as defineGetSharedFixture, but allow to pass a key as argument. Useful when there are many similar shared fixture to be defined.
Arguments
- createSharedFixtureByKey: A function that initialize the shared fixture for a specific key.
Returns
A function that perform lazy initialization of the shared fixture for a specific key.
Example
See concurrent-api-tests example.
Testing concurrent-api-tests itself
Run all unit tests, run this npm command:
npm start
Debug all unit tests, run this npm command:
npm run watch-no-emit (to activate incremental transpilation) and use a JavaScript Debug Terminal.
Lint, run this npm command:
npm run lint-fix
