Skip to content

Commit deec350

Browse files
committed
Run prettier on tests/**
1 parent 2f9b2ed commit deec350

16 files changed

+6502
-6283
lines changed

.prettierrc

+10-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
1-
{}
1+
{
2+
"overrides": [
3+
{
4+
"files": ["tests/**/*.js"],
5+
"options": {
6+
"printWidth": 10000000
7+
}
8+
}
9+
]
10+
}

tests/configs.test.js

+15-19
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,23 @@
1-
2-
3-
import { AutoConfig, env } from '../src/transformers.js';
4-
import { getFile } from '../src/utils/hub.js';
1+
import { AutoConfig, env } from "../src/transformers.js";
2+
import { getFile } from "../src/utils/hub.js";
53

64
// Initialise the testing environment
75
env.allowLocalModels = false;
86
env.useFSCache = false;
97

108
const TEST_DATA = {
11-
'Xenova/bert-base-uncased': {
12-
model_type: 'bert',
13-
},
14-
}
15-
16-
describe('Configs', () => {
17-
18-
for (const [model_id, minimal_config] of Object.entries(TEST_DATA)) {
9+
"Xenova/bert-base-uncased": {
10+
model_type: "bert",
11+
},
12+
};
1913

20-
it(model_id, async () => {
21-
const config = await AutoConfig.from_pretrained(model_id);
22-
for(const [key, value] of Object.entries(minimal_config)) {
23-
expect(config[key]).toEqual(value);
24-
}
25-
});
26-
}
14+
describe("Configs", () => {
15+
for (const [model_id, minimal_config] of Object.entries(TEST_DATA)) {
16+
it(model_id, async () => {
17+
const config = await AutoConfig.from_pretrained(model_id);
18+
for (const [key, value] of Object.entries(minimal_config)) {
19+
expect(config[key]).toEqual(value);
20+
}
21+
});
22+
}
2723
});

tests/data_structures.test.js

+28-31
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,33 @@
1+
import { PriorityQueue } from "../src/utils/data-structures.js";
12

3+
describe("Priority queue", () => {
4+
const EXAMPLE_ARRAY = [2, 5, 3, 1, 4];
5+
it("default (max heap)", () => {
6+
const queue = new PriorityQueue();
7+
queue.extend(EXAMPLE_ARRAY);
8+
expect(queue.pop()).toBe(5);
9+
});
210

3-
import { PriorityQueue } from '../src/utils/data-structures.js';
11+
it("min heap", () => {
12+
const queue = new PriorityQueue((a, b) => a < b);
13+
queue.extend(EXAMPLE_ARRAY);
14+
expect(queue.pop()).toBe(1);
15+
});
416

17+
it("heap w/ max size", () => {
18+
const queue = new PriorityQueue((a, b) => a > b, 3);
19+
queue.extend([1, 2, 3, 4, 5, 4, 3, 2, 1]);
20+
expect(queue.pop()).toBe(5);
521

6-
describe('Priority queue', () => {
7-
const EXAMPLE_ARRAY = [2, 5, 3, 1, 4];
8-
it('default (max heap)', () => {
9-
const queue = new PriorityQueue();
10-
queue.extend(EXAMPLE_ARRAY);
11-
expect(queue.pop()).toBe(5);
12-
});
13-
14-
it('min heap', () => {
15-
const queue = new PriorityQueue((a, b) => a < b);
16-
queue.extend(EXAMPLE_ARRAY);
17-
expect(queue.pop()).toBe(1);
18-
});
19-
20-
it('heap w/ max size', () => {
21-
const queue = new PriorityQueue((a, b) => a > b, 3);
22-
queue.extend([1, 2, 3, 4, 5, 4, 3, 2, 1]);
23-
expect(queue.pop()).toBe(5);
24-
25-
// Test with random sizes
26-
const sizes = [1, 3, 4, 5, 8, 9, 15, 16, 31, 32, 127, 128];
27-
const arr = Array.from({ length: 100 }, _ => Math.random());
28-
const max = Math.max(...arr);
29-
for (const size of sizes) {
30-
const queue = new PriorityQueue((a, b) => a > b, size);
31-
queue.extend(arr);
32-
expect(queue.pop()).toBe(max);
33-
expect(queue.size).toBeLessThanOrEqual(size);
34-
}
35-
});
22+
// Test with random sizes
23+
const sizes = [1, 3, 4, 5, 8, 9, 15, 16, 31, 32, 127, 128];
24+
const arr = Array.from({ length: 100 }, (_) => Math.random());
25+
const max = Math.max(...arr);
26+
for (const size of sizes) {
27+
const queue = new PriorityQueue((a, b) => a > b, size);
28+
queue.extend(arr);
29+
expect(queue.pop()).toBe(max);
30+
expect(queue.size).toBeLessThanOrEqual(size);
31+
}
32+
});
3633
});

tests/init.js

+42-59
Original file line numberDiff line numberDiff line change
@@ -7,71 +7,54 @@ import * as types from "node:util/types";
77
import { onnxruntimeBackend } from "onnxruntime-node/dist/backend";
88
import * as ONNX_COMMON from "onnxruntime-common";
99

10-
1110
/**
1211
* A workaround to define a new backend for onnxruntime, which
1312
* will not throw an error when running tests with jest.
1413
* For more information, see: https://github.com/jestjs/jest/issues/11864#issuecomment-1261468011
1514
*/
1615
export function init() {
17-
// In rare cases (specifically when running unit tests with GitHub actions), possibly due to
18-
// a large number of concurrent executions, onnxruntime might fallback to use the WASM backend.
19-
// In this case, we set the number of threads to 1 to avoid errors like:
20-
// - `TypeError: The worker script or module filename must be an absolute path or a relative path starting with './' or '../'. Received "blob:nodedata:..."`
21-
ONNX_COMMON.env.wasm.numThreads = 1;
22-
23-
let registerBackend = ONNX_COMMON.registerBackend;
24-
25-
// Define the constructors to monkey-patch
26-
const TYPED_ARRAYS_CONSTRUCTOR_NAMES = [
27-
"Int8Array",
28-
"Int16Array",
29-
"Int32Array",
30-
"BigInt64Array",
31-
"Uint8Array",
32-
"Uint8ClampedArray",
33-
"Uint16Array",
34-
"Uint32Array",
35-
"BigUint64Array",
36-
"Float32Array",
37-
"Float64Array",
38-
];
39-
40-
// Keep a reference to the original initialization method
41-
const originalMethod = onnxruntimeBackend.init;
42-
43-
// Monkey-patch the initialization function
44-
onnxruntimeBackend.init = function (...args) {
45-
// There is probably a better way to do this
46-
Array.isArray = x =>
47-
typeof x === "object" &&
48-
x !== null &&
49-
typeof x.length === "number" &&
50-
x?.constructor.toString() === Array.toString();
51-
52-
// For each typed array constructor
53-
for (const ctorName of TYPED_ARRAYS_CONSTRUCTOR_NAMES) {
54-
// Get the constructor from the current context
55-
const ctor = globalThis[ctorName];
56-
57-
// Get the corresponding test function from the `util` module
58-
const value = types[`is${ctorName}`].bind(types);
59-
60-
// Monkey-patch the constructor so "x instanceof ctor" returns "types[`is${ctorName}`](x)"
61-
Object.defineProperty(ctor, Symbol.hasInstance, {
62-
value,
63-
writable: true, // writable=true is necessary to overwrite the default implementation (and allow subsequent overwrites)
64-
configurable: false,
65-
enumerable: false,
66-
});
67-
}
68-
69-
// Call the original method
70-
return originalMethod.apply(this, args);
71-
};
72-
73-
// Register the backend with the highest priority, so it is used instead of the default one
74-
registerBackend("test", onnxruntimeBackend, Number.POSITIVE_INFINITY);
16+
// In rare cases (specifically when running unit tests with GitHub actions), possibly due to
17+
// a large number of concurrent executions, onnxruntime might fallback to use the WASM backend.
18+
// In this case, we set the number of threads to 1 to avoid errors like:
19+
// - `TypeError: The worker script or module filename must be an absolute path or a relative path starting with './' or '../'. Received "blob:nodedata:..."`
20+
ONNX_COMMON.env.wasm.numThreads = 1;
21+
22+
let registerBackend = ONNX_COMMON.registerBackend;
23+
24+
// Define the constructors to monkey-patch
25+
const TYPED_ARRAYS_CONSTRUCTOR_NAMES = ["Int8Array", "Int16Array", "Int32Array", "BigInt64Array", "Uint8Array", "Uint8ClampedArray", "Uint16Array", "Uint32Array", "BigUint64Array", "Float32Array", "Float64Array"];
26+
27+
// Keep a reference to the original initialization method
28+
const originalMethod = onnxruntimeBackend.init;
29+
30+
// Monkey-patch the initialization function
31+
onnxruntimeBackend.init = function (...args) {
32+
// There is probably a better way to do this
33+
Array.isArray = (x) => typeof x === "object" && x !== null && typeof x.length === "number" && x?.constructor.toString() === Array.toString();
34+
35+
// For each typed array constructor
36+
for (const ctorName of TYPED_ARRAYS_CONSTRUCTOR_NAMES) {
37+
// Get the constructor from the current context
38+
const ctor = globalThis[ctorName];
39+
40+
// Get the corresponding test function from the `util` module
41+
const value = types[`is${ctorName}`].bind(types);
42+
43+
// Monkey-patch the constructor so "x instanceof ctor" returns "types[`is${ctorName}`](x)"
44+
Object.defineProperty(ctor, Symbol.hasInstance, {
45+
value,
46+
writable: true, // writable=true is necessary to overwrite the default implementation (and allow subsequent overwrites)
47+
configurable: false,
48+
enumerable: false,
49+
});
50+
}
51+
52+
// Call the original method
53+
return originalMethod.apply(this, args);
54+
};
55+
56+
// Register the backend with the highest priority, so it is used instead of the default one
57+
registerBackend("test", onnxruntimeBackend, Number.POSITIVE_INFINITY);
7558
}
7659

7760
export const MAX_MODEL_LOAD_TIME = 10_000; // 10 seconds

0 commit comments

Comments
 (0)