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

Transform Async #5556

Closed
Cretezy opened this issue Feb 13, 2018 · 5 comments
Closed

Transform Async #5556

Cretezy opened this issue Feb 13, 2018 · 5 comments

Comments

@Cretezy
Copy link

Cretezy commented Feb 13, 2018

Do you want to request a feature or report a bug?

Feature.

What is the current behavior?

Transforms are synchronous, basically process returns a string of the transformed source.

What is the expected behavior?

It would be useful to also support Promises, for asynchronous transformation.

@cpojer
Copy link
Member

cpojer commented Feb 14, 2018

Unfortunately Jest expects transformers to be synchronous and we cannot change it at this time because we transform just-in-time when user code calls require.

@cpojer cpojer closed this as completed Feb 14, 2018
@Cretezy
Copy link
Author

Cretezy commented Feb 16, 2018

Make sense, thank you!

@jpvincent
Copy link

Hi
I understand the rationales for the "No", but let me add that here we would really need the transform to support asynchronous methods, like Webpack does with its loader. We made a custom loader (for translations files in resx format) that works for Webpack and that we could reuse almost as-is if the jest transform option was asynchronous.
Currently the workaround has been to pre-compile all our translation files before running Jest, but it's not optimized at all.

Thanks anyway for the transform option to exist at all :)

@ksjogo
Copy link

ksjogo commented Oct 11, 2018

@jpvincent
I ran into the same thing with a resx transformer.
This seems to work:

const xml2js = require('xml2js');
const util = require('./util');
const deasync = require('deasync');

/** @type { jest.Transformer } */
const transformer = {
  process(src) {
    let result = null;
    xml2js.parseString(src, (err, parsedXml) => {
      if (err) throw err;
      result = util.transformResx(parsedXml);
    });
    while (result === undefined) {
      deasync.runLoopOnce();
    }
    return result;
  }
};

module.exports = transformer;

@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.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants