-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
lightrider-entry-test.js
153 lines (128 loc) · 5.38 KB
/
lightrider-entry-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
/**
* @license Copyright 2018 The Lighthouse Authors. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
'use strict';
const assert = require('assert').strict;
const lhBackground = require('../lightrider/lightrider-entry.js');
const Runner = require('../../lighthouse-core/runner.js');
const LHError = require('../../lighthouse-core/lib/lh-error.js');
/* eslint-env jest */
describe('lightrider-entry', () => {
describe('#runLighthouseInLR', () => {
it('returns a runtimeError LHR when lighthouse throws a runtimeError', async () => {
const connectionError = new LHError(
LHError.errors.FAILED_DOCUMENT_REQUEST,
{errorDetails: 'Bad connection req'}
);
assert.strictEqual(connectionError.lhrRuntimeError, true);
const mockConnection = {
async connect() {
throw connectionError;
},
async disconnect() {},
async sendCommand() {},
on() {},
};
const url = 'https://example.com';
const output = 'json';
const result = await lhBackground.runLighthouseInLR(mockConnection, url, {output}, {});
const parsedResult = JSON.parse(result);
assert.strictEqual(parsedResult.runtimeError.code, connectionError.code);
assert.ok(parsedResult.runtimeError.message.includes(connectionError.friendlyMessage));
});
it('returns an unknown-runtimeError LHR when lighthouse throws an unknown error', async () => {
const errorMsg = 'Errors are the best!';
const connectionError = new Error(errorMsg);
assert.strictEqual(connectionError.lhrRuntimeError, undefined);
const mockConnection = {
async connect() {
throw connectionError;
},
async disconnect() {},
async sendCommand() {},
on() {},
};
const url = 'https://example.com';
const output = 'json';
const result = await lhBackground.runLighthouseInLR(mockConnection, url, {output}, {});
const parsedResult = JSON.parse(result);
assert.strictEqual(parsedResult.runtimeError.code, LHError.UNKNOWN_ERROR);
assert.ok(parsedResult.runtimeError.message.includes(errorMsg));
});
it('specifies the channel as lr', async () => {
const runStub = jest.spyOn(Runner, 'run');
const mockConnection = {};
const url = 'https://example.com';
await lhBackground.runLighthouseInLR(mockConnection, url, {}, {});
const config = runStub.mock.calls[0][1].config;
assert.equal(config.settings.channel, 'lr');
runStub.mockRestore();
});
it('uses the desktop config preset when device is desktop', async () => {
const runStub = jest.spyOn(Runner, 'run');
const mockConnection = {};
const url = 'https://example.com';
const lrDevice = 'desktop';
await lhBackground.runLighthouseInLR(mockConnection, url, {}, {lrDevice});
const config = runStub.mock.calls[0][1].config;
assert.equal(config.settings.formFactor, 'desktop');
runStub.mockRestore();
});
it('uses the mobile config preset when device is mobile', async () => {
const runStub = jest.spyOn(Runner, 'run');
const mockConnection = {};
const url = 'https://example.com';
const lrDevice = 'mobile';
await lhBackground.runLighthouseInLR(mockConnection, url, {}, {lrDevice});
const config = runStub.mock.calls[0][1].config;
assert.equal(config.settings.formFactor, 'mobile');
runStub.mockRestore();
});
it('overrides the default config when one is provided', async () => {
const runStub = jest.spyOn(Runner, 'run');
const mockConnection = {};
const url = 'https://example.com';
const configOverride = {
default: 'lighthouse:default',
settings: {
onlyAudits: ['network-requests'],
},
};
await lhBackground.runLighthouseInLR(mockConnection, url, {}, {configOverride});
const config = runStub.mock.calls[0][1].config;
assert.equal(config.settings.onlyAudits.length, 1);
assert.equal(config.settings.onlyAudits[0], 'network-requests');
runStub.mockRestore();
});
let originalRun;
beforeEach(() => {
originalRun = Runner.run;
});
afterEach(() => {
Runner.run = originalRun;
});
it('exposes artifacts when logAssets is true', async () => {
Runner.run = jest.fn(Runner.run).mockReturnValue(Promise.resolve({
lhr: {},
artifacts: {
Artifact: new Error('some error'),
},
}));
const mockConnection = {};
const url = 'https://example.com';
const lrFlags = {
logAssets: true,
};
const resultJson = await lhBackground.runLighthouseInLR(mockConnection, url, {}, lrFlags);
const result = JSON.parse(resultJson);
expect(result.artifacts).toMatchObject({
Artifact: {
sentinel: '__ErrorSentinel',
message: 'some error',
},
});
});
});
});