diff --git a/packages/fonts/src/mountFont.ts b/packages/fonts/src/mountFont.ts index 21bbd871..286dad2d 100644 --- a/packages/fonts/src/mountFont.ts +++ b/packages/fonts/src/mountFont.ts @@ -65,15 +65,14 @@ function getFontCss(font: MountFontParams) { * ``` */ export function mountFont(font: MountFontParams) { - const css = getFontCss(font); - - const newFontStyle = ` - - `; - if (globalThis && globalThis.document) { + const css = getFontCss(font); + + const newFontStyle = ` + + `; unmountFont(font.name); document.head.innerHTML += newFontStyle; } diff --git a/packages/fonts/tests/mountFont.test.ts b/packages/fonts/tests/mountFont.test.ts index 4c47b1d1..56fa0d87 100644 --- a/packages/fonts/tests/mountFont.test.ts +++ b/packages/fonts/tests/mountFont.test.ts @@ -63,4 +63,13 @@ describe('mountFont', () => { expect(boldStyle?.textContent).toContain('@import'); }); }); + + describe('when document is not defined', () => { + test('should not fail', () => { + Object.defineProperty(global, 'globalThis', { get: () => {} }); + mountFont({ name: 'should-not-be-added' } as any); + const element = document.getElementById('font-should-not-be-added'); + expect(element).toBeNull(); + }); + }); });