Mocha test loading glb file failing

I have the following code in my unit test to try and fetch a mocked up file (glb cube export from blender) to test if the renderer is functional. However, I am getting an issue with the three.js FileLoader that doesn’t make a ton of sense to me. The file is found as I can inspect the data inside the call, but the error I get here does not make sense to me…

Is there anyone here that knows what I can do so I can actually fetch the file correctly?

        const test3dFile = $.get("proofscopeComponent/tests/data/cube.glb")
        sinon.stub(window, 'fetch').callsFake((input, init) => {
            let requestUrl;

            if (input instanceof Request) {
                requestUrl = input.url;
            } else {
                requestUrl = input;
            }
            if (requestUrl.includes('get_3d_model_data')) {
                return test3dFile.then((data) => {
                    const response = new Response(data, {
                        status: 200,
                        headers: { 'Content-Type': 'model/gltf-binary' },
                    });

                    Object.defineProperty(response, 'url', { value: requestUrl });
                    return Promise.resolve(response);
                });
            }
            return originalFetch(input, init);
        });

What environment is your test running in, is that Node.js? And if so, what Node.js version? A quick test here might be…

Promise.resolve(123).finally(() => {
  console.log('finally runs');
});

… and see if that prints any errors. But any modern browser or Node.js version v10+ should support promise.finally.

I tested that code snippet and it does appear to be supported. I am running Node v20.12.2
The snippet does not print any errors and during non-testing the finally call works just fine…

After trying to figure it out I tihnk I know what it is…
Jquery is still in broad use in our software and that $.get(…) does not define finally.
I will have to wrap that in a standard promise so that it works…

Will need to test this and report back

1 Like