0

Currently, I can only detect development and production environments by .gitignoreing my config file, where I can explicitly set either process.env.ENV = 'prod' or process.env.ENV = 'devel' depending on the current environment. And I can be sure that this value will be imported when arangod starts.

But unit tests are not running on arangod startup, so maybe I need to hook up some listener to http requests or something to determine whether it's a request that fires unit tests? Can you help me with this plz?

Thank you

artnikpro
  • 5,487
  • 4
  • 38
  • 40
  • Could you clarify what you are doing, what you are trying to do and what is or isn't working for you right now? I'm not really sure how to help you without a more detailed explanation of the problem. – Alan Plum Jul 14 '16 at 15:06
  • @AlanPlum I need to somehow detect inside of my code whether mocha tests are initiating some function so that I could do "dry run" instead of a real run of some code. For example, I have a function for sending emails that I don't want to be sending anything every time I run tests, so it should just return true right away if it detects that it's the "test" environment – artnikpro Jul 14 '16 at 18:40
  • Thanks for sharing your solution. Could you also mark it as accepted? – dothebart Jul 21 '16 at 09:13
  • @dothebart, Updated and marked as accepted. Thanks – artnikpro Jul 21 '16 at 13:37

1 Answers1

2

I found two possible solutions:

1) Create a pre-initializing file for tests e.g. test/init.js with the following content:

process.env.ENV = 'test'

then add it to your "tests" array in manifest.json as the very first entry point for tests:

"tests": [
  "test/init.js",
  "**/__tests__/**/*.js"
]

Detect test environment in any part of your code with:

if (process.env.ENV === 'test') { ... }

2) Since tests are running with Mocha we could also use a quick solution:

if (typeof it === 'function' && typeof describe === 'function') {
  // code for Mocha environment only
}
artnikpro
  • 5,487
  • 4
  • 38
  • 40