0

I'm faced with the following issue. I'm testing a functionality with different behaviour based on process.env.NODE_ENV variable. Here is a short snippet of logic I aim to test

  if (process.env.NODE_ENV === 'production') {
    // this is what I aim to cover
  }

Here is a snippet of my suite:

  test('case 1', () => {
    const result = method.call()

    expect(result).toEqual(something)
  })

  test('case 2, with process.env.NODE_ENV set to "production"', () => {
    process.env.NODE_ENV = 'production'

    const result = method.call()

    expect(result).toEqual(somethingElse)
  })

After running the tests, the conditional requirement regarding NODE_ENV being set to production is not met and if statement from the first snippet is not covered. There's a peculiar thing I noticed with a scenario like this:

  test('case 1', () => {
    process.env.NODE_ENV = 'production'

    const result = method.call()

    expect(result).toEqual(something)
  })

  test('case 2, with process.env.NODE_ENV set to "production"', () => {
    const result = method.call()

    expect(result).toEqual(somethingElse)
  })

Now tests behave as I want them to, meaning process.env.NODE_ENV is set to production in case 2 and the if statement is covered. Can anyone explain why this happens? It appears as if the change of process.env takes effect only in the next suites.

pakut2
  • 500
  • 5
  • 21

0 Answers0