Skip to content

Commit 4a7eed4

Browse files
authored
Update chrome-launcher. (#297)
1 parent 7c39455 commit 4a7eed4

File tree

4 files changed

+114
-75
lines changed

4 files changed

+114
-75
lines changed

package-lock.json

Lines changed: 76 additions & 48 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"node": ">= 6"
3636
},
3737
"dependencies": {
38-
"chrome-launcher": "0.13.2",
38+
"chrome-launcher": "^0.13.4",
3939
"chrome-remote-interface": "^0.30.0",
4040
"devtools-protocol": "0.0.878340"
4141
},
@@ -45,8 +45,10 @@
4545
"@types/mocha": "^8.0.2",
4646
"@types/mock-fs": "^4.10.0",
4747
"@types/node": "^15.0.2",
48+
"@types/proxyquire": "^1.3.28",
4849
"@types/sinon": "^10.0.0",
4950
"@types/sinon-chai": "^3.2.0",
51+
"@types/source-map-support": "^0.5.3",
5052
"chai": "^4.1.0",
5153
"chai-string": "^1.3.0",
5254
"codecov": "^3.0.0",
@@ -60,9 +62,11 @@
6062
"mocha": "^8.1.1",
6163
"mock-fs": "^4.5.0",
6264
"pdf2json": "^1.1.7",
65+
"proxyquire": "^2.1.3",
6366
"remap-istanbul": "^0.13.0",
6467
"sinon": "^10.0.0",
6568
"sinon-chai": "^3.0.0",
69+
"source-map-support": "^0.5.19",
6670
"tslint": "^6.1.3",
6771
"tslint-microsoft-contrib": "^6.0.0",
6872
"typescript": "^4.1.3"

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export async function create(html: string, options?: CreateOptions): Promise<Cre
6666
async function generate(html: string, options: CreateOptions, tab: any): Promise<CreateResult> {
6767
await throwIfCanceledOrFailed(options);
6868
const client = await CDP({ ...options, target: tab });
69-
const connectionLost = new Promise<CreateResult>((_, reject) => {
69+
const connectionLost = new Promise<never>((_, reject) => {
7070
client.on('disconnect', () => {
7171
options._connectionLost = true;
7272
reject(new Error('HtmlPdf.create() connection lost.'));

test/index.ts

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@
22

33
// tslint:disable:no-unused-expression
44

5+
import 'source-map-support/register';
6+
57
import * as chai from 'chai';
68
import * as chromeLauncher from 'chrome-launcher';
79
import * as Chrome from 'chrome-remote-interface/lib/chrome';
810
import { Protocol } from 'devtools-protocol';
911
import * as fs from 'fs';
10-
import getPort = require('get-port');
12+
import * as getPort from 'get-port';
1113
import * as mockFs from 'mock-fs';
1214
import * as path from 'path';
1315
import * as PDFParser from 'pdf2json';
16+
import * as proxyquire from 'proxyquire';
1417
import * as sinon from 'sinon';
1518
import { Readable } from 'stream';
1619

@@ -87,41 +90,45 @@ describe('HtmlPdf', () => {
8790

8891
it('should handle a Chrome launch failure', async () => {
8992
const error = new Error('failed!');
90-
const launchStub = sinon.stub(chromeLauncher, 'launch').callsFake(() => Promise.reject(error));
9193
try {
92-
await HtmlPdf.create('<p>hello!</p>');
94+
const mockedHtmlPdf = proxyquire('../src', {
95+
'chrome-launcher': {
96+
launch: sinon.stub().rejects(error)
97+
}
98+
});
99+
await mockedHtmlPdf.create('<p>hello!</p>');
93100
expect.fail();
94101
} catch (err) {
95102
expect(err).to.equal(error);
96-
} finally {
97-
launchStub.restore();
98103
}
99104
});
100105

101106
it('should use running Chrome to generate a PDF (specify port)', async () => {
102-
const launchStub = sinon.stub(chromeLauncher, 'launch');
103-
try {
104-
const result = await HtmlPdf.create('<p>hello!</p>', {port});
105-
expect(result).to.be.an.instanceOf(HtmlPdf.CreateResult);
106-
expect(launchStub).to.not.have.been.called;
107-
const pdf = await getParsedPdf(result.toBuffer());
108-
expect(pdf.getRawTextContent()).to.startWith('hello!');
109-
} finally {
110-
launchStub.restore();
111-
}
107+
const launchStub = sinon.stub();
108+
const mockedHtmlPdf = proxyquire('../src', {
109+
'chrome-launcher': {
110+
launch: launchStub,
111+
}
112+
});
113+
const result = await mockedHtmlPdf.create('<p>hello!</p>', {port});
114+
expect(result).to.be.an.instanceOf(HtmlPdf.CreateResult);
115+
expect(launchStub).to.not.have.been.called;
116+
const pdf = await getParsedPdf(result.toBuffer());
117+
expect(pdf.getRawTextContent()).to.startWith('hello!');
112118
});
113119

114120
it('should use running Chrome to generate a PDF (specify host and port)', async () => {
115-
const launchStub = sinon.stub(chromeLauncher, 'launch');
116-
try {
117-
const result = await HtmlPdf.create('<p>hello!</p>', {host: 'localhost', port});
118-
expect(result).to.be.an.instanceOf(HtmlPdf.CreateResult);
119-
expect(launchStub).to.not.have.been.called;
120-
const pdf = await getParsedPdf(result.toBuffer());
121-
expect(pdf.getRawTextContent()).to.startWith('hello!');
122-
} finally {
123-
launchStub.restore();
124-
}
121+
const launchStub = sinon.stub();
122+
const mockedHtmlPdf = proxyquire('../src', {
123+
'chrome-launcher': {
124+
launch: launchStub,
125+
}
126+
});
127+
const result = await mockedHtmlPdf.create('<p>hello!</p>', {host: 'localhost', port});
128+
expect(result).to.be.an.instanceOf(HtmlPdf.CreateResult);
129+
expect(launchStub).to.not.have.been.called;
130+
const pdf = await getParsedPdf(result.toBuffer());
131+
expect(pdf.getRawTextContent()).to.startWith('hello!');
125132
});
126133

127134
it('should generate a PDF with Chrome options', async () => {

0 commit comments

Comments
 (0)