|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 2 | + |
| 3 | +const ALL_ENV_VARS = [ |
| 4 | + 'CONTEXT', |
| 5 | + 'VERCEL_ENV', |
| 6 | + 'URL', |
| 7 | + 'NUXT_ENV_VERCEL_URL', |
| 8 | + 'NUXT_ENV_VERCEL_PROJECT_PRODUCTION_URL', |
| 9 | +] |
| 10 | + |
| 11 | +describe('getPreviewUrl', () => { |
| 12 | + beforeEach(() => { |
| 13 | + // Reset consts evaluated at module init time |
| 14 | + vi.resetModules() |
| 15 | + }) |
| 16 | + |
| 17 | + beforeEach(() => { |
| 18 | + for (const envVar of ALL_ENV_VARS) { |
| 19 | + vi.stubEnv(envVar, undefined) |
| 20 | + } |
| 21 | + }) |
| 22 | + |
| 23 | + afterEach(() => { |
| 24 | + vi.unstubAllEnvs() |
| 25 | + }) |
| 26 | + |
| 27 | + it('returns `undefined` if no known preview env is detected', async () => { |
| 28 | + const { getPreviewUrl } = await import('../../../config/env') |
| 29 | + |
| 30 | + expect(getPreviewUrl()).toBeUndefined() |
| 31 | + }) |
| 32 | + |
| 33 | + it.each([ |
| 34 | + ['Netlify production', { CONTEXT: 'production', URL: 'https://prod.example.com' }], |
| 35 | + ['Vercel production', { VERCEL_ENV: 'production', NUXT_ENV_VERCEL_URL: 'prod.example.com' }], |
| 36 | + ])('%s environment returns `undefined`', async (_name, envVars) => { |
| 37 | + for (const [key, value] of Object.entries(envVars)) { |
| 38 | + vi.stubEnv(key, value) |
| 39 | + } |
| 40 | + const { getPreviewUrl } = await import('../../../config/env') |
| 41 | + |
| 42 | + expect(getPreviewUrl()).toBeUndefined() |
| 43 | + }) |
| 44 | + |
| 45 | + it.each([ |
| 46 | + ['Netlify dev', { CONTEXT: 'dev', URL: 'https://dev.example.com' }, 'https://dev.example.com'], |
| 47 | + [ |
| 48 | + 'Netlify deploy-preview', |
| 49 | + { |
| 50 | + CONTEXT: 'deploy-preview', |
| 51 | + URL: 'https://preview.example.com', |
| 52 | + }, |
| 53 | + 'https://preview.example.com', |
| 54 | + ], |
| 55 | + [ |
| 56 | + 'Netlify branch-deploy', |
| 57 | + { CONTEXT: 'branch-deploy', URL: 'https://beta.example.com' }, |
| 58 | + 'https://beta.example.com', |
| 59 | + ], |
| 60 | + [ |
| 61 | + 'Netlify preview-server', |
| 62 | + { |
| 63 | + CONTEXT: 'preview-server', |
| 64 | + URL: 'https://my-feat--preview.example.com', |
| 65 | + }, |
| 66 | + 'https://my-feat--preview.example.com', |
| 67 | + ], |
| 68 | + [ |
| 69 | + 'Vercel development', |
| 70 | + { VERCEL_ENV: 'development', NUXT_ENV_VERCEL_URL: 'dev.example.com' }, |
| 71 | + 'https://dev.example.com', |
| 72 | + ], |
| 73 | + [ |
| 74 | + 'Vercel preview', |
| 75 | + { VERCEL_ENV: 'preview', NUXT_ENV_VERCEL_URL: 'preview.example.com' }, |
| 76 | + 'https://preview.example.com', |
| 77 | + ], |
| 78 | + ])('%s environment returns preview URL', async (_name, envVars, expectedUrl) => { |
| 79 | + for (const [key, value] of Object.entries(envVars)) { |
| 80 | + vi.stubEnv(key, value) |
| 81 | + } |
| 82 | + |
| 83 | + const { getPreviewUrl } = await import('../../../config/env') |
| 84 | + |
| 85 | + expect(getPreviewUrl()).toBe(expectedUrl) |
| 86 | + }) |
| 87 | +}) |
| 88 | + |
| 89 | +describe('getProductionUrl', () => { |
| 90 | + beforeEach(() => { |
| 91 | + // Reset consts evaluated at module init time |
| 92 | + vi.resetModules() |
| 93 | + }) |
| 94 | + |
| 95 | + beforeEach(() => { |
| 96 | + for (const envVar of ALL_ENV_VARS) { |
| 97 | + vi.stubEnv(envVar, undefined) |
| 98 | + } |
| 99 | + }) |
| 100 | + |
| 101 | + afterEach(() => { |
| 102 | + vi.unstubAllEnvs() |
| 103 | + }) |
| 104 | + |
| 105 | + it('returns `undefined` if no known production env is detected', async () => { |
| 106 | + const { getProductionUrl } = await import('../../../config/env') |
| 107 | + |
| 108 | + expect(getProductionUrl()).toBeUndefined() |
| 109 | + }) |
| 110 | + |
| 111 | + it.each([ |
| 112 | + ['Netlify dev', { CONTEXT: 'dev', URL: 'https://dev.example.com' }], |
| 113 | + [ |
| 114 | + 'Netlify deploy-preview', |
| 115 | + { |
| 116 | + CONTEXT: 'deploy-preview', |
| 117 | + URL: 'https://preview.example.com', |
| 118 | + }, |
| 119 | + ], |
| 120 | + ['Netlify branch-deploy', { CONTEXT: 'branch-deploy', URL: 'https://beta.example.com' }], |
| 121 | + [ |
| 122 | + 'Netlify preview-server', |
| 123 | + { |
| 124 | + CONTEXT: 'preview-server', |
| 125 | + URL: 'https://my-feat--preview.example.com', |
| 126 | + }, |
| 127 | + ], |
| 128 | + [ |
| 129 | + 'Vercel development', |
| 130 | + { |
| 131 | + VERCEL_ENV: 'development', |
| 132 | + NUXT_ENV_VERCEL_PROJECT_PRODUCTION_URL: 'dev.example.com', |
| 133 | + }, |
| 134 | + ], |
| 135 | + [ |
| 136 | + 'Vercel preview', |
| 137 | + { |
| 138 | + VERCEL_ENV: 'preview', |
| 139 | + NUXT_ENV_VERCEL_PROJECT_PRODUCTION_URL: 'preview.example.com', |
| 140 | + }, |
| 141 | + ], |
| 142 | + ])('%s environment returns `undefined`', async (_name, envVars) => { |
| 143 | + for (const [key, value] of Object.entries(envVars)) { |
| 144 | + vi.stubEnv(key, value) |
| 145 | + } |
| 146 | + const { getProductionUrl } = await import('../../../config/env') |
| 147 | + |
| 148 | + expect(getProductionUrl()).toBeUndefined() |
| 149 | + }) |
| 150 | + |
| 151 | + it.each([ |
| 152 | + [ |
| 153 | + 'Netlify production', |
| 154 | + { CONTEXT: 'production', URL: 'https://prod.example.com' }, |
| 155 | + 'https://prod.example.com', |
| 156 | + ], |
| 157 | + [ |
| 158 | + 'Vercel production', |
| 159 | + { |
| 160 | + VERCEL_ENV: 'production', |
| 161 | + NUXT_ENV_VERCEL_PROJECT_PRODUCTION_URL: 'prod.example.com', |
| 162 | + }, |
| 163 | + 'https://prod.example.com', |
| 164 | + ], |
| 165 | + ])('%s environment returns production URL', async (_name, envVars, expectedUrl) => { |
| 166 | + for (const [key, value] of Object.entries(envVars)) { |
| 167 | + vi.stubEnv(key, value) |
| 168 | + } |
| 169 | + const { getProductionUrl } = await import('../../../config/env') |
| 170 | + |
| 171 | + expect(getProductionUrl()).toBe(expectedUrl) |
| 172 | + }) |
| 173 | +}) |
0 commit comments