-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bug]: fs.readFileSync().buffer instanceOf ArrayBuffer is false in jest-environment-jsdom #12586
Comments
jest-environment-jsdom's global jest-environment-jsdom's env is constructed through when
Source code of jsdom about how runScripts influence window object of jsdom: more links about jsdom's runScripts behavior: |
Thanks for explanation! This explains what's going and why the test fails.
However, I was able to workaround this by creating custom environment in // jest.env.js
const JSDOMEnvironment = require("jest-environment-jsdom");
class CustomJSDOMEnvironment extends JSDOMEnvironment {
constructor(config) {
super({
...config,
globals: {
...config.globals,
Uint32Array,
Uint8Array,
ArrayBuffer,
},
});
}
}
module.exports = CustomJSDOMEnvironment; and then using this in // jest.config.js
module.exports = {
testEnvironment: "<rootDir>/jest.env.js",
} Though I am still not sure if the issue I reported is expected behavior or should be considered a bug? From the end user perspective that's pretty non-intuitive when some of the downstream libraries don't behave as expected on getting seemingly correct |
could you provide a reproduction with I am not sure if jsdom support TextDecoder will solve your problem. see jsdom/jsdom#2524 |
Underlying issue here is #2549 |
@F3n67u this was my repro: // encoder.test.js
import fs from "fs";
function encodeDecode(s) {
const encoder = new TextEncoder();
const decoder = new TextDecoder();
return decoder.decode(encoder.encode(s));
}
test("encode and decode message from file", () => {
let data = fs.readFileSync("file.txt");
let data2 = encodeDecode(data);
console.log(data.toString());
console.log(data2);
expect(data2).toEqual(data.toString());
}); and then I had a import util from "util";
import { TextDecoder } from "text-encoding";
global.TextEncoder = util.TextEncoder;
global.TextDecoder = TextDecoder; Full repro is at https://github.com/pankdm/jest-playground/tree/jest-encoder-repro and running the command: FAIL ./encoder.test.js
✕ encode and decode message from file (26 ms)
● encode and decode message from file
expect(received).toEqual(expected) // deep equality
Expected: "Hello world!
"
Received: ""
12 | console.log(data.toString());
13 | console.log(data2);
> 14 | expect(data2).toEqual(data.toString());
| ^
15 | });
16 |
at Object.<anonymous> (encoder.test.js:14:17) Basically the issue is coming from this if-branch: https://github.com/inexorabletash/text-encoding/blob/3f330964c0e97e1ed344c2a3e963f4598610a7ad/lib/encoding.js#L1086 where the output will always be empty (since the One interesting thing I realized is that we were using
this particular issue went away. |
Yes, This solution is better I think. I also suggest you use Here is how I modify the code:
to
I believe if jsdom support |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Version
27.5.1
Steps to reproduce
Repro at: https://github.com/pankdm/jest-playground/tree/jest-bug-report
git clone https://github.com/pankdm/jest-playground.git
cd jest-playground && git checkout jest-bug-report
yarn install
yarn test
Expected behavior
Expecting the assertion in the following code to be true:
Actual behavior
Assertion returns false:
Additional context
The issue seems similar to the following ones:
Environment
The text was updated successfully, but these errors were encountered: