Skip to content

Commit

Permalink
feat: add polyfill for TextEncoder
Browse files Browse the repository at this point in the history
fixes #1914
fixes #2514
  • Loading branch information
ahnpnl committed Jun 19, 2024
1 parent 09847bc commit c0af6c3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
7 changes: 7 additions & 0 deletions setup-jest.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
require('zone.js');
require('zone.js/testing');
const { TextEncoder, TextDecoder } = require('util');

const { getTestBed } = require('@angular/core/testing');
const {
BrowserDynamicTestingModule,
platformBrowserDynamicTesting,
} = require('@angular/platform-browser-dynamic/testing');

if (typeof globalThis.TextEncoder === 'undefined') {
globalThis.TextEncoder = TextEncoder;
globalThis.TextDecoder = TextDecoder;
}

const testEnvironmentOptions = globalThis.ngJest?.testEnvironmentOptions ?? Object.create(null);

getTestBed().initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting(), testEnvironmentOptions);
6 changes: 6 additions & 0 deletions setup-jest.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import 'zone.js';
import 'zone.js/testing';
import { getTestBed } from '@angular/core/testing';
import { BrowserDynamicTestingModule, platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing';
import { TextEncoder, TextDecoder } from 'util';

if (typeof globalThis.TextEncoder === 'undefined') {
globalThis.TextEncoder = TextEncoder;
globalThis.TextDecoder = TextDecoder;
}

const testEnvironmentOptions = globalThis.ngJest?.testEnvironmentOptions ?? Object.create(null);

Expand Down
13 changes: 13 additions & 0 deletions src/config/setup-jest.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ describe('setup-jest', () => {

beforeEach(() => {
delete globalThis.ngJest;
delete globalThis.TextEncoder;
jest.clearAllMocks();
jest.resetModules();
});
Expand Down Expand Up @@ -78,6 +79,12 @@ describe('setup-jest', () => {
errorOnUnknownProperties: true,
});
});

test('should always have TextEncoder in globalThis', async () => {
await import('../../setup-jest');

expect(globalThis.TextEncoder).toBeDefined();
});
});

describe('for ESM setup-jest, test environment initialization', () => {
Expand Down Expand Up @@ -107,5 +114,11 @@ describe('setup-jest', () => {
errorOnUnknownProperties: true,
});
});

test('should always have TextEncoder in globalThis', async () => {
await import('../../setup-jest.mjs');

expect(globalThis.TextEncoder).toBeDefined();
});
});
});

0 comments on commit c0af6c3

Please sign in to comment.