Repository navigation
Expand file tree
/
Copy pathassert.test.js
More file actions
201 lines (147 loc) · 7.6 KB
/
Copy pathassert.test.js
File metadata and controls
201 lines (147 loc) · 7.6 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
const {checkResult, parseResult, assertResult, getExpectedResult} = require('../assert')
describe('parseResult', ()=>{
it('should parsedResult correctly', ()=>{
const resultString = `[{"location":"US","locationType":"multi-region","name":"stackql-public-releases"}]`
const expected = [{"location":"US","locationType":"multi-region","name":"stackql-public-releases"}]
const actual = parseResult(resultString);
expect(actual).toEqual(expected);
})
})
describe('getExpectedResult', ()=>{
it('should return expectedResult when expectedResultStr is passed', ()=>{
const expectedResultStr = `[{"location":"US","locationType":"multi-region","name":"stackql-public-releases"}]`
const expectedResult = [{"location":"US","locationType":"multi-region","name":"stackql-public-releases"}]
const actual = getExpectedResult(expectedResultStr, undefined);
expect(actual).toEqual(expectedResult);
})
it('should return expectedResult when expectedResultFilePath is passed', ()=>{
const expectedResultFilePath = 'lib/tests/success-result.json'
const expectedResult = [{"location":"US","locationType":"multi-region","name":"stackql-public-releases"}]
const actual = getExpectedResult(undefined, expectedResultFilePath);
expect(actual).toEqual(expectedResult);
})
})
describe('checkResult', () => {
const core = {
info: jest.fn(),
error: jest.fn(),
setFailed: jest.fn()
};
beforeEach(() => {
core.info.mockClear();
core.error.mockClear();
core.setFailed.mockClear();
});
it('should return false and log an error when the actual length does not match expected rows', () => {
const expectedRows = "3";
const actualResult = [{}, {}, {}, {}]; // 4 items, should not match expectedRows
const result = checkResult(core, undefined, actualResult, expectedRows);
expect(result).toBe(false);
expect(core.error).toHaveBeenCalledWith(`Expected rows: ${expectedRows}, got: ${actualResult.length}`);
});
it('should return true when the actual length matches expected rows', () => {
const expectedRows = "2";
const actualResult = [{}, {}]; // 2 items, matches expectedRows
const result = checkResult(core, undefined, actualResult, expectedRows);
expect(result).toBe(true);
expect(core.error).not.toHaveBeenCalled();
});
it('should return false and log an error when expected does not match actual and expectedRows is undefined', () => {
const expected = [{ name: "test1" }, { name: "test2" }];
const actual = [{ name: "test1" }, { name: "test3" }];
const result = checkResult(core, expected, actual, undefined);
expect(result).toBe(false);
expect(core.error).toHaveBeenCalledWith(`Expected results do not match actual results.\nExpected: ${JSON.stringify(expected)}\nActual: ${JSON.stringify(actual)}`);
});
it('should return true when expected matches actual and expectedRows is undefined', () => {
const expected = [{ name: "test1" }, { name: "test2" }];
const actual = [{ name: "test1" }, { name: "test2" }];
const result = checkResult(core, expected, actual, undefined);
expect(result).toBe(true);
expect(core.error).not.toHaveBeenCalled();
});
});
describe('assertResult', ()=>{
let coreObj;
const ACTION_ENV = {
RESULT: `[{"location":"US","locationType":"multi-region","name":"stackql-public-releases"}]`,
EXPECTED_RESULTS_STR: `[{"location":"US","locationType":"multi-region","name":"stackql-public-releases"}]`,
EXPECTED_RESULTS_FILE_PATH: 'test.json',
EXPECTED_ROWS: 1
}
beforeEach(() => {
jest.resetModules()
process.env = {...ACTION_ENV}
coreObj = {
setFailed: jest.fn(),
info: jest.fn(),
error: jest.fn(),
}
})
afterEach(() => {
process.env = ACTION_ENV
})
it('it should setFailed when there is expected results are undefined', () => {
process.env.EXPECTED_RESULTS_FILE_PATH = undefined
process.env.EXPECTED_RESULTS_STR = undefined
process.env.EXPECTED_ROWS = undefined
assertResult(coreObj)
expect(coreObj.setFailed).toHaveBeenCalledWith('Assertion error: ❌ Cannot find expected result, file path or expected rows')
});
it('it should setFailed when actual result is not equal to expected result', () => {
process.env.RESULT= "[{\"location\":\"EU\",\"locationType\":\"multi-region\",\"name\":\"stackql-public-releases\"}]"
assertResult(coreObj)
expect(coreObj.setFailed).toHaveBeenCalledWith(expect.stringContaining('StackQL Assert Failed'))
});
it('it should not setFailed when actual result equal to expected result', () => {
assertResult(coreObj)
expect(coreObj.setFailed).not.toHaveBeenCalled()
expect(coreObj.info).toHaveBeenCalledWith(expect.stringContaining('StackQL Assert Successful'))
});
it('it should setFailed when actual result is not equal to expected result', () => {
process.env.EXPECTED_RESULTS_STR= undefined;
process.env.EXPECTED_RESULTS_FILE_PATH = 'lib/tests/failed-result.json'
assertResult(coreObj)
expect(coreObj.setFailed).toHaveBeenCalledWith(expect.stringContaining('StackQL Assert Failed'))
});
it('it should not setFailed when actual result equal to expected result in file', () => {
process.env.EXPECTED_RESULTS_STR= undefined;
process.env.EXPECTED_RESULTS_FILE_PATH = 'lib/tests/success-result.json'
assertResult(coreObj)
expect(coreObj.setFailed).not.toHaveBeenCalled()
expect(coreObj.info).toHaveBeenCalledWith(expect.stringContaining('StackQL Assert Successful'))
})
it('it should setFailed when actual result does not match expected rows', () => {
process.env.EXPECTED_RESULTS_STR= undefined;
process.env.EXPECTED_RESULTS_FILE_PATH = undefined;
process.env.EXPECTED_ROWS = 2
assertResult(coreObj)
expect(coreObj.setFailed).toHaveBeenCalledWith(expect.stringContaining('StackQL Assert Failed'))
});
it('it should not setFailed when actual result match expected rows', ()=>{
process.env.EXPECTED_RESULTS_STR= undefined;
process.env.EXPECTED_RESULTS_FILE_PATH = undefined;
process.env.EXPECTED_ROWS = 1
assertResult(coreObj)
expect(coreObj.setFailed).not.toHaveBeenCalled()
expect(coreObj.info).toHaveBeenCalledWith(expect.stringContaining('StackQL Assert Successful'))
})
it('it should not setFailed when actual result match expected rows in string number', ()=>{
process.env.EXPECTED_RESULTS_STR= undefined;
process.env.EXPECTED_RESULTS_FILE_PATH = undefined;
process.env.EXPECTED_ROWS = '1'
assertResult(coreObj)
expect(coreObj.setFailed).not.toHaveBeenCalled()
expect(coreObj.info).toHaveBeenCalledWith(expect.stringContaining('StackQL Assert Successful'))
})
it('it should fail cleanly (not crash) when the query returns null and rows are expected', ()=>{
// stackql emits the literal `null` for an empty result set
process.env.RESULT = 'null'
process.env.EXPECTED_RESULTS_STR = undefined;
process.env.EXPECTED_RESULTS_FILE_PATH = undefined;
process.env.EXPECTED_ROWS = 1
assertResult(coreObj)
expect(coreObj.setFailed).toHaveBeenCalledWith(expect.stringContaining('StackQL Assert Failed'))
expect(coreObj.error).toHaveBeenCalledWith('Expected rows: 1, got: 0')
})
})