Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Importing in jest errors on missing TextEncoder #1057

Open
nbrustein opened this issue Dec 19, 2024 · 1 comment
Open

Importing in jest errors on missing TextEncoder #1057

nbrustein opened this issue Dec 19, 2024 · 1 comment

Comments

@nbrustein
Copy link

Select which package(s) are affected

@livekit/components-core

Describe the bug

What is happening

I have a jest spec that imports a LiveKit component that has this code:

import {
    type AgentState,
    LiveKitRoom,
    useVoiceAssistant,
    BarVisualizer,
    RoomAudioRenderer,
    VoiceAssistantControlBar,
    DisconnectButton,
} from '@livekit/components-react';

I'm not actually using that LiveKit component in this spec at all. It just happens to be getting imported.

The test run fails on ReferenceError: TextEncoder is not defined

I can fix this by adding this to my setup:

global.TextEncoder = require('util').TextEncoder;
global.TextDecoder = require('util').TextDecoder;

What I expect

Just importing some livekit components should not break my test setup.

How to resolve

In places like this where you're using TextEncoder or TextDecoder, do not instantiate the encoder at the top level of the file. Wait until the function is called that needs the encoder, and initialize it then.

Reproduction

Add the following import to a jest spec and run it:

import {
    type AgentState,
    LiveKitRoom,
    useVoiceAssistant,
    BarVisualizer,
    RoomAudioRenderer,
    VoiceAssistantControlBar,
    DisconnectButton,
} from '@livekit/components-react';

Logs

No response

System Info

System:
    OS: macOS 14.6.1
    CPU: (10) arm64 Apple M1 Max
    Memory: 11.13 GB / 64.00 GB
    Shell: 3.2.57 - /bin/bash
  Binaries:
    Node: 22.11.0 - ~/.asdf/installs/nodejs/22.11.0/bin/node
    Yarn: 4.5.3 - ~/.asdf/installs/nodejs/22.11.0/bin/yarn
    npm: 10.9.0 - ~/.asdf/plugins/nodejs/shims/npm
  Browsers:
    Chrome: 131.0.6778.205
    Safari: 18.1

Severity

annoyance

Additional Information

No response

@lukasIO
Copy link
Contributor

lukasIO commented Jan 6, 2025

I get your point, but I think this is an issue of the jest environment not defining TextEncoder which is a globally available constructor on all supported browers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants