|
18 | 18 | const fs = require('fs');
|
19 | 19 | const path = require('path');
|
20 | 20 | const process = require('process');
|
21 |
| -const { |
22 |
| - options, |
23 |
| - getAppOptions, |
24 |
| - getEnvOptions, |
25 |
| -} = require('../utils/getOptions'); |
| 21 | +const { options, getAppOptions, getEnvOptions } = require('../utils/getOptions'); |
26 | 22 | const constants = require('../constants/index');
|
27 | 23 |
|
28 | 24 | describe('Get Options script', () => {
|
29 |
| - const OLD_ENV = process.env; |
30 |
| - const processCwdValue = process.cwd(); |
31 |
| - |
32 |
| - beforeEach(() => { |
33 |
| - jest.resetModules(); |
34 |
| - process.env = { ...OLD_ENV }; |
35 |
| - delete process.env.NODE_ENV; |
| 25 | + const OLD_ENV = process.env; |
| 26 | + const processCwdValue = process.cwd(); |
| 27 | + |
| 28 | + beforeEach(() => { |
| 29 | + jest.resetModules(); |
| 30 | + process.env = { ...OLD_ENV }; |
| 31 | + delete process.env.NODE_ENV; |
| 32 | + }); |
| 33 | + |
| 34 | + afterEach(() => { |
| 35 | + jest.clearAllMocks(); |
| 36 | + process.env = OLD_ENV; |
| 37 | + }); |
| 38 | + |
| 39 | + describe('getEnvOptions', () => { |
| 40 | + test("should return empty envOptions object if environments don't set", () => { |
| 41 | + const envOptionsObject = getEnvOptions(); |
| 42 | + |
| 43 | + expect(envOptionsObject).toBeDefined(); |
| 44 | + expect(envOptionsObject).toEqual({}); |
36 | 45 | });
|
37 | 46 |
|
38 |
| - afterEach(() => { |
39 |
| - jest.clearAllMocks(); |
40 |
| - process.env = OLD_ENV; |
| 47 | + test('should return envOptions object with correct values', () => { |
| 48 | + const expectedEnvOptionsObject = { |
| 49 | + suiteName: 'suite name', |
| 50 | + output: 'output', |
| 51 | + classNameTemplate: 'class name', |
| 52 | + titleTemplate: 'title', |
| 53 | + }; |
| 54 | + process.env = { |
| 55 | + JEST_SUITE_NAME: 'suite name', |
| 56 | + JEST_JUNIT_OUTPUT: 'output', |
| 57 | + JEST_JUNIT_CLASSNAME: 'class name', |
| 58 | + JEST_JUNIT_TITLE: 'title', |
| 59 | + }; |
| 60 | + |
| 61 | + const envOptionsObject = getEnvOptions(); |
| 62 | + |
| 63 | + expect(envOptionsObject).toBeDefined(); |
| 64 | + expect(envOptionsObject).toEqual(expectedEnvOptionsObject); |
41 | 65 | });
|
| 66 | + }); |
42 | 67 |
|
43 |
| - describe('getEnvOptions', () => { |
44 |
| - test('should return empty envOptions object if environments don\'t set', () => { |
45 |
| - const envOptionsObject = getEnvOptions(); |
46 |
| - |
47 |
| - expect(envOptionsObject).toBeDefined(); |
48 |
| - expect(envOptionsObject).toEqual({}); |
49 |
| - }); |
50 |
| - |
51 |
| - test('should return envOptions object with correct values', () => { |
52 |
| - const expectedEnvOptionsObject = { |
53 |
| - suiteName: 'suite name', |
54 |
| - output: 'output', |
55 |
| - classNameTemplate: 'class name', |
56 |
| - titleTemplate: 'title', |
57 |
| - }; |
58 |
| - process.env = { |
59 |
| - JEST_SUITE_NAME: 'suite name', |
60 |
| - JEST_JUNIT_OUTPUT: 'output', |
61 |
| - JEST_JUNIT_CLASSNAME: 'class name', |
62 |
| - JEST_JUNIT_TITLE: 'title', |
63 |
| - }; |
64 |
| - |
65 |
| - const envOptionsObject = getEnvOptions(); |
66 |
| - |
67 |
| - expect(envOptionsObject).toBeDefined(); |
68 |
| - expect(envOptionsObject).toEqual(expectedEnvOptionsObject); |
69 |
| - }); |
70 |
| - }); |
| 68 | + describe('getAppOptions', () => { |
| 69 | + test('should return empty AppOptions object if fs.existsSync return false', () => { |
| 70 | + const pathToResolve = `${path.sep}path${path.sep}to${path.sep}directory`; |
| 71 | + jest.mock('fs'); |
| 72 | + fs.existsSync = jest.fn(); |
| 73 | + fs.existsSync.mockReturnValue(false); |
71 | 74 |
|
72 |
| - describe('getAppOptions', () => { |
73 |
| - test('should return empty AppOptions object if fs.existsSync return false', () => { |
74 |
| - const pathToResolve = `${path.sep}path${path.sep}to${path.sep}directory`; |
75 |
| - jest.mock('fs'); |
76 |
| - fs.existsSync = jest.fn(); |
77 |
| - fs.existsSync.mockReturnValue(false); |
78 |
| - |
79 |
| - const appOptionsObject = getAppOptions(pathToResolve); |
80 |
| - |
81 |
| - expect(appOptionsObject).toBeDefined(); |
82 |
| - expect(appOptionsObject).toEqual({}); |
83 |
| - }); |
84 |
| - |
85 |
| - test('should return empty AppOptions object if fs.existsSync return true and type of options' |
86 |
| - + ' is not [object Object]', () => { |
87 |
| - const spyProcessCwd = jest.spyOn(process, 'cwd'); |
88 |
| - // eslint-disable-next-line max-len |
89 |
| - spyProcessCwd.mockReturnValue(`${processCwdValue}${path.sep}__tests__${path.sep}fixtures${path.sep}mockedPackageJsonString`); |
90 |
| - jest.mock('fs'); |
91 |
| - fs.existsSync = jest.fn(); |
92 |
| - fs.existsSync.mockReturnValue(true); |
93 |
| - |
94 |
| - const appOptionsObject = getAppOptions(process.cwd()); |
95 |
| - |
96 |
| - expect(appOptionsObject).toBeDefined(); |
97 |
| - expect(appOptionsObject).toEqual({}); |
98 |
| - |
99 |
| - spyProcessCwd.mockClear(); |
100 |
| - }); |
101 |
| - |
102 |
| - test('should return AppOptions object if fs.existsSync return true and type of options' |
103 |
| - + ' is [object Object]', () => { |
104 |
| - const spyProcessCwd = jest.spyOn(process, 'cwd'); |
105 |
| - // eslint-disable-next-line max-len |
106 |
| - spyProcessCwd.mockReturnValue(`${processCwdValue}${path.sep}__tests__${path.sep}fixtures${path.sep}mockedPackageJsonObject`); |
107 |
| - jest.mock('fs'); |
108 |
| - fs.existsSync = jest.fn(); |
109 |
| - fs.existsSync.mockReturnValue(true); |
110 |
| - |
111 |
| - const appOptionsObject = getAppOptions(process.cwd()); |
112 |
| - |
113 |
| - expect(appOptionsObject).toBeDefined(); |
114 |
| - expect(appOptionsObject).toEqual({ keyOne: 'valueOne' }); |
115 |
| - }); |
116 |
| - }); |
| 75 | + const appOptionsObject = getAppOptions(pathToResolve); |
117 | 76 |
|
118 |
| - describe('options', () => { |
119 |
| - test('should return options object with empty reporterOptions object', () => { |
120 |
| - const expectedOptions = { ...constants.DEFAULT_OPTIONS }; |
121 |
| - const spyProcessCwd = jest.spyOn(process, 'cwd'); |
122 |
| - spyProcessCwd.mockReturnValue(processCwdValue); |
123 |
| - |
124 |
| - const optionsObject = options(); |
| 77 | + expect(appOptionsObject).toBeDefined(); |
| 78 | + expect(appOptionsObject).toEqual({}); |
| 79 | + }); |
125 | 80 |
|
126 |
| - expect(optionsObject).toBeDefined(); |
127 |
| - expect(optionsObject).toEqual(expectedOptions); |
128 |
| - }); |
| 81 | + test( |
| 82 | + 'should return empty AppOptions object if fs.existsSync return true and type of options' + |
| 83 | + ' is not [object Object]', |
| 84 | + () => { |
| 85 | + const spyProcessCwd = jest.spyOn(process, 'cwd'); |
| 86 | + // eslint-disable-next-line max-len |
| 87 | + spyProcessCwd.mockReturnValue( |
| 88 | + `${processCwdValue}${path.sep}__tests__${path.sep}fixtures${path.sep}mockedPackageJsonString`, |
| 89 | + ); |
| 90 | + jest.mock('fs'); |
| 91 | + fs.existsSync = jest.fn(); |
| 92 | + fs.existsSync.mockReturnValue(true); |
| 93 | + |
| 94 | + const appOptionsObject = getAppOptions(process.cwd()); |
| 95 | + |
| 96 | + expect(appOptionsObject).toBeDefined(); |
| 97 | + expect(appOptionsObject).toEqual({}); |
| 98 | + |
| 99 | + spyProcessCwd.mockClear(); |
| 100 | + }, |
| 101 | + ); |
| 102 | + |
| 103 | + test( |
| 104 | + 'should return AppOptions object if fs.existsSync return true and type of options' + |
| 105 | + ' is [object Object]', |
| 106 | + () => { |
| 107 | + const spyProcessCwd = jest.spyOn(process, 'cwd'); |
| 108 | + // eslint-disable-next-line max-len |
| 109 | + spyProcessCwd.mockReturnValue( |
| 110 | + `${processCwdValue}${path.sep}__tests__${path.sep}fixtures${path.sep}mockedPackageJsonObject`, |
| 111 | + ); |
| 112 | + jest.mock('fs'); |
| 113 | + fs.existsSync = jest.fn(); |
| 114 | + fs.existsSync.mockReturnValue(true); |
| 115 | + |
| 116 | + const appOptionsObject = getAppOptions(process.cwd()); |
| 117 | + |
| 118 | + expect(appOptionsObject).toBeDefined(); |
| 119 | + expect(appOptionsObject).toEqual({ keyOne: 'valueOne' }); |
| 120 | + }, |
| 121 | + ); |
| 122 | + }); |
| 123 | + |
| 124 | + describe('options', () => { |
| 125 | + test('should return options object with empty reporterOptions object', () => { |
| 126 | + const expectedOptions = { ...constants.DEFAULT_OPTIONS }; |
| 127 | + const spyProcessCwd = jest.spyOn(process, 'cwd'); |
| 128 | + spyProcessCwd.mockReturnValue(processCwdValue); |
| 129 | + |
| 130 | + const optionsObject = options(); |
| 131 | + |
| 132 | + expect(optionsObject).toBeDefined(); |
| 133 | + expect(optionsObject).toEqual(expectedOptions); |
129 | 134 | });
|
| 135 | + }); |
130 | 136 | });
|
0 commit comments