summaryrefslogtreecommitdiffhomepage
path: root/packages/browser/jest-environment.js
blob: 3d33072b670c4c5c66841e73220454e4a5b129e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/* eslint-disable @typescript-eslint/no-var-requires */
const Environment = require('jest-environment-jsdom');

/**
 * Set up a custom JSDOM-based test environment for Jest so we can add things JSDOM doesn't support
 */
class CustomTestEnvironment extends Environment {
  async setup() {
    await super.setup();
    /**
     * JSDOM doesn't implement TextEncoder so we need to fake it with Node's
     *
     * Solved thanks to https://stackoverflow.com/a/57713960/2133271
     */
    if (typeof this.global.TextEncoder === 'undefined') {
      const { TextEncoder } = require('util');
      this.global.TextEncoder = TextEncoder;
    }

    /**
     * Add support for TextDecoder to JSDOM
     */
    if (typeof this.global.TextDecoder === 'undefined') {
      const { TextDecoder } = require('util');
      this.global.TextDecoder = TextDecoder;
    }
  }
}

module.exports = CustomTestEnvironment;