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

Set NODE_ENV to to 'test' if not already set #1523

Merged
merged 4 commits into from
Oct 1, 2017

Conversation

P-Seebauer
Copy link
Contributor

Fixes #1470

Still needs tests and docs.

I haven't found a good place in the docs to put the "warnings about the dangers".

Also stops process.env.AVA_PATH from getting overwritten in fork.js. I did not find a reason why the Object.assign is inside the if statement. (I have had a look at #559 and the likes and it seems to me that there is a similar issue with the AVA_PATH waiting to happen). Since there are no tests, I believe that the overwrite was not intentional (and could move to another place, if it was).

@novemberborn
Copy link
Member

Thanks @P-Seebauer!

I haven't found a good place in the docs to put the "warnings about the dangers".

Perhaps https://github.com/avajs/ava/#process-isolation comes closest, as it talks about the test process. Add another paragraph explaining what value of NODE_ENV to expect, and warning users that this may influence their code?

Also stops process.env.AVA_PATH from getting overwritten in fork.js. I did not find a reason why the Object.assign is inside the if statement. (I have had a look at #559 and the likes and it seems to me that there is a similar issue with the AVA_PATH waiting to happen). Since there are no tests, I believe that the overwrite was not intentional (and could move to another place, if it was).

I think you're right. I must've assumed it was a copy already. The value isn't used in the main process though so it was harmless.

@P-Seebauer P-Seebauer force-pushed the set-node_env branch 2 times, most recently from 6c3a6f2 to 31495fd Compare September 17, 2017 19:26
@P-Seebauer P-Seebauer changed the title [WIP] Set NODE_ENV to to 'test' if not already set Set NODE_ENV to to 'test' if not already set Sep 17, 2017
@P-Seebauer
Copy link
Contributor Author

I think, I'm done.

Sorry about the commit/build spam, I wasn't aware that github links the branch this way.

I'm not too happy about the Documentation part. Do you have any suggestions what could be done better?

Copy link
Member

@novemberborn novemberborn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think, I'm done.

👍

I'm not too happy about the Documentation part. Do you have any suggestions what could be done better?

See the inline comment below.

readme.md Outdated
@@ -1127,6 +1127,8 @@ t.true(a.test(b) || b === c)

Each test file is run in a separate Node.js process. This allows you to change the global state or overriding a built-in in one test file, without affecting another. It's also great for performance on modern multi-core processors, allowing multiple test files to execute in parallel.

For each process, `NODE_ENV` gets set to `'test'` if it was not set. This could change some ouput and/or the behaviour of some modules (e.g. express' final error handler does [*not* log errors when `NODE_ENV` is `'test'`](https://github.com/expressjs/express/blob/c087a45b9cc3eb69c777e260ee880758b6e03a40/lib/application.js#L630)). Is very convenient to set some defaults (change database, disable logging, etc…), but you shouldn't rely on it too much (if you check your [coverage](#code-coverage), you'll see it go down). As `NODE_ENV` is always set, `'NODE_ENV' in process.env` always is `false` when called from your tests.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about this?

AVA will set process.env.NODE_ENV to test, unless the NODE_ENV environment variable has been set. This is useful if the code you're testing has test defaults (for example when picking what database to connect to, or environment-specific Babel options). It may cause your code or its dependencies to behave differently though. Note that 'NODE_ENV' in process.env will always be true.

The Express example seems overly specific, as do the references to code coverage. And the result of 'NODE_ENV' in process.env will be true, not false.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah looks much better.

@novemberborn novemberborn merged commit 42e7c74 into avajs:master Oct 1, 2017
@novemberborn
Copy link
Member

Thanks @P-Seebauer!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants