question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Failing test using jest on import { PDFDownloadLink }

See original GitHub issue

Describe the bug I am using PDFDownloadLink to create a PDF and download it when I click on a button. Everything is working fine, except when I run my tests using jest. I haven’t implemented any test on the PDF component itself, jest is blocked on the following import:

ReferenceError: _a is not defined
import { PDFDownloadLink } from '@react-pdf/renderer'

Thank you in advance for your time!

Expected behavior Pass the tests while importing PDFDownloadLink.

Screenshots Screenshot 2019-09-10 at 10 40 03

Desktop:

  • OS: MacOS
  • Browser: chrome
  • React-pdf version: 1.6.4

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:6
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

15reactions
Sustenancecommented, May 15, 2020

So this doesn’t necessarily fix this issue, but if your Jest tests are failing after adding the PDFDownloadLink to your component, you can mock it out to keep everything working. If you strictly want your tests to keep working, just do something like this:

jest.mock('@react-pdf/renderer', () => ({
  PDFDownloadLink: jest.fn(() => null),
}));

and if you care about testing the children that are being passed into the PDFDownloadLink, you can still render them with something like this:

jest.mock('@react-pdf/renderer', () => ({
  PDFDownloadLink: function PDFDownloadLink({ children }) {
    return <>{children({})}</>;
  },
}));

Just mock everything until it passes 😅

7reactions
aaron-origincommented, Oct 10, 2019

I also have a similar issue with testing this using Jest/React-Testing-Library. Below is the Error I receive.

console.error node_modules/jsdom/lib/jsdom/virtual-console.js:29
    Error: Uncaught [Error: PDFDownloadLink is a web specific API. Or you're either using this component on Node, or your bundler is not loading react-pdf from the appropiate web build.]
Read more comments on GitHub >

github_iconTop Results From Across the Web

react-pdf - npm
Import by adding import { Document } from 'react-pdf' . ... It is tested with the latest versions of Chrome, Edge, Safari, Firefox,...
Read more >
Unable to import module in my Jest test file - Stack Overflow
I am importing one such service in my unit test. I am using jest for unit testing. Without import, test is working fine....
Read more >
Testing React Apps - Jest
At Facebook, we use Jest to test React applications. ... When a snapshot test fails, you need to inspect whether it is an...
Read more >
Setup - Testing Library
In these docs we'll demonstrate configuring Jest, but you should be able to ... import { render, fireEvent } from '@testing-library/react';
Read more >
Testing Recipes - React
This page assumes you're using Jest as a test runner. ... hello.js import React from "react"; export default function Hello(props) { if (props.name) ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found