diff --git a/jest.config.mjs b/jest.config.mjs index 0d15ce842..3e55440e1 100644 --- a/jest.config.mjs +++ b/jest.config.mjs @@ -128,7 +128,7 @@ export default { // runner: "jest-runner", // The paths to modules that run some code to configure or set up the testing environment before each test - // setupFiles: [], + setupFiles: ["./tests/setup.js"], // A list of paths to modules that run some code to configure or set up the testing framework before each test // setupFilesAfterEnv: [], diff --git a/src/utils/tensor.js b/src/utils/tensor.js index 0022a4f72..42c8eaeba 100644 --- a/src/utils/tensor.js +++ b/src/utils/tensor.js @@ -22,9 +22,10 @@ import { TensorOpRegistry } from '../ops/registry.js'; export const DataTypeMap = Object.freeze({ float32: Float32Array, - // @ts-ignore ts(2552) Limited availability of Float16Array across browsers: - // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float16Array - float16: typeof Float16Array !== "undefined" ? Float16Array: Uint16Array, + // NOTE: onnxruntime-node's native binding expects Uint16Array for float16 tensors. + // Float16Array is not yet supported by onnxruntime-node (see microsoft/onnxruntime#26742). + // Once onnxruntime adds Float16Array support, this can be updated. + float16: Uint16Array, float64: Float64Array, string: Array, // string[] int8: Int8Array, diff --git a/tests/setup.js b/tests/setup.js new file mode 100644 index 000000000..88454fe4b --- /dev/null +++ b/tests/setup.js @@ -0,0 +1,17 @@ +// WORKAROUND: onnxruntime-node's native binding expects Uint16Array for float16 tensors, +// but onnxruntime-common uses Float16Array when available (Node 20+). +// Hide Float16Array before any onnxruntime imports to force Uint16Array usage. +// +// This file runs before any test imports, ensuring Float16Array is hidden +// when onnxruntime-common's checkTypedArray() runs. +// +// TODO: Remove this workaround once onnxruntime-node adds Float16Array support. +// Track the upstream PR: https://github.com/microsoft/onnxruntime/pull/26742 +// When that PR is merged and released, this file can be deleted and the +// setupFiles entry in jest.config.mjs can be removed. + +if (typeof globalThis.Float16Array !== 'undefined') { + // Save reference in case other code needs it + globalThis._Float16Array = globalThis.Float16Array; + delete globalThis.Float16Array; +}