Created
October 10, 2021 03:01
-
-
Save phatnguyenuit/45ad0428d9abff08a53b0729e6886182 to your computer and use it in GitHub Desktop.
Mock process.env in Jest - full example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import getAssetPath from './getAssetPath'; | |
describe('getAssetPath', () => { | |
const originalEnv = process.env; | |
describe.each` | |
nodeEnv | expectedPath | |
${'production'} | ${'/production-path'} | |
${'development'} | ${'/development-path'} | |
${'test'} | ${'/development-path'} | |
`('when process.env.NODE_ENV="$nodeEnv"', ({ nodeEnv, expectedPath }) => { | |
beforeEach(() => { | |
jest.resetModules(); | |
process.env = { | |
...originalEnv, | |
NODE_ENV: nodeEnv, | |
}; | |
}); | |
afterEach(() => { | |
process.env = originalEnv; | |
}); | |
it(`should return "${expectedPath}"`, () => { | |
expect(getAssetPath()).toEqual(expectedPath); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment