Skip to content

Commit 2180882

Browse files
[SDK-3550] Remove polyfills from bundles (#951)
* Remove polyfills from bundle * Patch Jest's JSDom to include TextEncoding * Rename Custom Jest Environment * Remove unfetch ponyfill * Remove unfetch from tests
1 parent 4b614f7 commit 2180882

24 files changed

+98
-105
lines changed

__tests__/Auth0Client/buildAuthorizeUrl.test.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import 'fast-text-encoding';
21
import { verify } from '../../src/jwt';
32

43
// @ts-ignore

__tests__/Auth0Client/buildLogoutUrl.test.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import 'fast-text-encoding';
2-
import unfetch from 'unfetch';
31
import { verify } from '../../src/jwt';
42
import { MessageChannel } from 'worker_threads';
53
import * as utils from '../../src/utils';
@@ -12,13 +10,12 @@ import { assertUrlEquals, setupFn } from './helpers';
1210

1311
import { TEST_CLIENT_ID, TEST_CODE_CHALLENGE, TEST_DOMAIN } from '../constants';
1412

15-
jest.mock('unfetch');
1613
jest.mock('es-cookie');
1714
jest.mock('../../src/jwt');
1815
jest.mock('../../src/worker/token.worker');
1916

2017
const mockWindow = <any>global;
21-
const mockFetch = (mockWindow.fetch = <jest.Mock>unfetch);
18+
const mockFetch = <jest.Mock>mockWindow.fetch;
2219
const mockVerify = <jest.Mock>verify;
2320

2421
jest

__tests__/Auth0Client/checkSession.test.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import 'fast-text-encoding';
21
import * as esCookie from 'es-cookie';
3-
import unfetch from 'unfetch';
42
import { verify } from '../../src/jwt';
53
import { MessageChannel } from 'worker_threads';
64
import * as utils from '../../src/utils';
@@ -22,13 +20,12 @@ import {
2220
TEST_STATE
2321
} from '../constants';
2422

25-
jest.mock('unfetch');
2623
jest.mock('es-cookie');
2724
jest.mock('../../src/jwt');
2825
jest.mock('../../src/worker/token.worker');
2926

3027
const mockWindow = <any>global;
31-
const mockFetch = (mockWindow.fetch = <jest.Mock>unfetch);
28+
const mockFetch = <jest.Mock>mockWindow.fetch;
3229
const mockVerify = <jest.Mock>verify;
3330

3431
jest
@@ -38,7 +35,7 @@ jest
3835
jest.spyOn(utils, 'runPopup');
3936

4037
const setup = setupFn(mockVerify);
41-
const checkSession = checkSessionFn(mockFetch);
38+
const checkSession = checkSessionFn(window.fetch);
4239

4340
describe('Auth0Client', () => {
4441
const oldWindowLocation = window.location;

__tests__/Auth0Client/constructor.test.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import 'fast-text-encoding';
2-
import unfetch from 'unfetch';
31
import { verify } from '../../src/jwt';
42
import { MessageChannel } from 'worker_threads';
53
import * as utils from '../../src/utils';
@@ -13,13 +11,12 @@ import { assertUrlEquals, loginWithRedirectFn, setupFn } from './helpers';
1311
import { TEST_CLIENT_ID, TEST_CODE_CHALLENGE, TEST_DOMAIN } from '../constants';
1412
import { ICache } from '../../src/cache';
1513

16-
jest.mock('unfetch');
1714
jest.mock('es-cookie');
1815
jest.mock('../../src/jwt');
1916
jest.mock('../../src/worker/token.worker');
2017

2118
const mockWindow = <any>global;
22-
const mockFetch = (mockWindow.fetch = <jest.Mock>unfetch);
19+
const mockFetch = <jest.Mock>mockWindow.fetch;
2320
const mockVerify = <jest.Mock>verify;
2421

2522
const mockCache: ICache = {

__tests__/Auth0Client/getIdTokenClaims.test.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import 'fast-text-encoding';
2-
import unfetch from 'unfetch';
31
import { verify } from '../../src/jwt';
42
import { MessageChannel } from 'worker_threads';
53
import * as utils from '../../src/utils';
@@ -12,13 +10,12 @@ import { loginWithPopupFn, loginWithRedirectFn, setupFn } from './helpers';
1210

1311
import { TEST_CODE_CHALLENGE } from '../constants';
1412

15-
jest.mock('unfetch');
1613
jest.mock('es-cookie');
1714
jest.mock('../../src/jwt');
1815
jest.mock('../../src/worker/token.worker');
1916

2017
const mockWindow = <any>global;
21-
const mockFetch = (mockWindow.fetch = <jest.Mock>unfetch);
18+
const mockFetch = <jest.Mock>mockWindow.fetch;
2219
const mockVerify = <jest.Mock>verify;
2320

2421
jest

__tests__/Auth0Client/getTokenSilently.test.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import 'fast-text-encoding';
21
import * as esCookie from 'es-cookie';
3-
import unfetch from 'unfetch';
42
import { verify } from '../../src/jwt';
53
import { MessageChannel } from 'worker_threads';
64
import * as utils from '../../src/utils';
@@ -47,15 +45,13 @@ import {
4745
INVALID_REFRESH_TOKEN_ERROR_MESSAGE
4846
} from '../../src/constants';
4947
import { GenericError } from '../../src/errors';
50-
import { CacheKey } from '../../src/cache';
5148

52-
jest.mock('unfetch');
5349
jest.mock('es-cookie');
5450
jest.mock('../../src/jwt');
5551
jest.mock('../../src/worker/token.worker');
5652

5753
const mockWindow = <any>global;
58-
const mockFetch = (mockWindow.fetch = <jest.Mock>unfetch);
54+
const mockFetch = <jest.Mock>mockWindow.fetch;
5955
const mockVerify = <jest.Mock>verify;
6056
const tokenVerifier = require('../../src/jwt').verify;
6157

__tests__/Auth0Client/getTokenWithPopup.test.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import 'fast-text-encoding';
2-
import unfetch from 'unfetch';
31
import { verify } from '../../src/jwt';
42
import { MessageChannel } from 'worker_threads';
53
import * as utils from '../../src/utils';
@@ -30,13 +28,12 @@ import { Auth0ClientOptions } from '../../src';
3028
import { DEFAULT_AUTH0_CLIENT } from '../../src/constants';
3129
import { expect } from '@jest/globals';
3230

33-
jest.mock('unfetch');
3431
jest.mock('es-cookie');
3532
jest.mock('../../src/jwt');
3633
jest.mock('../../src/worker/token.worker');
3734

3835
const mockWindow = <any>global;
39-
const mockFetch = (mockWindow.fetch = <jest.Mock>unfetch);
36+
const mockFetch = <jest.Mock>mockWindow.fetch;
4037
const mockVerify = <jest.Mock>verify;
4138
const assertPost = assertPostFn(mockFetch);
4239

__tests__/Auth0Client/getUser.test.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import 'fast-text-encoding';
2-
import unfetch from 'unfetch';
31
import { verify } from '../../src/jwt';
42
import { MessageChannel } from 'worker_threads';
53
import * as utils from '../../src/utils';
@@ -12,13 +10,12 @@ import { setupFn } from './helpers';
1210

1311
import { TEST_CODE_CHALLENGE } from '../constants';
1412

15-
jest.mock('unfetch');
1613
jest.mock('es-cookie');
1714
jest.mock('../../src/jwt');
1815
jest.mock('../../src/worker/token.worker');
1916

2017
const mockWindow = <any>global;
21-
const mockFetch = (mockWindow.fetch = <jest.Mock>unfetch);
18+
const mockFetch = <jest.Mock>mockWindow.fetch;
2219
const mockVerify = <jest.Mock>verify;
2320

2421
jest

__tests__/Auth0Client/handleRedirectCallback.test.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import 'fast-text-encoding';
2-
import unfetch from 'unfetch';
31
import { verify } from '../../src/jwt';
42
import { MessageChannel } from 'worker_threads';
53
import * as utils from '../../src/utils';
@@ -33,13 +31,12 @@ import {
3331

3432
import { DEFAULT_AUTH0_CLIENT } from '../../src/constants';
3533

36-
jest.mock('unfetch');
3734
jest.mock('es-cookie');
3835
jest.mock('../../src/jwt');
3936
jest.mock('../../src/worker/token.worker');
4037

4138
const mockWindow = <any>global;
42-
const mockFetch = (mockWindow.fetch = <jest.Mock>unfetch);
39+
const mockFetch = <jest.Mock>mockWindow.fetch;
4340
const mockVerify = <jest.Mock>verify;
4441

4542
jest

__tests__/Auth0Client/isAuthenticated.test.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import 'fast-text-encoding';
2-
import unfetch from 'unfetch';
31
import { verify } from '../../src/jwt';
42
import { MessageChannel } from 'worker_threads';
53
import * as utils from '../../src/utils';
@@ -12,13 +10,12 @@ import { loginWithPopupFn, loginWithRedirectFn, setupFn } from './helpers';
1210

1311
import { TEST_CODE_CHALLENGE } from '../constants';
1412

15-
jest.mock('unfetch');
1613
jest.mock('es-cookie');
1714
jest.mock('../../src/jwt');
1815
jest.mock('../../src/worker/token.worker');
1916

2017
const mockWindow = <any>global;
21-
const mockFetch = (mockWindow.fetch = <jest.Mock>unfetch);
18+
const mockFetch = <jest.Mock>mockWindow.fetch;
2219
const mockVerify = <jest.Mock>verify;
2320

2421
jest

__tests__/Auth0Client/loginWithPopup.test.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import 'fast-text-encoding';
21
import * as esCookie from 'es-cookie';
3-
import unfetch from 'unfetch';
42
import { verify } from '../../src/jwt';
53
import { MessageChannel } from 'worker_threads';
64
import * as utils from '../../src/utils';
@@ -37,13 +35,12 @@ import {
3735
DEFAULT_POPUP_CONFIG_OPTIONS
3836
} from '../../src/constants';
3937

40-
jest.mock('unfetch');
4138
jest.mock('es-cookie');
4239
jest.mock('../../src/jwt');
4340
jest.mock('../../src/worker/token.worker');
4441

4542
const mockWindow = <any>global;
46-
const mockFetch = (mockWindow.fetch = <jest.Mock>unfetch);
43+
const mockFetch = <jest.Mock>mockWindow.fetch;
4744
const mockVerify = <jest.Mock>verify;
4845
const tokenVerifier = require('../../src/jwt').verify;
4946

__tests__/Auth0Client/loginWithRedirect.test.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import 'fast-text-encoding';
21
import * as esCookie from 'es-cookie';
3-
import unfetch from 'unfetch';
42
import { verify } from '../../src/jwt';
53
import { MessageChannel } from 'worker_threads';
64
import * as utils from '../../src/utils';
@@ -37,13 +35,12 @@ import {
3735
} from '../constants';
3836
import version from '../../src/version';
3937

40-
jest.mock('unfetch');
4138
jest.mock('es-cookie');
4239
jest.mock('../../src/jwt');
4340
jest.mock('../../src/worker/token.worker');
4441

4542
const mockWindow = <any>global;
46-
const mockFetch = (mockWindow.fetch = <jest.Mock>unfetch);
43+
const mockFetch = <jest.Mock>mockWindow.fetch;
4744
const mockVerify = <jest.Mock>verify;
4845
const mockCookies = require('es-cookie');
4946
const tokenVerifier = require('../../src/jwt').verify;

__tests__/Auth0Client/logout.test.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import 'fast-text-encoding';
21
import * as esCookie from 'es-cookie';
3-
import unfetch from 'unfetch';
42
import { verify } from '../../src/jwt';
53
import { MessageChannel } from 'worker_threads';
64
import * as utils from '../../src/utils';
@@ -14,13 +12,12 @@ import { loginWithRedirectFn, setupFn } from './helpers';
1412
import { TEST_CLIENT_ID, TEST_CODE_CHALLENGE, TEST_DOMAIN } from '../constants';
1513
import { InMemoryAsyncCacheNoKeys } from '../cache/shared';
1614

17-
jest.mock('unfetch');
1815
jest.mock('es-cookie');
1916
jest.mock('../../src/jwt');
2017
jest.mock('../../src/worker/token.worker');
2118

2219
const mockWindow = <any>global;
23-
const mockFetch = (mockWindow.fetch = <jest.Mock>unfetch);
20+
const mockFetch = <jest.Mock>mockWindow.fetch;
2421
const mockVerify = <jest.Mock>verify;
2522
const loginWithRedirect = loginWithRedirectFn(mockWindow, mockFetch);
2623

__tests__/api.test.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import fetch from 'unfetch';
2-
31
import {
42
DEFAULT_AUTH0_CLIENT,
53
DEFAULT_SILENT_TOKEN_RETRY_COUNT
@@ -16,7 +14,6 @@ import { expect } from '@jest/globals';
1614
(<any>global).MessageChannel = MessageChannel;
1715

1816
jest.mock('../src/worker/token.worker');
19-
jest.mock('unfetch');
2017

2118
const mockFetch = <jest.Mock>fetch;
2219
(<any>global).MessageChannel = MessageChannel;
@@ -258,7 +255,7 @@ describe('oauthToken', () => {
258255
});
259256

260257
it('throws a fetch error when the network is down', async () => {
261-
mockFetch.mockReturnValue(Promise.reject(new ProgressEvent('error')));
258+
mockFetch.mockReturnValue(Promise.reject(new Error('Failed to fetch')));
262259

263260
await expect(
264261
oauthToken({

__tests__/http.test.ts

-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
import fetch from 'unfetch';
21
import { MfaRequiredError } from '../src/errors';
32
import { switchFetch, getJSON } from '../src/http';
43
import { expect } from '@jest/globals';
54

65
jest.mock('../src/worker/token.worker');
7-
jest.mock('unfetch');
86

97
const mockUnfetch = <jest.Mock>fetch;
108

__tests__/token.worker.test.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
import unfetch from 'unfetch';
21
import { MISSING_REFRESH_TOKEN_ERROR_MESSAGE } from '../src/constants';
32
import { assertPostFn } from './Auth0Client/helpers';
43
import * as utils from '../src/utils';
54
import { expect } from '@jest/globals';
65

7-
jest.mock('unfetch');
8-
9-
const mockFetch = unfetch as jest.Mock;
6+
const mockFetch = <jest.Mock>window.fetch;
107

118
describe('token worker', () => {
129
let originalFetch;

__tests__/utils.test.ts

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import 'fast-text-encoding';
2-
31
import {
42
parseQueryResult,
53
createQueryParams,

jest.config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module.exports = {
22
rootDir: './',
3-
testEnvironment: 'jsdom',
3+
testEnvironment: './jest.environment',
44
moduleFileExtensions: ['ts', 'js'],
55
testMatch: ['**/__tests__/**/*.test.ts'],
66
coveragePathIgnorePatterns: [
@@ -15,7 +15,7 @@ module.exports = {
1515
],
1616
coverageReporters: ['lcov', 'text', 'text-summary'],
1717
preset: 'ts-jest',
18-
setupFiles: ['jest-localstorage-mock'],
18+
setupFiles: ['jest-localstorage-mock', './jest.setup.js'],
1919
globals: {
2020
'ts-jest': {
2121
tsconfig: './tsconfig.test.json'

jest.environment.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const JSDOMEnvironment = require('jest-environment-jsdom').default;
2+
const util = require('util');
3+
4+
/**
5+
* Custom Jest Environment based on JSDOMEnvironment to support TextEncoder and TextDecoder.
6+
*
7+
* ref: https://github.com/jsdom/jsdom/issues/2524
8+
*/
9+
class CustomJSDOMEnvironment extends JSDOMEnvironment {
10+
constructor(config, context) {
11+
super(config, context);
12+
}
13+
14+
async setup() {
15+
await super.setup();
16+
this.global.TextEncoder = util.TextEncoder;
17+
this.global.TextDecoder = util.TextDecoder;
18+
}
19+
}
20+
21+
module.exports = CustomJSDOMEnvironment;

jest.setup.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require('jest-fetch-mock').enableMocks();

0 commit comments

Comments
 (0)