-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.test.js
88 lines (67 loc) · 2.31 KB
/
app.test.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
const { appEnv, appVersion } = require('./app')
describe('App Tests - Default ENV Values', () => {
const originalEnv = process.env
afterEach(() => {
// Reset process.env after each test case
process.env = originalEnv
})
describe('APP_VERSION Tests', () => {
it('should return default value when APP_VERSION', () => {
const result = appVersion()
expect(result).toBe(`App's version is v0.1`)
})
it('should throw an error if APP_VERSION is not defined', () => {
process.env = { APP_VERSION: undefined }
// OR process.env = {}
expect(appVersion).toThrow('APP_VERSION is not defined')
})
})
describe('NODE_ENV Tests', () => {
it('should return default value when NODE_ENV', () => {
const result = appEnv()
expect(result).toBe('App is running on local environment')
})
it('should throw an error if NODE_ENV is not defined', () => {
process.env = { NODE_ENV: undefined }
// OR process.env = {}
expect(appEnv).toThrow('NODE_ENV is not defined')
})
})
})
describe('App Tests - Manual ENV Values', () => {
const originalEnv = process.env
beforeEach(() => {
// Override process.env object before each test case:
process.env = {
NODE_ENV: 'prod',
APP_VERSION: 'v1.0',
}
// By default the value of process.env.NODE_ENV is test when running tests with jest
})
afterEach(() => {
// Reset process.env value after each test case
process.env = originalEnv
})
describe('APP_VERSION Tests', () => {
it('should return correct value when APP_VERSION is defined', () => {
const result = appVersion()
expect(result).toBe(`App's version is v1.0`)
})
it('should throw an error if APP_VERSION is not defined', () => {
process.env = { APP_VERSION: undefined }
// OR process.env = {}
expect(appVersion).toThrow('APP_VERSION is not defined')
})
})
describe('NODE_ENV Tests', () => {
it('should return correct value when NODE_ENV is defined', () => {
const result = appEnv()
expect(result).toBe('App is running on prod environment')
})
it('should throw an error if NODE_ENV is not defined', () => {
process.env = { NODE_ENV: undefined }
// OR process.env = {}
expect(appEnv).toThrow('NODE_ENV is not defined')
})
})
})