Skip to content
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

JSON files are not transformed via custom preprocessor #2578

Closed
avaly opened this issue Jan 12, 2017 · 11 comments · Fixed by #8278
Closed

JSON files are not transformed via custom preprocessor #2578

avaly opened this issue Jan 12, 2017 · 11 comments · Fixed by #8278

Comments

@avaly
Copy link
Contributor

avaly commented Jan 12, 2017

Report a bug

Jest is ignoring JSON files for applying custom preprocessors via the transform config.

What is the current behavior?

A JSON file source is never sent to a custom preprocessor configured to transform its source. Other files (e.g. XML) are sent properly to the preprocessor.

Reproduceable with the files from: https://gist.github.com/avaly/3ff041228a5c6f789119170c28a80367

What is the expected behavior?

The JSON file source should be sent to the preprocessor.

Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.

  • jest v18.1.0
  • node v6.9.4
  • yarn v0.18.1
  • Ubuntu 16.04
@cpojer
Copy link
Member

cpojer commented Feb 25, 2017

This is by design. We do exactly what node does here and that is requiring JSON files by stripping BOM and calling JSON.parse on it.

@cpojer cpojer closed this as completed Feb 25, 2017
@okonet
Copy link
Contributor

okonet commented Feb 25, 2017

But in this case it's not possible to implement JSON transforms at all, right?

@cpojer
Copy link
Member

cpojer commented Feb 25, 2017

That is true.

@okonet
Copy link
Contributor

okonet commented Feb 25, 2017

Is this also by design? why are JSON files treated differently as any others?

@cpojer
Copy link
Member

cpojer commented Feb 25, 2017

Mainly because there haven't been any use cases to modify JSON files. I'm not opposed to it, just saying it is not a use case that benefits us at Facebook so we'll relying on the community to fix this :)

@avaly
Copy link
Contributor Author

avaly commented Feb 27, 2017

@cpojer This would be one example of issues of running Jest instead of running tests in a browser environment, where we can apply webpack's raw-loader or any other specific loader to JSON files and transform them. This basically forces people to rename their JSON fixtures to some other extension, in order to bypass this limitation when using Jest.

@marcialca
Copy link

marcialca commented Dec 27, 2017

Hi, We just got hit by this issue while trying to migrate to Jest. Is there any patch or fix posible?

How would we about to make a fix/change to allow transform for JSON files?

Can we know which part of the code handle this default so we can take a look and try to fix or amplify the behavior

@marcialca
Copy link

Thanks!

@MLoughry
Copy link
Contributor

MLoughry commented Apr 3, 2019

@cpojer Is this still the intended design, that JSON files cannot be transformed?

I'm working on an i18n solution for me team that involves a webpack plugin that transforms the localized string JSON (and extracts the translations on a per-bundle basis). The transformed JSON is then passed to a function in the resulting web client code for localization to the appropriate locale.

Without the ability to implement a Jest transform, I'm going to have to deal with devs on my team who are confused why they can't mock the localization function in the expected manner.

MLoughry added a commit to MLoughry/jest that referenced this issue Apr 5, 2019
scotthovestadt pushed a commit that referenced this issue Apr 11, 2019
* Allow JSON transforms for #2578

* Additional comments

* A better fix, with test

* Fix linting error from CI runs

* Address comment from @scotthovestadt

* Re-implement per @scotthovestadt's guidance

* Update Changelog

* Provide full options to transformJson

* Fix lint error
@github-actions
Copy link

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.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants