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

Fix issue with dotenv in tests #89

Merged
merged 3 commits into from
Jan 16, 2023
Merged

Fix issue with dotenv in tests #89

merged 3 commits into from
Jan 16, 2023

Conversation

Cruikshanks
Copy link
Member

@Cruikshanks Cruikshanks commented Jan 16, 2023

https://eaflood.atlassian.net/browse/WATER-3833

In Refactor dotenv require it was identified that we were requiring dotnev in multiple places and ideally we should do it once, as early as possible.

It was spotted in Move location where 'dotenv' is being required that some of the tests were affected by this move, and we bumped it from index.js to /app/server.js.

We then created our first DB migrations and realised we also needed to include it in config/database.config.js else migrations wouldn't work.

What we now realise is that controller tests work because they require /app/server.js which requires dotenv. Also, any test that connects with the DB pulls in config/database.config.js so those are fine.

But when working on Request new bill run in Charging Module API and running tests that need config/services.config.js to be populated they were breaking. This is because dotenv is never getting required() so none of our env vars are getting read in.

TL;DR; there was a reason we require('dotenv') in all our config files in sroc-charging-module-api ; it's to make both the app and tests work as expected!

This change reverts Refactor dotenv require to get everything working again.

https://eaflood.atlassian.net/browse/WATER-3833

In [Refactor dotenv require](#10) it was identified that we were requiring [dotnev](https://github.com/motdotla/dotenv) in multiple places and ideally we should do it once, as early as possible.

It was spotted in [Move location where 'dotenv' is being required](#21) that some of the tests were effected by this move, and we bumped it from `index.js` to `/app/server.js`.

We then created our first DB migrations and realised we also needed to include it in `config/database.config.js` else migrations wouldn't work.

What we now realise is that controller tests work because they require `/app/server.js` which requires **dotenv**. Also, any test that connects with the DB pulls in `config/database.config.js` so those are fine.

But when working on [Request new bill run in Charging Module API](request-new-bill-run-in-charging-module) and running tests that need `config/services.config.js` to be populated they were breaking. This is because **dotenv** is never getting `required()` so none of our env vars are getting read in.

TL;DR; there was a reason we `require('dotenv')` in all our config files in [sroc-charging-module-api
](https://github.com/DEFRA/sroc-charging-module-api); it's to make both the app _and_ tests work as expected!

This change reverts [Refactor dotenv require](#10) to get everything working again.
@Cruikshanks Cruikshanks added the bug Something isn't working label Jan 16, 2023
@Cruikshanks Cruikshanks self-assigned this Jan 16, 2023
We don't need to include it here because we know it will have been brought into the app by 1 or more of the config files.

The app won't start without reading in this config so we _know_ `require('dontenv')` will have been run somewhere!
@Cruikshanks Cruikshanks marked this pull request as ready for review January 16, 2023 10:55
@Cruikshanks Cruikshanks merged commit 4435f1f into main Jan 16, 2023
@Cruikshanks Cruikshanks deleted the fix-dot-env-for-tests branch January 16, 2023 11:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants