cds-dbs
cds-dbs copied to clipboard
[PARKED:] PoC - Snapshot testing
Summary
As the pluggable DB layer starts to support more databases it means increases development setup overhead. To keep it simple this PoC uses Jest
and expect
their snapshot concept to replace the database driver with snapshots.
How it works
git clone ...
cd ...
npm i
npm t # Runs all tests using no databases just replays the last recorded state
npm run test:all # Runs all tests using the databases including their setup scripts (updates snapshot recordings)
Side effects
To make this work everything has to be re-producible. That does not work with some APIs, but these APIs are generally the root cause for flaky / instable tests. So solving these sources of randomness will benefit everything in the long run.
new Date() / Date.now()
Time is not re-producible so when ever we will run the tests the time will be different from last time. This is easily solved by using the correct APIs. Jest
provides a fake timer API which allows for setting the time of the test to any Date
desired. So by locking this into place the context timestamp will always be the same. Additionally some tests where relying on the passage of time this is an anti pattern for testing. So when using the fake timers time does not pass unless explicitly told to pass. This way you don't need to wait a second for a second of time to pass. Simply calling the API will make 24hours pass in a few miliseconds.
UUID / GUID
Universally uniqueness does not help with achieving re-producible tests. The chance of the snapshot being the same being bigger then the amount of time that the universe has existed are not very good odds. So this issue is solved by overwriting the cds.utils.uuid
with a simple counter that returns the same format as is expected from the uuid
function. The counter is reset after the cds.test
scope is initialized so that the order or tests also don't impact the generated sequences.