Discord.js: TypeError: TextDecoder is not a constructor when running tests

Created on 20 Mar 2020  ·  9Comments  ·  Source: discordjs/discord.js

Please describe the problem you are having in as much detail as possible:

After upgrading from v11.6.2 to v12.0.2, while the bot runs as expected my jest test suite fails with the following error:

 FAIL  src/bots/discord.spec.ts
  ● Test suite failed to run

    TypeError: TextDecoder is not a constructor



      at Object.<anonymous> (node_modules/discord.js/src/WebSocket.js:22:12)
      at Object.<anonymous> (node_modules/discord.js/src/client/voice/networking/VoiceWebSocket.js:4:19)
      at Object.<anonymous> (node_modules/discord.js/src/client/voice/VoiceConnection.js:5:24)
      at Object.<anonymous> (node_modules/discord.js/src/client/voice/ClientVoiceManager.js:4:25)
      at Object.<anonymous> (node_modules/discord.js/src/client/Client.js:5:28)
      at Object.<anonymous> (node_modules/discord.js/src/index.js:8:11)
      at Object.<anonymous> (src/bots/discord.ts:6391:41)
      at Object.<anonymous> (src/bots/discord.spec.ts:1:1)

I have a DiscordBot class with some static methods and those are the only ones being tested but it looks like it never gets to that and just fails when importing discord.js.

Looking through the stacktrace I noticed that in WebSocket.js there's this line:

  TextDecoder = require('util').TextDecoder;

and I don't see the TextDecoder anywhere, but I could be missing something.

Further details:

  • Jest version: 25.1.0
  • discord.js version: 12.0.2
  • Node.js version: 12.15.0
  • Operating system: Windows and Linux Debian
  • Priority this issue should have – please be realistic and elaborate if possible: No idea
unverified bug

Most helpful comment

I'll keep the snarky remarks to myself:

The standard testEnvironment for jest tests is the browser/DOM, you need to change it via the CLI or config option to node

https://jestjs.io/docs/en/configuration#testenvironmentoptions-object

jest.config.js

module.exports = {
    testEnvironment: 'node'
}

All 9 comments

Are you sure that's the actual node version you're running? util.TextDecoder was added a reaaally long time ago (v8.3.0)

Yeah I'm quite sure
image

There's also a minimum engine dependency of ^12 in my package.json engines. Any idea on why I might be missing the TextDecoder? I also tried on a Linux machine with node v12.10.0.

I'm also using typescript, maybe I've done a goof in that part

Honestly no clue, are you using something like nvm to manage your node installation(s)?

Could you console.log(process.version) at the beginning of your code?

Honestly no clue, are you using something like nvm to manage your node installation(s)?

Could you console.log(process.version) at the beginning of your code?

Still getting v12.15.0

Using Typescript shouldn't matter here, frankly, you may be compiling your code to an older Javascript flavor (depending on target), sure, but you're still running it on a node version high enough.

Could you try to write a reproducable sample I can just copy paste?

I tried bumping everything in the compiler options but no luck.

I'm not sure if I can do a sample but the code is open source and you can find it in https://github.com/GameFeeder/GameFeeder/tree/205-discord-presence. I think that simply checking out to that branch, yarn install and yarn test should be enough to see the error. If that's not useful let me know and I'll try writing a minimal reproducible sample.

I also have this problem. Here's a bare minimum project

Node v13.11.0
discord.js 12.0.2
Windows 10

package.json

{
  "name": "jest-test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "jest ./test"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "discord.js": "^12.0.2",
    "jest": "^25.1.0"
  }
}

test/a.test.js

const Discord = require('discord.js')

describe('test case', () => {
  it('passes', () => {

  })
})

Output:

> [email protected] test C:\Users\Admin\Desktop\nodetest\jest-test
> jest ./test

FAIL test/a.test.js
  ● Test suite failed to run

    TypeError: TextDecoder is not a constructor

    > 1 | const Discord = require('discord.js')
        | ^
      2 |
      3 | describe('test case', () => {
      4 |   it('passes', () => {

      at Object.<anonymous> (node_modules/discord.js/src/WebSocket.js:22:12)
      at Object.<anonymous> (test/a.test.js:1:1)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        1.515s
Ran all test suites matching /.\\test/i.
npm ERR! Test failed.  See above for more details.

Edit: To be clear, this is an extremely debilitating issue - almost all my tests are failing because of this. If I try to revert back to 12.0.0, the tests still fail for some reason and I'm at a loss as to what to do

Does this only occur when running tests? Or is it preventing your bot from compiling and running?

I'll keep the snarky remarks to myself:

The standard testEnvironment for jest tests is the browser/DOM, you need to change it via the CLI or config option to node

https://jestjs.io/docs/en/configuration#testenvironmentoptions-object

jest.config.js

module.exports = {
    testEnvironment: 'node'
}
Was this page helpful?
0 / 5 - 0 ratings