Skip to content
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

Add first example reference model test (Regions) #14

Merged
merged 6 commits into from
Oct 8, 2024

Conversation

Cruikshanks
Copy link
Member

Now that we've imported and updated all our models, it is time to start adding some example tests to further research node-test as an alternative to Lab.

In database terms, all models are accessed and used via Objection.js. However, for testing, we split them into two camps: reference models and transaction models.

Reference models are things we don't expect to create within the service as part of daily use. They are records that would be seeded, such as regions.

Transaction models are things we expect to be created, such as bill runs or licences.

When it comes to testing we seed the tables that back the reference models, hence their helpers generally have a select() method. Transaction models, when needed, will be created during the testing. Their helpers generally have an add() method.

When we first tried this test, node-test wouldn't quit. It wouldn't even move to the next spec after region.model.test.js. This is a problem we first faced when pulling this project together. Time away has clearly helped because we were able to diagnose the cause as the database connection we create blocks node test from exiting (see test_runner: allow to force exit after all tests finished for a deeper dive into the issue which gave us our a-ha! moment).

This does mean we'll need to add an after() block to any spec that results in a DB connection being made, but that should be simple enough.

Completing this example has also highlighted that we'll probably want to extend assert in some way, to avoid 'messy duplication' in our tests.

But for now, here is our first working model node-test!

Now we've imported and updated all our models it is time to start adding some example tests to drive research of node-test as an alternate to Lab further.

In database terms, all models are accessed and used in the same way via Objection.js. However, we split them into two camps for testing; reference models and transaction models.

Reference models are things we don't expect to be creating within the service as part of daily use. They are records that would be seeded, for example, regions.

Transaction models are things we expect to be created, for example, bill runs or licences.

When it comes to testing we seed the tables that back the reference models, hence their helpers generally have a `select()` method. Transaction models, when needed, will be created during the testing. Their helpers generally have an `add()` method.

When we first tried this test, node-test wouldn't quit. It wouldn't even move to the next spec after `region.model.test.js`. This is a problem we first faced when pulling this project together. Time away has clearly helped because we were able to diagnose the cause as the database connection we create blocks node test from exiting (see [test_runner: allow to force exit after all tests finished](nodejs/node#49925) for a deeper dive into the issue which gave us our a-ha! moment).

It does mean we'll need to add an `after()` block into any spec that results in a DB connection being made. But that should be simple enough.

Completing this example has also highlighted that we'll probably want to extend `assert` in some way, to avoid 'messy duplication' in our tests.

But for now, here is our first working model node-test!
@Cruikshanks Cruikshanks self-assigned this Oct 8, 2024
`pretest` is not being called because we are using the custom script name `npm run test:lcov` rather than just `npm test`.

So, first attempt is calling pretest explicitly before running the unit tests.
@Cruikshanks Cruikshanks marked this pull request as ready for review October 8, 2024 10:54
@Cruikshanks Cruikshanks merged commit 81442ea into main Oct 8, 2024
4 checks passed
@Cruikshanks Cruikshanks deleted the add-example--reference-model-test branch October 8, 2024 10:55
@Cruikshanks Cruikshanks added the testing A change to the tests or CI label Oct 14, 2024
Cruikshanks added a commit that referenced this pull request Oct 14, 2024
In [Add first example reference model test (Regions)](#14) we added our first model test and chose a 'reference' type.

In database terms, all models are accessed and used via Objection.js. However, for testing, we split them into two camps: reference models and transaction models.

Reference models are things we don't expect to create within the service as part of daily use. They are records that would be seeded, such as regions.

Transaction models are things we expect to be created, such as bill runs, licences or in this case, workflow.

This change migrates the model tests for `WorkflowModel`.
Cruikshanks added a commit that referenced this pull request Oct 14, 2024
In [Add first example reference model test (Regions)](#14) we added our first model test and chose a 'reference' type.

In database terms, all models are accessed and used via Objection.js. However, for testing, we split them into two camps: reference models and transaction models.

Reference models are things we don't expect to create within the service as part of daily use. They are records that would be seeded, such as regions.

Transaction models are things we expect to be created, such as bill runs, licences or in this case, workflow.

This change migrates the model tests for `WorkflowModel`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
testing A change to the tests or CI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant