Skip to content

React Testing Library failing test with code 'Font.register...'Β #2137

@mdodge-ecgrow

Description

@mdodge-ecgrow

I have several components that are creating PDF documents with this library with custom imported fonts. I am registering the fonts like this:

Font.register({
	family: 'PTSerif',
	format: 'truetype',
	src: PTSerif,
});

Font.register({
	family: 'Raleway',
	format: 'truetype',
	src: Raleway,
});

Well, I am starting to write unit tests for all my code using React Testing Library. And on these components I am getting a failing test with the following error: TypeError: Cannot read properties of undefined (reading 'register') and it highlights the Font.register line. Any ideas how I can get around this?

I did add this import to my test file and did not see any changes in the test results:

import {
	Page,
	Text,
	View,
	Document,
	StyleSheet,
	Canvas,
	Font,
	Image,
} from '@react-pdf/renderer';

Update
So I just tried adding the 'Font' function to this snippet in my test file:

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

And my error changed to: TypeError: _renderer.Font.register is not a function. I tried to change the mocked function to 'Font.register' but my IDE doesn't seem to like that syntax.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions