File size: 2,650 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { createEnvDefinitions } from './create-env-definitions'

describe('create-env-definitions', () => {
  it('should create env definitions', async () => {
    const loadedEnvFiles = [
      {
        path: '.env.local',
        contents: '',
        env: {
          FROM_ENV_LOCAL: 'FROM_ENV_LOCAL',
        },
      },
      {
        path: '.env.development.local',
        contents: '',
        env: {
          FROM_ENV_DEV_LOCAL: 'FROM_ENV_DEV_LOCAL',
        },
      },
      {
        path: 'next.config.js',
        contents: '',
        env: {
          FROM_NEXT_CONFIG: 'FROM_NEXT_CONFIG',
        },
      },
    ]
    const definitionStr = await createEnvDefinitions({
      distDir: '/dist',
      loadedEnvFiles,
    })
    expect(definitionStr).toMatchInlineSnapshot(`
      "// Type definitions for Next.js environment variables
      declare global {
        namespace NodeJS {
          interface ProcessEnv {
            /** Loaded from \`.env.local\` */
            FROM_ENV_LOCAL?: string
            /** Loaded from \`.env.development.local\` */
            FROM_ENV_DEV_LOCAL?: string
            /** Loaded from \`next.config.js\` */
            FROM_NEXT_CONFIG?: string
          }
        }
      }
      export {}"
    `)
  })

  it('should allow empty env', async () => {
    const definitionStr = await createEnvDefinitions({
      distDir: '/dist',
      loadedEnvFiles: [],
    })
    expect(definitionStr).toMatchInlineSnapshot(`
      "// Type definitions for Next.js environment variables
      declare global {
        namespace NodeJS {
          interface ProcessEnv {

          }
        }
      }
      export {}"
    `)
  })

  it('should dedupe env definitions in order of priority', async () => {
    const loadedEnvFiles = [
      {
        path: '.env.local',
        contents: '',
        env: {
          DUPLICATE_ENV: 'DUPLICATE_ENV',
        },
      },
      {
        path: '.env.development.local',
        contents: '',
        env: {
          DUPLICATE_ENV: 'DUPLICATE_ENV',
        },
      },
      {
        path: 'next.config.js',
        contents: '',
        env: {
          DUPLICATE_ENV: 'DUPLICATE_ENV',
        },
      },
    ]
    const definitionStr = await createEnvDefinitions({
      distDir: '/dist',
      loadedEnvFiles,
    })
    expect(definitionStr).toMatchInlineSnapshot(`
      "// Type definitions for Next.js environment variables
      declare global {
        namespace NodeJS {
          interface ProcessEnv {
            /** Loaded from \`.env.local\` */
            DUPLICATE_ENV?: string
          }
        }
      }
      export {}"
    `)
  })
})