1
+ import os from 'os'
2
+ import process from 'process'
3
+ import { v4 as uuidv4 } from 'uuid' ;
4
+ import configStore from 'configstore'
5
+ import prompts from 'prompts'
6
+
7
+ import getLivestormConfig from '../../src/helpers/getLivestormConfig'
8
+ import livestormDomain from '../../src/helpers/livestormDomain'
9
+
10
+ import baseConfig , { withEnv , fromConfigStore } from '../fixtures/configs'
11
+
12
+ enum ConfigFileSources {
13
+ LivestormConfig = "livestorm.config.js" ,
14
+ Environments = "environments.json" ,
15
+ }
16
+
17
+ const fsify = require ( 'fsify' ) ( {
18
+ cwd : os . tmpdir ( ) ,
19
+ persistent : false ,
20
+ force : true
21
+ } )
22
+
23
+ let spy = jest . spyOn ( process , 'cwd' ) ;
24
+ let directory
25
+
26
+ jest . mock ( 'configstore' )
27
+ jest . mock ( 'prompts' )
28
+
29
+
30
+ const writeConfigFile = async ( config : any , source = ConfigFileSources . LivestormConfig ) => {
31
+ const contents = [
32
+ {
33
+ type : fsify . FILE ,
34
+ name : source ,
35
+ contents : source === ConfigFileSources . LivestormConfig ? `module.exports = ${ JSON . stringify ( config ) } ` : JSON . stringify ( config )
36
+ } ,
37
+ ]
38
+
39
+ if ( source === ConfigFileSources . Environments ) {
40
+ contents . push ( {
41
+ type : fsify . FILE ,
42
+ // @ts -ignore
43
+ name : 'package.json' ,
44
+ contents : '{}'
45
+ } )
46
+ }
47
+ const structure = [
48
+ {
49
+ type : fsify . DIRECTORY ,
50
+ name : directory ,
51
+ contents,
52
+ }
53
+ ]
54
+
55
+ await fsify ( structure )
56
+ }
57
+
58
+ beforeEach ( ( ) => {
59
+ directory = uuidv4 ( )
60
+ spy . mockReturnValue ( os . tmpdir ( ) + '/' + directory ) ;
61
+ } )
62
+
63
+ describe ( 'The helper to get the livestorm config' , ( ) => {
64
+ describe ( 'from the the livestorm.config.js file' , ( ) => {
65
+ it ( 'should return a simple config by default' , async ( ) => {
66
+ await writeConfigFile ( baseConfig )
67
+ const config = await getLivestormConfig ( )
68
+ expect ( config ) . toStrictEqual ( {
69
+ ...baseConfig ,
70
+ endpoint : livestormDomain
71
+ } )
72
+ } )
73
+
74
+ it ( 'should return the conf from the set environment' , async ( ) => {
75
+ await writeConfigFile ( withEnv )
76
+ const config = await getLivestormConfig ( 'fakeEnv' )
77
+ expect ( config ) . toStrictEqual ( {
78
+ ...baseConfig ,
79
+ ...withEnv [ 'environments' ] . fakeEnv
80
+ } )
81
+ } )
82
+
83
+ it ( 'should retrieve the conf from the config store if exists' , async ( ) => {
84
+ const mockedConfigstore = jest . mocked ( configStore , true )
85
+ const get = mockedConfigstore . mock . instances [ 0 ] . get as jest . Mock
86
+
87
+ get . mockReturnValue ( fromConfigStore )
88
+
89
+ await writeConfigFile ( baseConfig )
90
+ const config = await getLivestormConfig ( 'fakeEnv' )
91
+
92
+ expect ( get ) . toHaveBeenCalled ( )
93
+
94
+ const [ key ] = get . mock . calls [ 0 ]
95
+
96
+ expect ( key ) . toBe ( `envs.fakeEnv` )
97
+
98
+ expect ( config ) . toStrictEqual ( {
99
+ ...baseConfig ,
100
+ ...fromConfigStore
101
+ } )
102
+ } )
103
+
104
+ it ( 'should retrieve the conf from the config store if global env is selected' , async ( ) => {
105
+ const mockedConfigstore = jest . mocked ( configStore , true )
106
+ const get = mockedConfigstore . mock . instances [ 0 ] . get as jest . Mock
107
+
108
+ get . mockReturnValue ( fromConfigStore )
109
+
110
+ const mockedPrompts = jest . mocked ( prompts , false )
111
+
112
+ mockedPrompts . mockReturnValue ( Promise . resolve ( {
113
+ selectedEnvConf : 'global'
114
+ } ) )
115
+
116
+ await writeConfigFile ( withEnv )
117
+ const config = await getLivestormConfig ( 'fakeEnv' )
118
+
119
+ expect ( get ) . toHaveBeenCalled ( )
120
+
121
+ const [ key ] = get . mock . calls [ 0 ]
122
+
123
+ expect ( key ) . toBe ( `envs.fakeEnv` )
124
+
125
+ expect ( config ) . toStrictEqual ( {
126
+ ...baseConfig ,
127
+ ...fromConfigStore
128
+ } )
129
+ } )
130
+
131
+ it ( 'should return an error when no config file' , async ( ) => {
132
+ await expect ( getLivestormConfig ( ) ) . rejects . toBe ( 'The livestorm conf file is missing.' ) ;
133
+ } )
134
+
135
+ it ( 'should return an error when no name in the config' , async ( ) => {
136
+ await writeConfigFile ( { ...baseConfig , name : null } )
137
+ await expect ( getLivestormConfig ( ) ) . rejects . toBe ( 'The name is missing.' ) ;
138
+ } )
139
+
140
+ it ( 'should return an error when no API Token in the config' , async ( ) => {
141
+ await writeConfigFile ( { ...baseConfig , apiToken : null } )
142
+ await expect ( getLivestormConfig ( ) ) . rejects . toBe ( 'The API Token is missing.' ) ;
143
+ } )
144
+ } )
145
+
146
+ describe ( 'from the the environments.json file' , ( ) => {
147
+ it ( 'should return a simple config by default' , async ( ) => {
148
+ await writeConfigFile ( {
149
+ development : baseConfig
150
+ } , ConfigFileSources . Environments )
151
+ const config = await getLivestormConfig ( )
152
+ expect ( config ) . toStrictEqual ( {
153
+ ...baseConfig ,
154
+ endpoint : livestormDomain
155
+ } )
156
+ } )
157
+
158
+ it ( 'should return the env config if set' , async ( ) => {
159
+ await writeConfigFile ( {
160
+ production : baseConfig
161
+ } , ConfigFileSources . Environments )
162
+ const config = await getLivestormConfig ( 'production' )
163
+ expect ( config ) . toStrictEqual ( {
164
+ ...baseConfig ,
165
+ endpoint : livestormDomain
166
+ } )
167
+ } )
168
+ } )
169
+
170
+ describe ( 'with 2 conf files' , ( ) => {
171
+ it ( 'should return the conf from livestorm.config.js' , async ( ) => {
172
+ await writeConfigFile ( { ...baseConfig , name : 'fromLivestormConfJs' } )
173
+ await writeConfigFile ( {
174
+ development : {
175
+ ...baseConfig ,
176
+ name : 'fromEnvironmentsJson'
177
+ }
178
+ } , ConfigFileSources . Environments )
179
+ const config = await getLivestormConfig ( )
180
+ expect ( config . name ) . toBe ( 'fromLivestormConfJs' )
181
+ } )
182
+ } )
183
+ } )
0 commit comments