Parsing of JSON imports #58
Comments
I have made a little investigation and find out this: All JSON files are imported with JSON.parse from jest-runtime That is why It couldn't even be transformed to a string using jest/transform config option (as I did for image files in components), I have tried - but it ignores transform for json files. I have found a discussion about it on the forum of facebook\jest - And developers said that they won't change it even if it so not suitable for everyone. When I have faced with such a case at the components tests - I have used JSON.stringify for all json files - it will have the same result as for strings or object. Hope, my information will be usefull. |
Irina, thank you for your information! A comment by Christopher states clearly that it is done to emulate node.js behavior and it won't change anytime soon most likely. Simon offers a patch for Jest runtime, but I'm not sure it's worth it for us. So I would offer for discussion the following question: I don's see any use cases where we would need to have JSON imports unparsed and parse them manually. Do you? Do you think this change, if made globally to all libraries, will simplify things for dev and especially for testing? |
As for me - I didn't see all the code - but I didn't face with cases that needs manual parsing. |
Also I have started to create tests for inflection tables and as for paradigm tables - it uses JSON.parse in verbParadigmTables and verbParticipleParadigmTables - and it couldn't be executed with Jest as it imported json as object. So I couldn't solve it with trandform and with direct JSON.stringify - only way is synchronize the way of handling json in production and test environments. |
Sorry for chiming in late here, but I'd like to see us handle the imports consistent across test and production environments. So if changing Webpack is the simplest way to do that, then I'm in favor of it. |
We can update all libraries at once, but we can also do it one by one. Here is what I think would work: The current version of So those libraries that are not updated (i.e. still parses JSON manually) should update their |
yes I agree with that suggestion. |
That all should be in master now |
I believe it could be closed. |
Since
inflection-tables
tests are moved to usebabel-jest
, there is an issue with parsing of JSON imports. Tests are executed in node.js environment, and statements likeimport file from 'file.json'
results infile
being in object.However, mostly due to historical reasons (since Rollup which we used before did not parse JSON files automatically), we import JSON files as strings and parse them manually with
JSON.parse()
.That creates problems with testing, since in tests we have imports as objects (since it is parsed automatically by node.js), but our code expects it to be a string (as during "normal" execution in browser environment). I did not find any way to disable automatic parsing of JSON imports by node.js (if you know any, please let me know!).
So it seems that the only solution is to enable automatic parsing of JSON imports by Webpack. This way it will match node.js behavior during testing and the problem will go away.
What do you think? Do you see any other ways to solve this?
The text was updated successfully, but these errors were encountered: