Skip to content

Commit 6374f4a

Browse files
committed
use global dependency when DataLoader initiated, refactor tests for easier mocking
1 parent ca45b68 commit 6374f4a

File tree

5 files changed

+32
-23
lines changed

5 files changed

+32
-23
lines changed

src/__tests__/browser.test.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,21 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
// Set up mocks to simulate a recent browser environment.
9-
// Remove process.nextTick.
10-
// This mock must be imported before importing DataLoader.
11-
import './nextTick.mock.ts';
12-
138
import DataLoader from '../index.ts';
14-
import { describe, it, expect } from '@jest/globals';
9+
import { describe, it, expect, beforeEach, afterEach } from '@jest/globals';
1510

1611
describe('Browser support', () => {
12+
const originalProcess = global.process;
13+
14+
beforeEach(() => {
15+
// @ts-expect-error testing a browser environment by removing process.nextTick
16+
global.process = { nextTick: undefined };
17+
});
18+
19+
afterEach(() => {
20+
global.process = originalProcess;
21+
});
22+
1723
it('batches multiple requests without process.nextTick', async () => {
1824
const loadCalls: ReadonlyArray<number>[] = [];
1925
const identityLoader = new DataLoader<number, number>(async keys => {

src/__tests__/nextTick.mock.ts

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/__tests__/oldbrowser.test.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,25 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
// Set up mocks to simulate an old browser environment.
9-
// Remove both setImmediate and process.nextTick.
10-
// These mocks must be imported before importing DataLoader.
11-
import './nextTick.mock.ts';
12-
import './setImmediate.mock.ts';
13-
148
import DataLoader from '../index.ts';
15-
import { describe, it, expect } from '@jest/globals';
9+
import { describe, it, expect, beforeEach, afterEach } from '@jest/globals';
1610

1711
describe('Old browser support', () => {
12+
const originalSetImmediate = global.setImmediate;
13+
const originalProcess = global.process;
14+
15+
beforeEach(() => {
16+
// @ts-expect-error testing an old browser environment by removing setImmediate
17+
global.setImmediate = undefined;
18+
// @ts-expect-error testing an old browser environment by removing process.nextTick
19+
global.process = { nextTick: undefined };
20+
});
21+
22+
afterEach(() => {
23+
global.setImmediate = originalSetImmediate;
24+
global.process = originalProcess;
25+
});
26+
1827
it('batches multiple requests without setImmediate', async () => {
1928
const loadCalls: ReadonlyArray<number>[] = [];
2029
const identityLoader = new DataLoader<number, number>(async keys => {

src/__tests__/setImmediate.mock.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,8 @@ class DataLoader<K, V, C = K> {
348348
// for enqueuing a job to be performed after promise microtasks and before the
349349
// next macrotask. For browser environments, a macrotask is used (via
350350
// setImmediate or setTimeout) at a potential performance penalty.
351-
const enqueuePostPromiseJob: (fn: () => void) => void =
352-
typeof process === 'object' && typeof process.nextTick === 'function'
351+
function getEnqueuePostPromiseJob(): (fn: () => void) => void {
352+
return typeof process === 'object' && typeof process.nextTick === 'function'
353353
? function (fn) {
354354
if (!resolvedPromise) {
355355
resolvedPromise = Promise.resolve();
@@ -365,6 +365,7 @@ const enqueuePostPromiseJob: (fn: () => void) => void =
365365
: function (fn) {
366366
setTimeout(fn);
367367
};
368+
}
368369

369370
// Private: cached resolved Promise instance
370371
let resolvedPromise: Promise<void> | undefined;
@@ -413,7 +414,7 @@ function getValidBatchScheduleFn<K, V, C>(
413414
): (callback: () => void) => void {
414415
const batchScheduleFn = options && options.batchScheduleFn;
415416
if (batchScheduleFn === undefined) {
416-
return enqueuePostPromiseJob;
417+
return getEnqueuePostPromiseJob();
417418
}
418419
if (typeof batchScheduleFn !== 'function') {
419420
throw new TypeError(

0 commit comments

Comments
 (0)