pagean 4.4.2 → 6.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +9 -9
- package/docs/upgrade-guide.md +16 -0
- package/index.js +7 -4
- package/lib/config.js +2 -2
- package/lib/default-config.json +41 -39
- package/lib/externalFileUtils.js +1 -1
- package/lib/linkUtils.js +162 -25
- package/lib/logger.js +1 -1
- package/lib/report-template.handlebars +224 -209
- package/lib/schemaErrors.js +5 -4
- package/lib/testUtils.js +1 -1
- package/lib/tests.js +14 -14
- package/package.json +28 -30
- package/schemas/pageanrc.schema.json +199 -191
- package/.codeclimate.json +0 -8
- package/.depcheckrc.json +0 -3
- package/.dockerignore +0 -12
- package/.eslintrc.json +0 -19
- package/.gitattributes +0 -4
- package/.gitlab/gitlab-releaser.json +0 -16
- package/.htmlhintrc +0 -25
- package/.markdownlint.json +0 -13
- package/.pa11yci.json +0 -5
- package/.pageanrc.json +0 -55
- package/.stylelintrc.json +0 -7
- package/CHANGELOG.md +0 -204
- package/gitlab.pageanrc.json +0 -16
- package/jest.config.json +0 -22
- package/static-server.pageanrc.json +0 -50
- package/tests/__snapshots__/config.test.js.snap +0 -591
- package/tests/__snapshots__/externalFileUtils.test.js.snap +0 -7
- package/tests/__snapshots__/index.test.js.snap +0 -30
- package/tests/__snapshots__/pagean.test.js.snap +0 -82
- package/tests/__snapshots__/pageanrc.test.js.snap +0 -759
- package/tests/__snapshots__/reporter.test.js.snap +0 -509
- package/tests/config.test.js +0 -264
- package/tests/externalFileUtils.test.js +0 -184
- package/tests/index.test.js +0 -181
- package/tests/linkUtils.test.js +0 -290
- package/tests/logger.test.js +0 -324
- package/tests/pagean.test.js +0 -85
- package/tests/pageanrc.test.js +0 -96
- package/tests/reporter.test.js +0 -82
- package/tests/schemaErrors.test.js +0 -88
- package/tests/test-cases/.htmlhintrc +0 -25
- package/tests/test-cases/brokenLinks.html +0 -22
- package/tests/test-cases/consoleLog.html +0 -11
- package/tests/test-cases/duplicateLinks.html +0 -22
- package/tests/test-cases/dynamicContent.html +0 -13
- package/tests/test-cases/externalScripts.html +0 -21
- package/tests/test-cases/horizontalScrollbar.html +0 -13
- package/tests/test-cases/htmlError.html +0 -11
- package/tests/test-cases/noExternalScripts.html +0 -12
- package/tests/test-cases/notDocumentLinks.html +0 -18
- package/tests/test-cases/pagean-results.json +0 -1
- package/tests/test-cases/scriptError404.html +0 -9
- package/tests/test-cases/slowLoad.html +0 -9
- package/tests/test-configs/cli-tests/.pageanrc.json +0 -28
- package/tests/test-configs/cli-tests/all-empty.pageanrc.json +0 -10
- package/tests/test-configs/cli-tests/all-errors.pageanrc.json +0 -69
- package/tests/test-configs/cli-tests/all-fail-cli.pageanrc.json +0 -9
- package/tests/test-configs/cli-tests/no-urls.pageanrc.json +0 -7
- package/tests/test-configs/cli-tests/pass-fail-cli.pageanrc.json +0 -14
- package/tests/test-configs/integration-tests/all-passing-tests.pageanrc.json +0 -63
- package/tests/test-configs/integration-tests/broken-links-error.pageanrc.json +0 -22
- package/tests/test-configs/integration-tests/console-error-reporter-cli-json.pageanrc.json +0 -20
- package/tests/test-configs/integration-tests/console-error-reporter-html.pageanrc.json +0 -19
- package/tests/test-configs/integration-tests/console-error.pageanrc.json +0 -16
- package/tests/test-configs/integration-tests/console-output.pageanrc.json +0 -16
- package/tests/test-configs/integration-tests/external-scripts-error.pageanrc.json +0 -19
- package/tests/test-configs/integration-tests/horizontal-scrollbar.pageanrc.json +0 -16
- package/tests/test-configs/integration-tests/html-error.pageanrc.json +0 -16
- package/tests/test-configs/integration-tests/page-load-time.pageanrc.json +0 -16
- package/tests/test-configs/unit-tests/empty-url-values.pageanrc.json +0 -9
- package/tests/test-configs/unit-tests/empty-urls.pageanrc.json +0 -3
- package/tests/test-configs/unit-tests/empty.pageanrc.json +0 -2
- package/tests/test-configs/unit-tests/global-and-test-specific-settings-shorthand.pageanrc.json +0 -30
- package/tests/test-configs/unit-tests/global-and-test-specific-settings-test-props.pageanrc.json +0 -42
- package/tests/test-configs/unit-tests/global-and-test-specific-settings.pageanrc.json +0 -34
- package/tests/test-configs/unit-tests/global-test-settings.pageanrc.json +0 -15
- package/tests/test-configs/unit-tests/htmlhintrc-invalid.pageanrc.json +0 -7
- package/tests/test-configs/unit-tests/htmlhintrc-valid.pageanrc.json +0 -7
- package/tests/test-configs/unit-tests/ignored-links-denormalized.pageanrc.json +0 -18
- package/tests/test-configs/unit-tests/invalid.pageanrc.json +0 -0
- package/tests/test-configs/unit-tests/no-test-settings.pageanrc.json +0 -6
- package/tests/test-configs/unit-tests/project-no-test-settings.pageanrc.json +0 -7
- package/tests/test-configs/unit-tests/puppeteer-no-test-settings.pageanrc.json +0 -9
- package/tests/test-configs/unit-tests/reporters-empty.pageanrc.json +0 -7
- package/tests/test-configs/unit-tests/reporters-invalid-type.pageanrc.json +0 -9
- package/tests/test-configs/unit-tests/reporters-not-array.pageanrc.json +0 -6
- package/tests/test-configs/unit-tests/reporters-valid.pageanrc.json +0 -9
- package/tests/test-configs/unit-tests/test-specific-settings.pageanrc.json +0 -26
- package/tests/test-configs/unit-tests/url-types.pageanrc.json +0 -10
- package/tests/testUtils.test.js +0 -244
- package/tests/tests.test.js +0 -533
package/tests/linkUtils.test.js
DELETED
|
@@ -1,290 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const axios = require('axios');
|
|
4
|
-
const puppeteer = require('puppeteer');
|
|
5
|
-
const protocolify = require('protocolify');
|
|
6
|
-
|
|
7
|
-
const { checkLink, httpResponse, isFailedResponse, normalizeLink } = require('../lib/linkUtils');
|
|
8
|
-
|
|
9
|
-
// eslint-disable-next-line max-lines-per-function
|
|
10
|
-
describe('check link', () => {
|
|
11
|
-
const testUrl = './tests/test-cases/brokenLinks.html';
|
|
12
|
-
const testUrlLink = protocolify(testUrl);
|
|
13
|
-
const userAgent = 'pagean test user-agent';
|
|
14
|
-
const puppeteerLaunchOptions = { args: ['--no-sandbox'] };
|
|
15
|
-
let browser, page;
|
|
16
|
-
|
|
17
|
-
const getContext = (ignoredLinks = []) => {
|
|
18
|
-
return {
|
|
19
|
-
testSettings: {
|
|
20
|
-
ignoredLinks
|
|
21
|
-
},
|
|
22
|
-
page
|
|
23
|
-
};
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
beforeAll(async() => {
|
|
27
|
-
browser = await puppeteer.launch(puppeteerLaunchOptions);
|
|
28
|
-
page = await browser.newPage();
|
|
29
|
-
page.setUserAgent(userAgent);
|
|
30
|
-
await page.goto(testUrlLink, { waitUntil: 'load' });
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
afterEach(() => {
|
|
34
|
-
jest.restoreAllMocks();
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
afterAll(async() => {
|
|
38
|
-
await page.close();
|
|
39
|
-
await browser.close();
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
// eslint-disable-next-line max-lines-per-function
|
|
43
|
-
const checkLinksWithMocks = async(link, ignoredLinks, invalidPage = false, httpStatus = httpResponse.ok) => {
|
|
44
|
-
const context = getContext(ignoredLinks);
|
|
45
|
-
if (invalidPage) {
|
|
46
|
-
// This enables forcing an error to be thrown for various test cases
|
|
47
|
-
delete context.page;
|
|
48
|
-
}
|
|
49
|
-
const pageGetElementSpy = jest.spyOn(page, '$');
|
|
50
|
-
const pageEvaluateSpy = jest.spyOn(page, 'evaluate');
|
|
51
|
-
|
|
52
|
-
const axiosMock = () => {
|
|
53
|
-
if (isNaN(httpStatus)) {
|
|
54
|
-
// If execution error, format to match axios error
|
|
55
|
-
const err = new Error();
|
|
56
|
-
err.code = httpStatus;
|
|
57
|
-
throw err;
|
|
58
|
-
}
|
|
59
|
-
else if (httpStatus >= httpResponse.badRequest) {
|
|
60
|
-
// If erroneous response, throw error with response to match axios
|
|
61
|
-
const err = new Error();
|
|
62
|
-
err.response = { status: httpStatus };
|
|
63
|
-
throw err;
|
|
64
|
-
}
|
|
65
|
-
return { status: httpStatus };
|
|
66
|
-
};
|
|
67
|
-
const axiosHeadSpy = jest.spyOn(axios, 'head').mockImplementation(axiosMock);
|
|
68
|
-
const axiosGetSpy = jest.spyOn(axios, 'get').mockImplementation(axiosMock);
|
|
69
|
-
|
|
70
|
-
const result = await checkLink(context, link);
|
|
71
|
-
|
|
72
|
-
return {
|
|
73
|
-
axiosGetSpy,
|
|
74
|
-
axiosHeadSpy,
|
|
75
|
-
pageEvaluateSpy,
|
|
76
|
-
pageGetElementSpy,
|
|
77
|
-
result
|
|
78
|
-
};
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
// eslint-disable-next-line max-lines-per-function
|
|
82
|
-
describe('ignored links', () => {
|
|
83
|
-
const ignoreLinksTestLink = 'https://this.is.a.test/';
|
|
84
|
-
const normalizedIgnoreLinksTestLink = 'https://this.is.a.test';
|
|
85
|
-
|
|
86
|
-
it('should return 100 if exact link is in ignored links', async() => {
|
|
87
|
-
expect.assertions(1);
|
|
88
|
-
const ignoredLinks = [normalizedIgnoreLinksTestLink];
|
|
89
|
-
const testResults = await checkLinksWithMocks(ignoreLinksTestLink, ignoredLinks);
|
|
90
|
-
expect(testResults.result).toStrictEqual(httpResponse.continue);
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
it('should return 100 if normalized link is in ignored links', async() => {
|
|
94
|
-
expect.assertions(1);
|
|
95
|
-
const ignoredLinks = [normalizedIgnoreLinksTestLink];
|
|
96
|
-
const testResults = await checkLinksWithMocks(normalizedIgnoreLinksTestLink, ignoredLinks);
|
|
97
|
-
expect(testResults.result).toStrictEqual(httpResponse.continue);
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
it('should not return 100 is link is not in ignored links', async() => {
|
|
101
|
-
expect.assertions(1);
|
|
102
|
-
const ignoredLinks = ['https://this.is.not.a.test/'];
|
|
103
|
-
const testResults = await checkLinksWithMocks(ignoreLinksTestLink, ignoredLinks);
|
|
104
|
-
expect(testResults.result).not.toStrictEqual(httpResponse.continue);
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
it('should not return 100 is ignored links are not specified', async() => {
|
|
108
|
-
expect.assertions(1);
|
|
109
|
-
const testResults = await checkLinksWithMocks(ignoreLinksTestLink);
|
|
110
|
-
expect(testResults.result).not.toStrictEqual(httpResponse.continue);
|
|
111
|
-
});
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
// eslint-disable-next-line max-lines-per-function
|
|
115
|
-
describe('same page links', () => {
|
|
116
|
-
it('should return 200 for url with # and no element (top of page)', async() => {
|
|
117
|
-
expect.assertions(1);
|
|
118
|
-
const link = `${testUrlLink}#`;
|
|
119
|
-
const testResults = await checkLinksWithMocks(link);
|
|
120
|
-
expect(testResults.result).toStrictEqual(httpResponse.ok);
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
it('should return 200 for url with #top', async() => {
|
|
124
|
-
expect.assertions(1);
|
|
125
|
-
const link = `${testUrlLink}#top`;
|
|
126
|
-
const testResults = await checkLinksWithMocks(link);
|
|
127
|
-
expect(testResults.result).toStrictEqual(httpResponse.ok);
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
it('should check page for element if element specified', async() => {
|
|
131
|
-
expect.assertions(1);
|
|
132
|
-
const element = '#foo';
|
|
133
|
-
const link = `${testUrlLink}${element}`;
|
|
134
|
-
const testResults = await checkLinksWithMocks(link);
|
|
135
|
-
expect(testResults.pageGetElementSpy).toHaveBeenCalledWith(element);
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
it('should return 200 if element found on page', async() => {
|
|
139
|
-
expect.assertions(1);
|
|
140
|
-
const element = '#linked';
|
|
141
|
-
const link = `${testUrlLink}${element}`;
|
|
142
|
-
const testResults = await checkLinksWithMocks(link);
|
|
143
|
-
expect(testResults.result).toStrictEqual(httpResponse.ok);
|
|
144
|
-
});
|
|
145
|
-
|
|
146
|
-
it('should return #element not found error if element is not found on page', async() => {
|
|
147
|
-
expect.assertions(1);
|
|
148
|
-
const element = '#notlinked';
|
|
149
|
-
const link = `${testUrlLink}${element}`;
|
|
150
|
-
const testResults = await checkLinksWithMocks(link);
|
|
151
|
-
expect(testResults.result).toStrictEqual(`${element} Not Found`);
|
|
152
|
-
});
|
|
153
|
-
|
|
154
|
-
it('should return 999 if error thrown while checking link', async() => {
|
|
155
|
-
expect.assertions(1);
|
|
156
|
-
const element = '#linked';
|
|
157
|
-
const link = `${testUrlLink}${element}`;
|
|
158
|
-
const testResults = await checkLinksWithMocks(link, [], true);
|
|
159
|
-
expect(testResults.result).toStrictEqual(httpResponse.unknownError);
|
|
160
|
-
});
|
|
161
|
-
});
|
|
162
|
-
|
|
163
|
-
// eslint-disable-next-line max-lines-per-function
|
|
164
|
-
describe('external page links', () => {
|
|
165
|
-
const externalLink = 'https://www.google.com';
|
|
166
|
-
|
|
167
|
-
it('should make only a HEAD request for link if not same page and response is valid', async() => {
|
|
168
|
-
expect.assertions(2); // eslint-disable-line no-magic-numbers
|
|
169
|
-
const testResults = await checkLinksWithMocks(externalLink);
|
|
170
|
-
expect(testResults.axiosHeadSpy.mock.calls[0][0]).toStrictEqual(externalLink);
|
|
171
|
-
expect(testResults.axiosGetSpy).not.toHaveBeenCalled();
|
|
172
|
-
});
|
|
173
|
-
|
|
174
|
-
it('should make only a HEAD request for link if not same page and HEAD returns code 429', async() => {
|
|
175
|
-
expect.assertions(2); // eslint-disable-line no-magic-numbers
|
|
176
|
-
const testResults = await checkLinksWithMocks(externalLink, [], false, httpResponse.tooManyRequests);
|
|
177
|
-
expect(testResults.axiosHeadSpy.mock.calls[0][0]).toStrictEqual(externalLink);
|
|
178
|
-
expect(testResults.axiosGetSpy).not.toHaveBeenCalled();
|
|
179
|
-
});
|
|
180
|
-
|
|
181
|
-
it('should make only a HEAD request for link if not same page and HEAD returns an execution error', async() => {
|
|
182
|
-
expect.assertions(2); // eslint-disable-line no-magic-numbers
|
|
183
|
-
const responseCode = 'ENOTFOUND';
|
|
184
|
-
const testResults = await checkLinksWithMocks(externalLink, [], false, responseCode);
|
|
185
|
-
expect(testResults.axiosHeadSpy.mock.calls[0][0]).toStrictEqual(externalLink);
|
|
186
|
-
expect(testResults.axiosGetSpy).not.toHaveBeenCalled();
|
|
187
|
-
});
|
|
188
|
-
|
|
189
|
-
it('should make only a HEAD request passing the current page user agent if response is valid', async() => {
|
|
190
|
-
expect.assertions(2); // eslint-disable-line no-magic-numbers
|
|
191
|
-
const expectedHeaders = { headers: { 'User-Agent': userAgent } };
|
|
192
|
-
const testResults = await checkLinksWithMocks(externalLink);
|
|
193
|
-
expect(testResults.axiosHeadSpy.mock.calls[0][1]).toStrictEqual(expect.objectContaining(expectedHeaders));
|
|
194
|
-
expect(testResults.axiosGetSpy).not.toHaveBeenCalled();
|
|
195
|
-
});
|
|
196
|
-
|
|
197
|
-
it('should make HEAD and GET request for link if not same page and HEAD returns error', async() => {
|
|
198
|
-
expect.assertions(2); // eslint-disable-line no-magic-numbers
|
|
199
|
-
const testResults = await checkLinksWithMocks(externalLink, [], false, httpResponse.badRequest);
|
|
200
|
-
expect(testResults.axiosHeadSpy.mock.calls[0][0]).toStrictEqual(externalLink);
|
|
201
|
-
expect(testResults.axiosGetSpy.mock.calls[0][0]).toStrictEqual(externalLink);
|
|
202
|
-
});
|
|
203
|
-
|
|
204
|
-
it('should make HEAD and GET request passing the current page user agent if HEAD returns error', async() => {
|
|
205
|
-
expect.assertions(2); // eslint-disable-line no-magic-numbers
|
|
206
|
-
const expectedHeaders = { headers: { 'User-Agent': userAgent } };
|
|
207
|
-
const testResults = await checkLinksWithMocks(externalLink, [], false, httpResponse.badRequest);
|
|
208
|
-
expect(testResults.axiosHeadSpy.mock.calls[0][1]).toStrictEqual(expect.objectContaining(expectedHeaders));
|
|
209
|
-
expect(testResults.axiosGetSpy.mock.calls[0][1]).toStrictEqual(expect.objectContaining(expectedHeaders));
|
|
210
|
-
});
|
|
211
|
-
|
|
212
|
-
it('should return the HTTP response status for a nominal request', async() => {
|
|
213
|
-
expect.assertions(1);
|
|
214
|
-
const testResults = await checkLinksWithMocks(externalLink, [], false, httpResponse.ok);
|
|
215
|
-
expect(testResults.result).toStrictEqual(httpResponse.ok);
|
|
216
|
-
});
|
|
217
|
-
|
|
218
|
-
it('should return the HTTP response status for an off-nominal request with a response', async() => {
|
|
219
|
-
expect.assertions(1);
|
|
220
|
-
const link = 'https://www.google.com/this.is.not.found/';
|
|
221
|
-
const testResults = await checkLinksWithMocks(link, [], false, httpResponse.notFound);
|
|
222
|
-
expect(testResults.result).toStrictEqual(httpResponse.notFound);
|
|
223
|
-
});
|
|
224
|
-
|
|
225
|
-
it('should return the error code for a request with an execution error', async() => {
|
|
226
|
-
expect.assertions(1);
|
|
227
|
-
const link = 'https://this.is.a.test/';
|
|
228
|
-
const responseCode = 'ENOTFOUND';
|
|
229
|
-
const testResults = await checkLinksWithMocks(link, [], false, responseCode);
|
|
230
|
-
expect(testResults.result).toStrictEqual(responseCode);
|
|
231
|
-
});
|
|
232
|
-
});
|
|
233
|
-
});
|
|
234
|
-
|
|
235
|
-
describe('is failed response', () => {
|
|
236
|
-
const response = { status: undefined };
|
|
237
|
-
it('should return true for any numeric status value greater than or equal to 400', () => {
|
|
238
|
-
// eslint-disable-next-line no-magic-numbers
|
|
239
|
-
expect.assertions(2);
|
|
240
|
-
response.status = 400;
|
|
241
|
-
expect(isFailedResponse(response)).toStrictEqual(true);
|
|
242
|
-
response.status = 500;
|
|
243
|
-
expect(isFailedResponse(response)).toStrictEqual(true);
|
|
244
|
-
});
|
|
245
|
-
|
|
246
|
-
it('return false for any numeric status value less than 400', () => {
|
|
247
|
-
// eslint-disable-next-line no-magic-numbers
|
|
248
|
-
expect.assertions(2);
|
|
249
|
-
response.status = 302;
|
|
250
|
-
expect(isFailedResponse(response)).toStrictEqual(false);
|
|
251
|
-
response.status = 200;
|
|
252
|
-
expect(isFailedResponse(response)).toStrictEqual(false);
|
|
253
|
-
});
|
|
254
|
-
|
|
255
|
-
it('return true for any status value that is nor a number', () => {
|
|
256
|
-
// eslint-disable-next-line no-magic-numbers
|
|
257
|
-
expect.assertions(2);
|
|
258
|
-
response.status = 'ENOTFOUND';
|
|
259
|
-
expect(isFailedResponse(response)).toStrictEqual(true);
|
|
260
|
-
response.status = undefined;
|
|
261
|
-
expect(isFailedResponse(response)).toStrictEqual(true);
|
|
262
|
-
});
|
|
263
|
-
});
|
|
264
|
-
|
|
265
|
-
describe('normalize link', () => {
|
|
266
|
-
const referenceUrl = 'https://www.test.com';
|
|
267
|
-
|
|
268
|
-
it('should remove trailing slash without altering URL', () => {
|
|
269
|
-
expect.assertions(1);
|
|
270
|
-
const initialURL = `${referenceUrl}/`;
|
|
271
|
-
expect(normalizeLink(initialURL)).toStrictEqual(referenceUrl);
|
|
272
|
-
});
|
|
273
|
-
|
|
274
|
-
it('should not strip www from domain', () => {
|
|
275
|
-
expect.assertions(1);
|
|
276
|
-
expect(normalizeLink(referenceUrl)).toStrictEqual(referenceUrl);
|
|
277
|
-
});
|
|
278
|
-
|
|
279
|
-
it('should add https as protocol if protocol relative URL', () => {
|
|
280
|
-
expect.assertions(1);
|
|
281
|
-
const initialURL = referenceUrl.replace('https:', '');
|
|
282
|
-
expect(normalizeLink(initialURL)).toStrictEqual(referenceUrl);
|
|
283
|
-
});
|
|
284
|
-
|
|
285
|
-
it('should not remove hash from url', () => {
|
|
286
|
-
expect.assertions(1);
|
|
287
|
-
const initialURL = `${referenceUrl}/#elementId`;
|
|
288
|
-
expect(normalizeLink(initialURL)).toStrictEqual(initialURL);
|
|
289
|
-
});
|
|
290
|
-
});
|
package/tests/logger.test.js
DELETED
|
@@ -1,324 +0,0 @@
|
|
|
1
|
-
/* eslint-disable max-lines */
|
|
2
|
-
|
|
3
|
-
'use strict';
|
|
4
|
-
|
|
5
|
-
const testLogger = require('../lib/logger');
|
|
6
|
-
const { testResultStates } = require('../lib/testUtils');
|
|
7
|
-
const defaultConfig = require('../lib/default-config.json');
|
|
8
|
-
defaultConfig.project = 'Default project';
|
|
9
|
-
|
|
10
|
-
const configNoCli = {
|
|
11
|
-
reporters: [
|
|
12
|
-
'html',
|
|
13
|
-
'json'
|
|
14
|
-
]
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
const defaultUrl = 'https://somewhere.com';
|
|
18
|
-
const defaultTestName = 'should run a test';
|
|
19
|
-
|
|
20
|
-
// eslint-disable-next-line max-lines-per-function
|
|
21
|
-
describe('startUrlTests', () => {
|
|
22
|
-
let loggerWithCli, loggerNoCli;
|
|
23
|
-
|
|
24
|
-
beforeEach(() => {
|
|
25
|
-
loggerWithCli = testLogger(defaultConfig);
|
|
26
|
-
loggerNoCli = testLogger(configNoCli);
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
afterEach(() => {
|
|
30
|
-
jest.restoreAllMocks();
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
it('should add URL results object to test results', () => {
|
|
34
|
-
expect.assertions(3); // eslint-disable-line no-magic-numbers
|
|
35
|
-
// Console log is mocked to avoid writing output to the console during the test
|
|
36
|
-
jest.spyOn(console, 'log').mockImplementation(() => {});
|
|
37
|
-
|
|
38
|
-
const startingResults = loggerWithCli.getTestResults();
|
|
39
|
-
expect(startingResults.results).toHaveLength(0);
|
|
40
|
-
|
|
41
|
-
loggerWithCli.startUrlTests(defaultUrl);
|
|
42
|
-
const endingResults = loggerWithCli.getTestResults();
|
|
43
|
-
expect(endingResults.results).toHaveLength(1);
|
|
44
|
-
expect(endingResults.results[0].url).toStrictEqual(defaultUrl);
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
it('should log to console with URL being tested if CLI reporter enabled', () => {
|
|
48
|
-
expect.assertions(1);
|
|
49
|
-
const consoleLogSpy = jest.spyOn(console, 'log').mockImplementation(() => {});
|
|
50
|
-
loggerWithCli.startUrlTests(defaultUrl);
|
|
51
|
-
expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining(defaultUrl));
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
it('should not log to console with URL being tested if CLI reporter disabled', () => {
|
|
55
|
-
expect.assertions(1);
|
|
56
|
-
const consoleLogSpy = jest.spyOn(console, 'log').mockImplementation(() => {});
|
|
57
|
-
loggerNoCli.startUrlTests(defaultUrl);
|
|
58
|
-
expect(consoleLogSpy).not.toHaveBeenCalled();
|
|
59
|
-
});
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
// eslint-disable-next-line max-lines-per-function
|
|
63
|
-
describe('logTestResults', () => {
|
|
64
|
-
let loggerWithCli, loggerNoCli;
|
|
65
|
-
|
|
66
|
-
beforeEach(() => {
|
|
67
|
-
loggerWithCli = testLogger(defaultConfig);
|
|
68
|
-
loggerNoCli = testLogger(configNoCli);
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
afterEach(() => {
|
|
72
|
-
jest.restoreAllMocks();
|
|
73
|
-
delete require.cache[require.resolve('../lib/logger')];
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
// This includes only the console log from startUrlTests and getTestResults
|
|
77
|
-
const consoleLoggerSpyDefaultCalls = 2;
|
|
78
|
-
const testLogTestResults = (testResults, logger) => {
|
|
79
|
-
const consoleLogSpy = jest.spyOn(console, 'log').mockImplementation(() => {});
|
|
80
|
-
|
|
81
|
-
logger.startUrlTests(defaultUrl);
|
|
82
|
-
logger.logTestResults(testResults);
|
|
83
|
-
|
|
84
|
-
const allTestResults = logger.getTestResults();
|
|
85
|
-
|
|
86
|
-
return { allTestResults, consoleLogSpy };
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
const testResultSymbols = Object.freeze({
|
|
90
|
-
passed: '√',
|
|
91
|
-
failed: '×',
|
|
92
|
-
warning: '‼'
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
const testConsoleLogOutput = (testResult, logger) => {
|
|
96
|
-
const consoleSpy = jest.spyOn(console, 'log').mockImplementation(() => {});
|
|
97
|
-
|
|
98
|
-
logger.startUrlTests(defaultUrl);
|
|
99
|
-
|
|
100
|
-
const testResults = {
|
|
101
|
-
name: defaultTestName,
|
|
102
|
-
result: testResult,
|
|
103
|
-
data: []
|
|
104
|
-
};
|
|
105
|
-
logger.logTestResults(testResults);
|
|
106
|
-
return consoleSpy;
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
it('should not log test results if no results provided', () => {
|
|
110
|
-
expect.assertions(2); // eslint-disable-line no-magic-numbers
|
|
111
|
-
const { allTestResults, consoleLogSpy } = testLogTestResults(undefined, loggerWithCli);
|
|
112
|
-
expect(allTestResults.results[0].tests).toHaveLength(0);
|
|
113
|
-
expect(consoleLogSpy).toHaveBeenCalledTimes(consoleLoggerSpyDefaultCalls);
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
it('should log test results if provided', () => {
|
|
117
|
-
expect.assertions(1);
|
|
118
|
-
const testResults = {
|
|
119
|
-
name: defaultTestName,
|
|
120
|
-
result: testResultStates.passed,
|
|
121
|
-
data: []
|
|
122
|
-
};
|
|
123
|
-
const { allTestResults } = testLogTestResults(testResults, loggerWithCli);
|
|
124
|
-
expect(allTestResults.results[0].tests[0]).toStrictEqual(testResults);
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
it('should output test name to console if test results provided and CLI reporter enabled', () => {
|
|
128
|
-
expect.assertions(1);
|
|
129
|
-
const consoleSpy = testConsoleLogOutput(testResultStates.passed, loggerWithCli);
|
|
130
|
-
// eslint-disable-next-line no-magic-numbers
|
|
131
|
-
expect(consoleSpy).toHaveBeenNthCalledWith(2, expect.stringContaining(defaultTestName));
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
it('should not output test name to console if test results provided and CLI reporter disabled', () => {
|
|
135
|
-
expect.assertions(1);
|
|
136
|
-
const consoleSpy = testConsoleLogOutput(testResultStates.passed, loggerNoCli);
|
|
137
|
-
expect(consoleSpy).not.toHaveBeenCalled();
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
const testConsoleLogTestResults = (testResult) => {
|
|
141
|
-
expect.assertions(2); // eslint-disable-line no-magic-numbers
|
|
142
|
-
const consoleSpy = testConsoleLogOutput(testResult, loggerWithCli);
|
|
143
|
-
/* eslint-disable no-magic-numbers */
|
|
144
|
-
expect(consoleSpy).toHaveBeenNthCalledWith(2, expect.stringContaining(testResult));
|
|
145
|
-
expect(consoleSpy).toHaveBeenNthCalledWith(2, expect.stringContaining(testResultSymbols[testResult]));
|
|
146
|
-
/* eslint-enable no-magic-numbers */
|
|
147
|
-
};
|
|
148
|
-
|
|
149
|
-
const testNoConsoleLogTestResults = (testResult) => {
|
|
150
|
-
expect.assertions(1);
|
|
151
|
-
const consoleSpy = testConsoleLogOutput(testResult, loggerNoCli);
|
|
152
|
-
expect(consoleSpy).not.toHaveBeenCalled();
|
|
153
|
-
};
|
|
154
|
-
|
|
155
|
-
it('should output test results to console for a passed test if CLI reporter enabled', () => {
|
|
156
|
-
expect.hasAssertions();
|
|
157
|
-
testConsoleLogTestResults(testResultStates.passed);
|
|
158
|
-
});
|
|
159
|
-
|
|
160
|
-
it('should not output test results to console for a passed test if CLI reporter disabled', () => {
|
|
161
|
-
expect.hasAssertions();
|
|
162
|
-
testNoConsoleLogTestResults(testResultStates.passed);
|
|
163
|
-
});
|
|
164
|
-
|
|
165
|
-
it('should output test results to console for a warning test if CLI reporter enabled', () => {
|
|
166
|
-
expect.hasAssertions();
|
|
167
|
-
testConsoleLogTestResults(testResultStates.warning);
|
|
168
|
-
});
|
|
169
|
-
|
|
170
|
-
it('should not output test results to console for a warning test if CLI reporter disabled', () => {
|
|
171
|
-
expect.hasAssertions();
|
|
172
|
-
testNoConsoleLogTestResults(testResultStates.warning);
|
|
173
|
-
});
|
|
174
|
-
|
|
175
|
-
it('should output test results to console for a failed test if CLI reporter enabled', () => {
|
|
176
|
-
expect.hasAssertions();
|
|
177
|
-
testConsoleLogTestResults(testResultStates.failed);
|
|
178
|
-
});
|
|
179
|
-
|
|
180
|
-
it('should not output test results to console for a failed test if CLI reporter disabled', () => {
|
|
181
|
-
expect.hasAssertions();
|
|
182
|
-
testNoConsoleLogTestResults(testResultStates.failed);
|
|
183
|
-
});
|
|
184
|
-
|
|
185
|
-
it('should increment only test and passed counters for a passed test', () => {
|
|
186
|
-
expect.assertions(4); // eslint-disable-line no-magic-numbers
|
|
187
|
-
const testResults = {
|
|
188
|
-
name: defaultTestName,
|
|
189
|
-
result: testResultStates.passed
|
|
190
|
-
};
|
|
191
|
-
const { allTestResults } = testLogTestResults(testResults, loggerWithCli);
|
|
192
|
-
expect(allTestResults.summary.tests).toStrictEqual(1);
|
|
193
|
-
expect(allTestResults.summary.passed).toStrictEqual(1);
|
|
194
|
-
expect(allTestResults.summary.warning).toStrictEqual(0);
|
|
195
|
-
expect(allTestResults.summary.failed).toStrictEqual(0);
|
|
196
|
-
});
|
|
197
|
-
|
|
198
|
-
it('should increment only test and failed counters for a failed test', () => {
|
|
199
|
-
expect.assertions(4); // eslint-disable-line no-magic-numbers
|
|
200
|
-
const testResults = {
|
|
201
|
-
name: defaultTestName,
|
|
202
|
-
result: testResultStates.failed
|
|
203
|
-
};
|
|
204
|
-
const { allTestResults } = testLogTestResults(testResults, loggerWithCli);
|
|
205
|
-
expect(allTestResults.summary.tests).toStrictEqual(1);
|
|
206
|
-
expect(allTestResults.summary.passed).toStrictEqual(0);
|
|
207
|
-
expect(allTestResults.summary.warning).toStrictEqual(0);
|
|
208
|
-
expect(allTestResults.summary.failed).toStrictEqual(1);
|
|
209
|
-
});
|
|
210
|
-
|
|
211
|
-
it('should increment only test and warning counters for a warning test', () => {
|
|
212
|
-
expect.assertions(4); // eslint-disable-line no-magic-numbers
|
|
213
|
-
const testResults = {
|
|
214
|
-
name: defaultTestName,
|
|
215
|
-
result: testResultStates.warning
|
|
216
|
-
};
|
|
217
|
-
const { allTestResults } = testLogTestResults(testResults, loggerWithCli);
|
|
218
|
-
expect(allTestResults.summary.tests).toStrictEqual(1);
|
|
219
|
-
expect(allTestResults.summary.passed).toStrictEqual(0);
|
|
220
|
-
expect(allTestResults.summary.warning).toStrictEqual(1);
|
|
221
|
-
expect(allTestResults.summary.failed).toStrictEqual(0);
|
|
222
|
-
});
|
|
223
|
-
});
|
|
224
|
-
|
|
225
|
-
// eslint-disable-next-line max-lines-per-function
|
|
226
|
-
describe('getTestResults', () => {
|
|
227
|
-
let loggerWithCli, loggerNoCli;
|
|
228
|
-
|
|
229
|
-
beforeEach(() => {
|
|
230
|
-
loggerWithCli = testLogger(defaultConfig);
|
|
231
|
-
loggerNoCli = testLogger(configNoCli);
|
|
232
|
-
});
|
|
233
|
-
|
|
234
|
-
afterEach(() => {
|
|
235
|
-
jest.restoreAllMocks();
|
|
236
|
-
});
|
|
237
|
-
|
|
238
|
-
const testGetTestResults = (includeTests, testName, testResult, logger) => {
|
|
239
|
-
const consoleLogSpy = jest.spyOn(console, 'log').mockImplementation(() => {});
|
|
240
|
-
|
|
241
|
-
if (includeTests) {
|
|
242
|
-
logger.startUrlTests(defaultUrl);
|
|
243
|
-
const name = testName;
|
|
244
|
-
const result = testResult;
|
|
245
|
-
logger.logTestResults({ name, result });
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
const results = logger.getTestResults();
|
|
249
|
-
return {
|
|
250
|
-
results,
|
|
251
|
-
consoleLogSpy
|
|
252
|
-
};
|
|
253
|
-
};
|
|
254
|
-
|
|
255
|
-
it('should return a results object with appropriate properties with no tests run', () => {
|
|
256
|
-
expect.assertions(2); // eslint-disable-line no-magic-numbers
|
|
257
|
-
|
|
258
|
-
const { results } = testGetTestResults(false, undefined, undefined, loggerWithCli);
|
|
259
|
-
expect(results).toStrictEqual(expect.objectContaining({
|
|
260
|
-
project: defaultConfig.project,
|
|
261
|
-
executionStart: expect.any(Date),
|
|
262
|
-
summary: {
|
|
263
|
-
tests: 0,
|
|
264
|
-
passed: 0,
|
|
265
|
-
warning: 0,
|
|
266
|
-
failed: 0
|
|
267
|
-
},
|
|
268
|
-
results: expect.any(Array)
|
|
269
|
-
}));
|
|
270
|
-
expect(results.results).toHaveLength(0);
|
|
271
|
-
});
|
|
272
|
-
|
|
273
|
-
it('should return a results object with appropriate properties with tests run', () => {
|
|
274
|
-
expect.assertions(3); // eslint-disable-line no-magic-numbers
|
|
275
|
-
|
|
276
|
-
const testName = defaultTestName;
|
|
277
|
-
const testResult = testResultStates.passed;
|
|
278
|
-
const { results } = testGetTestResults(true, testName, testResult, loggerWithCli);
|
|
279
|
-
|
|
280
|
-
expect(results).toStrictEqual(expect.objectContaining({
|
|
281
|
-
project: defaultConfig.project,
|
|
282
|
-
executionStart: expect.any(Date),
|
|
283
|
-
summary: {
|
|
284
|
-
tests: 1,
|
|
285
|
-
passed: 1,
|
|
286
|
-
warning: 0,
|
|
287
|
-
failed: 0
|
|
288
|
-
},
|
|
289
|
-
results: expect.any(Array)
|
|
290
|
-
}));
|
|
291
|
-
expect(results.results).toHaveLength(1);
|
|
292
|
-
expect(results.results[0]).toStrictEqual(expect.objectContaining({
|
|
293
|
-
url: defaultUrl,
|
|
294
|
-
tests: [{ name: testName, result: testResult }] }));
|
|
295
|
-
});
|
|
296
|
-
|
|
297
|
-
const testTestResultsSummary = (logger, messageCount) => {
|
|
298
|
-
expect.assertions(4); // eslint-disable-line no-magic-numbers
|
|
299
|
-
|
|
300
|
-
const testResult = testResultStates.warning;
|
|
301
|
-
const { consoleLogSpy } = testGetTestResults(true, defaultTestName, testResult, logger);
|
|
302
|
-
|
|
303
|
-
// eslint-disable-next-line prefer-destructuring
|
|
304
|
-
const message = consoleLogSpy.mock.calls[messageCount - 1][0];
|
|
305
|
-
expect(message).toMatch(/Tests: 1/);
|
|
306
|
-
expect(message).toMatch(/Passed: 0/);
|
|
307
|
-
expect(message).toMatch(/Warning: 1/);
|
|
308
|
-
expect(message).toMatch(/Failed: 0/);
|
|
309
|
-
};
|
|
310
|
-
|
|
311
|
-
it('should log to console the test result summary with total, passed, failed, and warning counts if CLI reporter enabled', () => {
|
|
312
|
-
expect.hasAssertions();
|
|
313
|
-
// Expect test name, test result, and the final results messages
|
|
314
|
-
const messageCount = 3;
|
|
315
|
-
testTestResultsSummary(loggerWithCli, messageCount);
|
|
316
|
-
});
|
|
317
|
-
|
|
318
|
-
it('should log to console the test result summary with total, passed, failed, and warning counts if CLI reporter disabled', () => {
|
|
319
|
-
expect.hasAssertions();
|
|
320
|
-
// Expect only the final results message
|
|
321
|
-
const messageCount = 1;
|
|
322
|
-
testTestResultsSummary(loggerNoCli, messageCount);
|
|
323
|
-
});
|
|
324
|
-
});
|