-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.spec.js
137 lines (110 loc) · 4.43 KB
/
index.spec.js
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
const plugin = require('./index')
const cypressConfigExample = {
baseUrl: 'http://example.com',
env: {},
viewportWidth: 800,
viewportHeight: 600
}
describe('Cypress dotenv plugin', () => {
const OLD_ENV = process.env
beforeEach(() => {
process.env = { ...OLD_ENV }
})
it('Should load the content of the .env file', () => {
plugin(cypressConfigExample)
expect(process.env.CYPRESS_TEST_VAR).toBeDefined()
expect(process.env.CYPRESS_TEST_VAR).toEqual('hello')
expect(process.env.NON_CYPRESS_TEST_VAR).toBeDefined()
expect(process.env.NON_CYPRESS_TEST_VAR).toEqual('goodbye')
})
it('Should do nothing if no CYPRESS_ env vars are present', () => {
const dotenvConfig = { path: './.env.no-cypress' }
const enhancedConfig = plugin(cypressConfigExample, dotenvConfig)
expect(enhancedConfig.env).toEqual({})
})
it('Should only add CYPRESS_ vars to the config by default', () => {
const enhancedConfig = plugin(cypressConfigExample)
expect(enhancedConfig.env.TEST_VAR).toBeDefined()
expect(enhancedConfig.env.TEST_VAR).toEqual('hello')
})
it('Should not alter any existing env vars that do not conflict', () => {
const config = {
...cypressConfigExample,
env: {
SOME_OTHER_ENV_VAR: 'hey there'
}
}
const enhancedConfig = plugin(config)
expect(enhancedConfig.env.TEST_VAR).toBeDefined()
expect(enhancedConfig.env.TEST_VAR).toEqual('hello')
expect(enhancedConfig.env.SOME_OTHER_ENV_VAR).toBeDefined()
expect(enhancedConfig.env.SOME_OTHER_ENV_VAR).toEqual('hey there')
})
it('Should not alter existing CYPRESS_ environment variables', () => {
process.env['CYPRESS_TEST_VAR'] = 'existing'
plugin(cypressConfigExample)
expect(process.env.CYPRESS_TEST_VAR).toBeDefined()
expect(process.env.CYPRESS_TEST_VAR).not.toEqual('hello')
expect(process.env.CYPRESS_TEST_VAR).toEqual('existing')
})
it('Should update any standard Cypress config keys, even if the .env key is in SNAKE_CASE', () => {
const enhancedConfig = plugin(cypressConfigExample)
expect(enhancedConfig.baseUrl).toEqual('http://google.com')
})
it('Should parse things that are numbers, as numbers', () => {
const enhancedConfig = plugin(cypressConfigExample)
expect(enhancedConfig.env.I_AM_NUMBER).toEqual(100)
})
it('Should parse things that are numbers with leading zeros, as a string', () => {
const enhancedConfig = plugin(cypressConfigExample)
expect(enhancedConfig.env.I_AM_A_NUMBER_WITH_LEADING_ZERO).toEqual('0100')
})
it('Should parse things that are booleans, as booleans', () => {
const enhancedConfig = plugin(cypressConfigExample)
expect(enhancedConfig.env.I_AM_BOOLEAN).toEqual(true)
})
it('Should yield config.env as an object, even if there is a CYPRESS_ENV present', () => {
const enhancedConfig = plugin(cypressConfigExample)
expect(typeof enhancedConfig.env).toEqual('object')
})
it('Should not error out if there is no .env file present', () => {
const dotenvConfig = { path: './non-existent-file' }
const enhancedConfig = plugin(cypressConfigExample, dotenvConfig)
expect(enhancedConfig.env).toEqual({})
expect(enhancedConfig).toEqual(cypressConfigExample)
})
it('Should not error out if an empty Cypress config object is passed in', () => {
const expectedOutput = {
BASE_URL: 'http://google.com',
ENV: 'testing',
I_AM_BOOLEAN: true,
I_AM_NUMBER: 100,
TEST_VAR: 'hello',
I_AM_A_NUMBER_WITH_LEADING_ZERO: '0100'
}
const enhancedConfig = plugin({})
expect(enhancedConfig.env).toEqual(expectedOutput)
expect(enhancedConfig).toEqual({ env: expectedOutput })
})
it('Should not error out if an "undefined" Cypress config object is passed in', () => {
global.console = { warn: jest.fn() }
enhancedConfig = plugin(undefined)
expect(enhancedConfig).toEqual({})
expect(console.warn).toHaveBeenCalledTimes(1)
})
describe('Optional all argument', () => {
it('Should return all available env vars', () => {
const dotenvConfig = { path: './.env' }
const enhancedConfig = plugin(cypressConfigExample, dotenvConfig, true)
expect(enhancedConfig.env).toEqual({
BASE_URL: 'http://google.com',
ENV: 'testing',
I_AM_BOOLEAN: true,
I_AM_NUMBER: 100,
NON_CYPRESS_TEST_VAR: 'goodbye',
TEST_VAR: 'hello',
I_AM_A_NUMBER_WITH_LEADING_ZERO: '0100'
})
})
})
})