/
chrome-launcher-test.ts
125 lines (98 loc) · 4.18 KB
/
chrome-launcher-test.ts
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
/**
* @license Copyright 2016 Google Inc. 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';
import {Launcher} from '../chrome-launcher';
import {spy, stub} from 'sinon';
import * as assert from 'assert';
const log = require('lighthouse-logger');
const fsMock = {
openSync: () => {},
closeSync: () => {},
writeFileSync: () => {}
};
describe('Launcher', () => {
beforeEach(() => {
log.setLevel('error');
});
afterEach(() => {
log.setLevel('');
});
it('sets default launching flags', async () => {
const spawnStub = stub().returns({pid: 'some_pid'});
const chromeInstance = new Launcher(
{userDataDir: 'some_path'},
{fs: fsMock as any, rimraf: spy() as any, spawn: spawnStub as any});
stub(chromeInstance, 'waitUntilReady').returns(Promise.resolve());
chromeInstance.prepare();
try {
await chromeInstance.launch();
} catch (err) {
return Promise.reject(err);
}
const chromeFlags = spawnStub.getCall(0).args[1] as string[];
assert.ok(chromeFlags.find(f => f.startsWith('--remote-debugging-port')))
assert.ok(chromeFlags.find(f => f.startsWith('--disable-background-networking')))
assert.strictEqual(chromeFlags[chromeFlags.length - 1], 'about:blank');
});
it('accepts and uses a custom path', async () => {
const rimrafMock = spy();
const chromeInstance =
new Launcher({userDataDir: 'some_path'}, {fs: fsMock as any, rimraf: rimrafMock as any});
chromeInstance.prepare();
await chromeInstance.destroyTmp();
assert.strictEqual(rimrafMock.callCount, 0);
});
it('cleans up the tmp dir after closing', async () => {
const rimrafMock = stub().callsFake((_, done) => done());
const chromeInstance = new Launcher({}, {fs: fsMock as any, rimraf: rimrafMock as any});
chromeInstance.prepare();
await chromeInstance.destroyTmp();
assert.strictEqual(rimrafMock.callCount, 1);
});
it('does not delete created directory when custom path passed', () => {
const chromeInstance = new Launcher({userDataDir: 'some_path'}, {fs: fsMock as any});
chromeInstance.prepare();
assert.equal(chromeInstance.userDataDir, 'some_path');
});
it('defaults to genering a tmp dir when no data dir is passed', () => {
const chromeInstance = new Launcher({}, {fs: fsMock as any});
const originalMakeTmp = chromeInstance.makeTmpDir;
chromeInstance.makeTmpDir = () => 'tmp_dir'
chromeInstance.prepare()
assert.equal(chromeInstance.userDataDir, 'tmp_dir');
// Restore the original fn.
chromeInstance.makeTmpDir = originalMakeTmp;
});
it('doesn\'t fail when killed twice', async () => {
const chromeInstance = new Launcher();
await chromeInstance.launch();
await chromeInstance.kill();
await chromeInstance.kill();
});
it('doesn\'t launch multiple chrome processes', async () => {
const chromeInstance = new Launcher();
await chromeInstance.launch();
let pid = chromeInstance.pid!;
await chromeInstance.launch();
assert.strictEqual(pid, chromeInstance.pid);
await chromeInstance.kill();
});
it('removes --disable-extensions from flags on enableExtensions', async () => {
const spawnStub = stub().returns({pid: 'some_pid'});
const chromeInstance = new Launcher(
{enableExtensions: true},
{fs: fsMock as any, rimraf: spy() as any, spawn: spawnStub as any});
stub(chromeInstance, 'waitUntilReady').returns(Promise.resolve());
chromeInstance.prepare();
try {
await chromeInstance.launch();
} catch (err) {
return Promise.reject(err);
}
const chromeFlags = spawnStub.getCall(0).args[1] as string[];
assert.ok(!chromeFlags.includes('--disable-extensions'));
});
});