-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.test.js
239 lines (218 loc) · 6.74 KB
/
index.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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/* eslint-env jest */
const util = require('util')
const extendJestWithShellMatchers = require('./index')
const { runAllTestCasesForInput } = require('./src/matchers-test-utils')
beforeAll(() => {
extendJestWithShellMatchers()
})
describe('initialization', () => {
it('should throw when jest version >= 23 is not found', () => {
const jestModule = require('jest')
const getVersionSpy = jest.spyOn(jestModule, 'getVersion')
const extendSpy = jest.spyOn(expect, 'extend')
extendSpy.mockImplementation(() => undefined)
getVersionSpy.mockImplementationOnce(() => undefined)
expect(extendJestWithShellMatchers).toThrowErrorMatchingInlineSnapshot(
`"jest-shell-matchers requires jest version >= 23"`
)
expect(getVersionSpy).toHaveBeenCalledTimes(1)
getVersionSpy.mockImplementationOnce(() => '22.00.99')
expect(extendJestWithShellMatchers).toThrowErrorMatchingInlineSnapshot(
`"jest-shell-matchers requires jest version >= 23"`
)
expect(getVersionSpy).toHaveBeenCalledTimes(2)
getVersionSpy.mockImplementationOnce(() => '23.00.00')
expect(extendJestWithShellMatchers).not.toThrow()
expect(getVersionSpy).toHaveBeenCalledTimes(3)
getVersionSpy.mockRestore()
extendSpy.mockRestore()
})
})
describe('toHaveMatchingSpawnOutput - no mocks', () => {
it('expecting a Number', async () => {
const output = await runAllTestCasesForInput({
input: ['sh', ['./test-scripts/exit-0.sh']],
correctOutput: 0,
incorrectOutput: 1
})
expect(output.correctMessageData.pass).toBe(true)
expect(output.correctMessageData.message()).toMatchInlineSnapshot(`
"MATCHER HINT: .not.toBe
Expected exit code not to be:
EXPECTED: 0
Received:
RECEIVED: 0"
`)
expect(output.incorrectMessageData.pass).toBe(false)
expect(output.incorrectMessageData.message()).toMatchInlineSnapshot(`
"MATCHER HINT: .toBe
Expected exit code to be:
EXPECTED: 1
Received:
RECEIVED: 0"
`)
})
it('expecting a String', async () => {
const output = await runAllTestCasesForInput({
input: ['sh', ['./hello-world.sh']],
correctOutput: `Hello World
`,
incorrectOutput: `Jim Bob
Crinkleberry`
})
expect(output.correctMessageData.pass).toBe(true)
expect(output.correctMessageData.message()).toMatchInlineSnapshot(`
"MATCHER HINT: .not.toBe
Expected stdout not to be:
EXPECTED: \\"Hello World\\\\n\\"
Received:
RECEIVED: \\"Hello World\\\\n\\""
`)
expect(output.incorrectMessageData.pass).toBe(false)
expect(output.incorrectMessageData.message()).toMatchInlineSnapshot(`
"MATCHER HINT: .toBe
Expected stdout to be:
EXPECTED: \\"Jim Bob\\\\nCrinkleberry\\"
Received:
RECEIVED: \\"Hello World\\\\n\\""
`)
})
it('expecting a RegExp', async () => {
const output = await runAllTestCasesForInput({
input: ['sh', ['./hello-world.sh']],
correctOutput: /^Hello World/,
incorrectOutput: /^Jim Bob/
})
expect(output.correctMessageData.pass).toBe(true)
expect(output.correctMessageData.message()).toMatchInlineSnapshot(`
"MATCHER HINT: .not.toEqual
Expected stdout not to be:
EXPECTED: {}
Received:
RECEIVED: \\"Hello World\\\\n\\""
`)
expect(output.incorrectMessageData.pass).toBe(false)
expect(output.incorrectMessageData.message()).toMatchInlineSnapshot(`
"MATCHER HINT: .toEqual
Expected stdout to be:
EXPECTED: {}
Received:
RECEIVED: \\"Hello World\\\\n\\""
`)
})
it('expecting an Object', async () => {
const output = await runAllTestCasesForInput({
input: ['sh', ['./hello-world.sh']],
correctOutput: {
code: 0,
stdout: /^Hello World/,
stderr: ''
},
incorrectOutput: {
code: 0,
stdout: /^Jim Bob/,
stderr: ''
}
})
expect(output.correctMessageData.pass).toBe(true)
expect(output.correctMessageData.message()).toMatchInlineSnapshot(`
"MATCHER HINT: .not.toEqual
Expected not to be:
EXPECTED: {\\"code\\":0,\\"stdout\\":{},\\"stderr\\":\\"\\"}
Received:
RECEIVED: {\\"code\\":0,\\"signal\\":\\"\\",\\"stdout\\":\\"Hello World\\\\n\\",\\"stderr\\":\\"\\"}"
`)
expect(output.incorrectMessageData.pass).toBe(false)
expect(output.incorrectMessageData.message()).toMatchInlineSnapshot(`
"MATCHER HINT: .toEqual
Expected:
EXPECTED: {\\"code\\":0,\\"stdout\\":{},\\"stderr\\":\\"\\"}
Received:
RECEIVED: {\\"code\\":0,\\"signal\\":\\"\\",\\"stdout\\":\\"Hello World\\\\n\\",\\"stderr\\":\\"\\"}"
`)
})
})
describe('toHaveMatchingSpawnOutput - with mocks', () => {
it('should mock the ls command', async () => {
const ls = jest.fn(input => {
return {
exit: 0,
stdout: ['frog', 'shrimp', 'crab'].join('\n'),
stderr: ''
}
})
const output = await runAllTestCasesForInput({
input: [
'sh',
['./test-scripts/ls-grep-frog.sh'],
{
mocks: {
ls
}
}
],
correctOutput: {
code: 0,
stdout: `frog
`
},
incorrectOutput: 1
})
expect(ls).toHaveBeenCalledTimes(4)
expect(output.correctMessageData.pass).toBe(true)
expect(output.correctMessageData.message()).toMatchInlineSnapshot(`
"MATCHER HINT: .not.toEqual
Expected not to be:
EXPECTED: {\\"code\\":0,\\"stdout\\":\\"frog\\\\n\\"}
Received:
RECEIVED: {\\"code\\":0,\\"signal\\":\\"\\",\\"stdout\\":\\"frog\\\\n\\",\\"stderr\\":\\"\\"}"
`)
expect(output.incorrectMessageData.pass).toBe(false)
expect(output.incorrectMessageData.message()).toMatchInlineSnapshot(`
"MATCHER HINT: .toBe
Expected exit code to be:
EXPECTED: 1
Received:
RECEIVED: 0"
`)
})
})
describe('toHaveMatchingSpawnOutput - with args and options', () => {
it('should set an environment variable and pass arguments to the command', async () => {
const output = await runAllTestCasesForInput({
input: [
'sh',
['./test-scripts/input-output.sh', 124],
{
env: {
TO_STDOUT: 'Pancake Soup',
TO_STDERR: 'Strawberry Spaceship'
}
}
],
correctOutput: {
code: 124,
stdout: `Pancake Soup
`,
stderr: /^Strawberry Spaceship/
},
incorrectOutput: 1
})
expect(output.correctMessageData.pass).toBe(true)
expect(output.correctMessageData.message()).toMatchInlineSnapshot(`
"MATCHER HINT: .not.toEqual
Expected not to be:
EXPECTED: {\\"code\\":124,\\"stdout\\":\\"Pancake Soup\\\\n\\",\\"stderr\\":{}}
Received:
RECEIVED: {\\"code\\":124,\\"signal\\":\\"\\",\\"stdout\\":\\"Pancake Soup\\\\n\\",\\"stderr\\":\\"Strawberry Spaceship\\\\n\\"}"
`)
expect(output.incorrectMessageData.pass).toBe(false)
expect(output.incorrectMessageData.message()).toMatchInlineSnapshot(`
"MATCHER HINT: .toBe
Expected exit code to be:
EXPECTED: 1
Received:
RECEIVED: 124"
`)
})
})