|
| 1 | +import { isBuild } from '../../src/utils/isBuild'; |
| 2 | + |
| 3 | +let originalEnv: typeof process.env; |
| 4 | +let originalArgv: typeof process.argv; |
| 5 | + |
| 6 | +function assertNoMagicValues(): void { |
| 7 | + if (Object.keys(process.env).includes('SENTRY_BUILD_PHASE') || process.argv.includes('build')) { |
| 8 | + throw new Error('Not starting test with a clean setup'); |
| 9 | + } |
| 10 | +} |
| 11 | + |
| 12 | +describe('isBuild()', () => { |
| 13 | + beforeEach(() => { |
| 14 | + assertNoMagicValues(); |
| 15 | + originalEnv = { ...process.env }; |
| 16 | + originalArgv = [...process.argv]; |
| 17 | + }); |
| 18 | + |
| 19 | + afterEach(() => { |
| 20 | + process.env = originalEnv; |
| 21 | + process.argv = originalArgv; |
| 22 | + assertNoMagicValues(); |
| 23 | + }); |
| 24 | + |
| 25 | + it("detects 'build' in argv", () => { |
| 26 | + // the result of calling `next build` |
| 27 | + process.argv = ['/abs/path/to/node', '/abs/path/to/nextjs/excecutable', 'build']; |
| 28 | + expect(isBuild()).toBe(true); |
| 29 | + }); |
| 30 | + |
| 31 | + it("sets env var when 'build' in argv", () => { |
| 32 | + // the result of calling `next build` |
| 33 | + process.argv = ['/abs/path/to/node', '/abs/path/to/nextjs/excecutable', 'build']; |
| 34 | + isBuild(); |
| 35 | + expect(Object.keys(process.env).includes('SENTRY_BUILD_PHASE')).toBe(true); |
| 36 | + }); |
| 37 | + |
| 38 | + it("does not set env var when 'build' not in argv", () => { |
| 39 | + isBuild(); |
| 40 | + expect(Object.keys(process.env).includes('SENTRY_BUILD_PHASE')).toBe(false); |
| 41 | + }); |
| 42 | + |
| 43 | + it('detects env var', () => { |
| 44 | + process.env.SENTRY_BUILD_PHASE = 'true'; |
| 45 | + expect(isBuild()).toBe(true); |
| 46 | + }); |
| 47 | + |
| 48 | + it("returns false when 'build' not in `argv` and env var not present", () => { |
| 49 | + expect(isBuild()).toBe(false); |
| 50 | + }); |
| 51 | +}); |
0 commit comments