|
2 | 2 |
|
3 | 3 | // tslint:disable:no-unused-expression
|
4 | 4 |
|
| 5 | +import 'source-map-support/register'; |
| 6 | + |
5 | 7 | import * as chai from 'chai';
|
6 | 8 | import * as chromeLauncher from 'chrome-launcher';
|
7 | 9 | import * as Chrome from 'chrome-remote-interface/lib/chrome';
|
8 | 10 | import { Protocol } from 'devtools-protocol';
|
9 | 11 | import * as fs from 'fs';
|
10 |
| -import getPort = require('get-port'); |
| 12 | +import * as getPort from 'get-port'; |
11 | 13 | import * as mockFs from 'mock-fs';
|
12 | 14 | import * as path from 'path';
|
13 | 15 | import * as PDFParser from 'pdf2json';
|
| 16 | +import * as proxyquire from 'proxyquire'; |
14 | 17 | import * as sinon from 'sinon';
|
15 | 18 | import { Readable } from 'stream';
|
16 | 19 |
|
@@ -87,41 +90,45 @@ describe('HtmlPdf', () => {
|
87 | 90 |
|
88 | 91 | it('should handle a Chrome launch failure', async () => {
|
89 | 92 | const error = new Error('failed!');
|
90 |
| - const launchStub = sinon.stub(chromeLauncher, 'launch').callsFake(() => Promise.reject(error)); |
91 | 93 | 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>'); |
93 | 100 | expect.fail();
|
94 | 101 | } catch (err) {
|
95 | 102 | expect(err).to.equal(error);
|
96 |
| - } finally { |
97 |
| - launchStub.restore(); |
98 | 103 | }
|
99 | 104 | });
|
100 | 105 |
|
101 | 106 | 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!'); |
112 | 118 | });
|
113 | 119 |
|
114 | 120 | 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!'); |
125 | 132 | });
|
126 | 133 |
|
127 | 134 | it('should generate a PDF with Chrome options', async () => {
|
|
0 commit comments