-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
coerce_spec.js
69 lines (52 loc) · 2.03 KB
/
coerce_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
require('../spec_helper')
const coerce = require(`${root}lib/util/coerce`)
const getProcessEnvVars = require(`${root}lib/config`).getProcessEnvVars
describe('lib/util/coerce', () => {
beforeEach(function () {
this.env = process.env
})
afterEach(function () {
process.env = this.env
})
context('coerce', () => {
it('coerces string', () => {
expect(coerce('foo')).to.eq('foo')
})
it('coerces string from process.env', () => {
process.env['CYPRESS_STRING'] = 'bar'
const cypressEnvVar = getProcessEnvVars(process.env)
expect(coerce(cypressEnvVar)).to.deep.include({ STRING: 'bar' })
})
it('coerces number', () => {
expect(coerce('123')).to.eq(123)
})
// NOTE: When exporting shell variables, they are saved in `process.env` as strings, hence why
// all `process.env` variables are assigned as strings in these unit tests
it('coerces number from process.env', () => {
process.env['CYPRESS_NUMBER'] = '8000'
const cypressEnvVar = getProcessEnvVars(process.env)
expect(coerce(cypressEnvVar)).to.deep.include({ NUMBER: 8000 })
})
it('coerces boolean', () => {
expect(coerce('true')).to.be.true
})
it('coerces boolean from process.env', () => {
process.env['CYPRESS_BOOLEAN'] = 'false'
const cypressEnvVar = getProcessEnvVars(process.env)
expect(coerce(cypressEnvVar)).to.deep.include({ BOOLEAN: false })
})
it('coerces array', () => {
expect(coerce('[foo,bar]')).to.have.members(['foo', 'bar'])
})
it('coerces array from process.env', () => {
process.env['CYPRESS_ARRAY'] = '[google.com,yahoo.com]'
const cypressEnvVar = getProcessEnvVars(process.env)
const coercedCypressEnvVar = coerce(cypressEnvVar)
expect(coercedCypressEnvVar).to.have.keys('ARRAY')
expect(coercedCypressEnvVar['ARRAY']).to.have.members(['google.com', 'yahoo.com'])
})
it('defaults value with multiple types to string', () => {
expect(coerce('123foo456')).to.eq('123foo456')
})
})
})