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 · 10 comments

Comments

Projects
None yet
6 participants
@avaly
Copy link
Contributor

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

This comment has been minimized.

Copy link
Contributor

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 Feb 25, 2017

@okonet

This comment has been minimized.

Copy link
Contributor

commented Feb 25, 2017

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

@cpojer

This comment has been minimized.

Copy link
Contributor

commented Feb 25, 2017

That is true.

@okonet

This comment has been minimized.

Copy link
Contributor

commented Feb 25, 2017

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

@cpojer

This comment has been minimized.

Copy link
Contributor

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

This comment has been minimized.

Copy link
Contributor Author

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

This comment has been minimized.

Copy link

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

@SimenB

This comment has been minimized.

Copy link
Collaborator

commented Dec 27, 2017

It's here:

if (path.extname(modulePath) === '.json') {
localModule.exports = this._environment.global.JSON.parse(
stripBOM(fs.readFileSync(modulePath, 'utf8')),
);

@SimenB SimenB added the Help Wanted label Dec 27, 2017

@marcialca

This comment has been minimized.

Copy link

commented Dec 27, 2017

Thanks!

@MLoughry

This comment has been minimized.

Copy link
Contributor

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 added a commit that referenced this issue Apr 11, 2019

Allow JSON transforms (#8278)
* 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.