forked from vebr/jest-lcov-reporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index_test.js
230 lines (185 loc) · 5.09 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
import core from "@actions/core"
import { promises as fs } from "fs"
import { context, GitHub } from "@actions/github"
import { parse } from "../lcov"
import { commentIdentifier, diff } from "../comment"
jest.mock("@actions/core", () => ({
getInput: jest.fn(),
getBooleanInput: jest.fn(),
setFailed: jest.fn(),
}))
jest.mock("@actions/github", () => ({
GitHub: jest.fn(),
context: {
payload: {
repository: { full_name: "name" },
pull_request: {},
},
repo: {
repo: "repo",
owner: "owner",
},
},
}))
jest.mock("fs", () => ({
promises: {
readFile: jest.fn(),
},
}))
jest.mock("../lcov")
jest.mock("../comment")
let updateComment = false
beforeEach(() => {
core.getInput.mockImplementation(arg => {
if (arg === "github-token") return "GITHUB_TOKEN"
if (arg === "name") return "NAME"
if (arg === "lcov-file") return "LCOV_FILE"
if (arg === "lcov-base") return "LCOV_BASE"
return ""
})
core.getBooleanInput.mockImplementation(arg => {
if (arg === "update-comment") return updateComment
return false
})
context.payload.pull_request = {
base: { ref: "base-ref" },
head: { ref: "head-ref" },
number: 1,
}
parse.mockReturnValueOnce(Promise.resolve("report"))
diff.mockReturnValueOnce(Promise.resolve("diff"))
})
afterEach(() => {
jest.clearAllMocks()
})
test("it logs a message when lcov file does not exist", async () => {
jest.spyOn(console, "log")
fs.readFile.mockReturnValue(Promise.resolve(null))
let module
jest.isolateModules(() => {
module = require("../index").default
})
await module
expect(console.log).toHaveBeenCalledWith(
"No coverage report found at 'LCOV_FILE', exiting...",
)
})
test("it catches and logs if an error occurs", async () => {
jest.spyOn(console, "log")
fs.readFile
.mockReturnValueOnce(Promise.resolve("file"))
.mockReturnValueOnce(Promise.resolve(null))
const error = new Error("Something went wrong...")
const createCommentMock = jest.fn().mockReturnValue(Promise.reject(error))
GitHub.mockReturnValue({
issues: {
createComment: createCommentMock,
},
})
let module
jest.isolateModules(() => {
module = require("../index").default
})
await module
expect(console.log).toHaveBeenCalledWith(error)
expect(core.setFailed).toHaveBeenCalledWith(error.message)
})
test("when a non pull_request event is passed it logs a message", async () => {
jest.spyOn(console, "log")
context.payload.pull_request = null
fs.readFile
.mockReturnValueOnce(Promise.resolve("file"))
.mockReturnValueOnce(Promise.resolve(null))
const createCommentMock = jest.fn().mockReturnValue(Promise.resolve())
const updateCommentMock = jest.fn().mockReturnValue(Promise.resolve())
GitHub.mockReturnValue({
issues: {
createComment: createCommentMock,
updateComment: updateCommentMock,
},
})
let module
jest.isolateModules(() => {
module = require("../index").default
})
await module
expect(console.log).toHaveBeenCalledWith("Not a pull request, skipping...")
})
test("a comment is created on the pull request with the coverage details", async () => {
updateComment = false
fs.readFile
.mockReturnValueOnce(Promise.resolve("file"))
.mockReturnValueOnce(Promise.resolve(null))
const createCommentMock = jest.fn().mockReturnValue(Promise.resolve())
GitHub.mockReturnValue({
issues: {
createComment: createCommentMock,
},
})
let module
jest.isolateModules(() => {
module = require("../index").default
})
await module
expect(createCommentMock).toHaveBeenCalledWith({
repo: "repo",
owner: "owner",
issue_number: 1,
body: "diff",
})
})
describe("when update-comment is enabled", () => {
test("a comment is created on the pull request when there is no comment to update", async () => {
updateComment = true
fs.readFile
.mockReturnValueOnce(Promise.resolve("file"))
.mockReturnValueOnce(Promise.resolve(null))
const createCommentMock = jest.fn().mockReturnValue(Promise.resolve())
GitHub.mockReturnValue({
issues: {
createComment: createCommentMock,
listComments: jest.fn().mockReturnValue(Promise.resolve({ data: [] })),
},
})
let module
jest.isolateModules(() => {
module = require("../index").default
})
await module
expect(createCommentMock).toHaveBeenCalledWith({
repo: "repo",
owner: "owner",
issue_number: 1,
body: "diff",
})
})
test("a comment is updated on the pull request when there is already a comment posted", async () => {
updateComment = true
fs.readFile
.mockReturnValueOnce(Promise.resolve("file"))
.mockReturnValueOnce(Promise.resolve(null))
commentIdentifier.mockReturnValueOnce("COMMENT ID")
const updateCommentMock = jest.fn().mockReturnValue(Promise.resolve())
GitHub.mockReturnValue({
issues: {
updateComment: updateCommentMock,
listComments: jest.fn().mockReturnValue(
Promise.resolve({
data: [{ id: 1, body: "Some content <!-- COMMENT ID -->" }],
}),
),
},
})
let module
jest.isolateModules(() => {
module = require("../index").default
})
await module
expect(updateCommentMock).toHaveBeenCalledWith({
repo: "repo",
owner: "owner",
comment_id: 1,
body: "diff",
})
})
})