Amit
Amit

Reputation: 51

ReferenceError: html2pdf is not defined (JSPDF)

I am getting below error when I am writing unit test with shallow rendering

ReferenceError: html2pdf is not defined at /PathToProject/node_modules/jspdf/dist/jspdf.min.js:202:16291

    When I comment out my unit test code whole jspdf code works fine.
    I am surprised why it is failing only when I execute unit test like below -

    import { expect } from "chai";
    import { shallow } from "enzyme";
    import "mocha";
    import * as React from "react";

    const jsdom = require("jsdom-global");
    jsdom();
    import App from "../src/app";

    describe("<App/>", () => {
      it("should do a shallow render of <Provider/>", () => {

        const elem = shallow(<App/>); //this line is creating issue
        expect(elem.find("Provider")).to.have.length(1);
      });
    });

Note: Before executing unit test everything works perfectly fine. All the jspdf functionality works well. but as soon as i am executing unit test by writing shallow it is throwing this error.

Upvotes: 0

Views: 7155

Answers (1)

shannon
shannon

Reputation: 377

There's currently an open issue in the repo https://github.com/MrRio/jsPDF/issues/2462. The workaround mentioned in the second reply might be worth a try. If not, this issue appears to have been introduced with v1.5.1 and the only way I've found to make jspdf play well with enzyme is to move back to v1.4.1. On 1.4.1, I get a different error* when running tests but it doesn't cause the tests to fail.

*The error is Not implemented: HTMLCanvasElement.prototype.getContext (without installing the canvas npm package) - and it is not solved by installing the canvas npm package

Upvotes: 1

Related Questions