Skip to content

Commit faf3fb0

Browse files
committed
mirate to typescript
1 parent ed9862a commit faf3fb0

File tree

9 files changed

+208
-333
lines changed

9 files changed

+208
-333
lines changed

src/__tests__/abuse.test.js renamed to src/__tests__/abuse.test.ts

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,24 @@
33
*
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
6-
*
7-
* @flow
86
*/
97

10-
const DataLoader = require('..');
8+
import DataLoader from '../index.ts';
9+
import { describe, it, expect } from '@jest/globals';
1110

1211
describe('Provides descriptive error messages for API abuse', () => {
1312
it('Loader creation requires a function', () => {
1413
expect(() => {
15-
// $FlowExpectError
16-
new DataLoader(); // eslint-disable-line no-new
14+
// @ts-expect-error testing invalid usage
15+
new DataLoader();
1716
}).toThrow(
1817
'DataLoader must be constructed with a function which accepts ' +
1918
'Array<key> and returns Promise<Array<value>>, but got: undefined.',
2019
);
2120

2221
expect(() => {
23-
// $FlowExpectError
24-
new DataLoader({}); // eslint-disable-line no-new
22+
// @ts-expect-error testing invalid usage
23+
new DataLoader({});
2524
}).toThrow(
2625
'DataLoader must be constructed with a function which accepts ' +
2726
'Array<key> and returns Promise<Array<value>>, but got: [object Object].',
@@ -32,15 +31,15 @@ describe('Provides descriptive error messages for API abuse', () => {
3231
const idLoader = new DataLoader<number, number>(async keys => keys);
3332

3433
expect(() => {
35-
// $FlowExpectError
34+
// @ts-expect-error testing invalid usage
3635
idLoader.load();
3736
}).toThrow(
3837
'The loader.load() function must be called with a value, ' +
3938
'but got: undefined.',
4039
);
4140

4241
expect(() => {
43-
// $FlowExpectError
42+
// @ts-expect-error testing invalid usage
4443
idLoader.load(null);
4544
}).toThrow(
4645
'The loader.load() function must be called with a value, ' +
@@ -57,15 +56,15 @@ describe('Provides descriptive error messages for API abuse', () => {
5756
const idLoader = new DataLoader<number, number>(async keys => keys);
5857

5958
expect(() => {
60-
// $FlowExpectError
59+
// @ts-expect-error testing invalid usage
6160
idLoader.loadMany();
6261
}).toThrow(
6362
'The loader.loadMany() function must be called with Array<key> ' +
6463
'but got: undefined.',
6564
);
6665

6766
expect(() => {
68-
// $FlowExpectError
67+
// @ts-expect-error testing invalid usage
6968
idLoader.loadMany(1, 2, 3);
7069
}).toThrow(
7170
'The loader.loadMany() function must be called with Array<key> ' +
@@ -79,7 +78,7 @@ describe('Provides descriptive error messages for API abuse', () => {
7978
});
8079

8180
it('Batch function must return a Promise, not null', async () => {
82-
// $FlowExpectError
81+
// @ts-expect-error testing invalid usage
8382
const badLoader = new DataLoader<number, number>(() => null);
8483

8584
let caughtError;
@@ -89,7 +88,7 @@ describe('Provides descriptive error messages for API abuse', () => {
8988
caughtError = error;
9089
}
9190
expect(caughtError).toBeInstanceOf(Error);
92-
expect((caughtError: any).message).toBe(
91+
expect((caughtError as Error).message).toBe(
9392
'DataLoader must be constructed with a function which accepts ' +
9493
'Array<key> and returns Promise<Array<value>>, but the function did ' +
9594
'not return a Promise: null.',
@@ -108,7 +107,7 @@ describe('Provides descriptive error messages for API abuse', () => {
108107
caughtError = error;
109108
}
110109
expect(caughtError).toBeInstanceOf(Error);
111-
expect((caughtError: any).message).toBe(
110+
expect((caughtError as Error).message).toBe(
112111
'DataLoader must be constructed with a function which accepts ' +
113112
'Array<key> and returns Promise<Array<value>>, but the function ' +
114113
'errored synchronously: Error: Mock Synchronous Error.',
@@ -117,7 +116,7 @@ describe('Provides descriptive error messages for API abuse', () => {
117116

118117
it('Batch function must return a Promise, not a value', async () => {
119118
// Note: this is returning the keys directly, rather than a promise to keys.
120-
// $FlowExpectError
119+
// @ts-expect-error testing invalid usage
121120
const badLoader = new DataLoader<number, number>(keys => keys);
122121

123122
let caughtError;
@@ -127,7 +126,7 @@ describe('Provides descriptive error messages for API abuse', () => {
127126
caughtError = error;
128127
}
129128
expect(caughtError).toBeInstanceOf(Error);
130-
expect((caughtError: any).message).toBe(
129+
expect((caughtError as Error).message).toBe(
131130
'DataLoader must be constructed with a function which accepts ' +
132131
'Array<key> and returns Promise<Array<value>>, but the function did ' +
133132
'not return a Promise: 1.',
@@ -136,7 +135,7 @@ describe('Provides descriptive error messages for API abuse', () => {
136135

137136
it('Batch function must return a Promise of an Array, not null', async () => {
138137
// Note: this resolves to undefined
139-
// $FlowExpectError
138+
// @ts-expect-error testing invalid usage
140139
const badLoader = new DataLoader<number, number>(async () => null);
141140

142141
let caughtError;
@@ -146,7 +145,7 @@ describe('Provides descriptive error messages for API abuse', () => {
146145
caughtError = error;
147146
}
148147
expect(caughtError).toBeInstanceOf(Error);
149-
expect((caughtError: any).message).toBe(
148+
expect((caughtError as Error).message).toBe(
150149
'DataLoader must be constructed with a function which accepts ' +
151150
'Array<key> and returns Promise<Array<value>>, but the function did ' +
152151
'not return a Promise of an Array: null.',
@@ -164,7 +163,7 @@ describe('Provides descriptive error messages for API abuse', () => {
164163
caughtError = error;
165164
}
166165
expect(caughtError).toBeInstanceOf(Error);
167-
expect((caughtError: any).message).toBe(
166+
expect((caughtError as Error).message).toBe(
168167
'DataLoader must be constructed with a function which accepts ' +
169168
'Array<key> and returns Promise<Array<value>>, but the function did ' +
170169
'not return a Promise of an Array of the same length as the Array ' +
@@ -180,17 +179,17 @@ describe('Provides descriptive error messages for API abuse', () => {
180179
}
181180

182181
expect(() => {
183-
// $FlowExpectError
184182
const incompleteMap = new IncompleteMap();
185183
const options = { cacheMap: incompleteMap };
186-
new DataLoader(async keys => keys, options); // eslint-disable-line no-new
184+
// @ts-expect-error testing invalid usage
185+
new DataLoader(async keys => keys, options);
187186
}).toThrow('Custom cacheMap missing methods: set, delete, clear');
188187
});
189188

190189
it('Requires a number for maxBatchSize', () => {
191190
expect(
192191
() =>
193-
// $FlowExpectError
192+
// @ts-expect-error testing invalid usage
194193
new DataLoader(async keys => keys, { maxBatchSize: null }),
195194
).toThrow('maxBatchSize must be a positive number: null');
196195
});
@@ -204,15 +203,15 @@ describe('Provides descriptive error messages for API abuse', () => {
204203
it('Requires a function for cacheKeyFn', () => {
205204
expect(
206205
() =>
207-
// $FlowExpectError
206+
// @ts-expect-error testing invalid usage
208207
new DataLoader(async keys => keys, { cacheKeyFn: null }),
209208
).toThrow('cacheKeyFn must be a function: null');
210209
});
211210

212211
it('Requires a function for batchScheduleFn', () => {
213212
expect(
214213
() =>
215-
// $FlowExpectError
214+
// @ts-expect-error testing invalid usage
216215
new DataLoader(async keys => keys, { batchScheduleFn: null }),
217216
).toThrow('batchScheduleFn must be a function: null');
218217
});

src/__tests__/browser.test.js renamed to src/__tests__/browser.test.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@
33
*
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
6-
*
7-
* @flow
86
*/
97

10-
// Mock out process.nextTick as not existing for this test before requiring.
11-
process.nextTick = (null: any);
12-
const DataLoader = require('..');
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+
13+
import DataLoader from '../index.ts';
14+
import { describe, it, expect } from '@jest/globals';
1315

1416
describe('Browser support', () => {
1517
it('batches multiple requests without process.nextTick', async () => {
16-
const loadCalls = [];
18+
const loadCalls: ReadonlyArray<number>[] = [];
1719
const identityLoader = new DataLoader<number, number>(async keys => {
1820
loadCalls.push(keys);
1921
return keys;

0 commit comments

Comments
 (0)