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

Import fails with Jest 28 #249

Open
korompaiistvan opened this issue Jun 13, 2022 · 4 comments
Open

Import fails with Jest 28 #249

korompaiistvan opened this issue Jun 13, 2022 · 4 comments

Comments

@korompaiistvan
Copy link

It seems Jest 28 introduced a breaking change that renders this module unusable with that jest version.
Trying to import a string from a text file gives the following error:

  ● Test suite failed to run

    ● Invalid return value:
      `process()` or/and `processAsync()` method of code transformer found at 
      "/workspaces/lynxfortableau/node_modules/jest-raw-loader/index.js" 
      should return an object or a Promise resolving to an object. The object 
      must have `code` property with a string of processed code.
      This error may be caused by a breaking change in Jest 28:
      https://jestjs.io/docs/upgrading-to-jest28#transformer
      Code Transformation Documentation:
      https://jestjs.io/docs/code-transformation

The problem is solved if I patch the contents of node_modules/jest/index.js from:

module.exports = {
  process: content => "module.exports = " + JSON.stringify(content)
};

To:

module.exports = {
  process: content => { return { code: "module.exports = " + JSON.stringify(content)}}
};

I think this change would be backwards compatible with earlier version of jest until at least v21 but I did not test this in detail, only looked at the type definitions from the v21.0 release.

May I submit a pull request for this change?

@glensc
Copy link

glensc commented Sep 22, 2022

Still don't understand why not release 2.0.0 of jest-raw-loader, as copying own node_modules/jest-raw-loader/index.js to every project updating to jest is tedious and annoying and why was this package created in the first place then? to solve copy-paste distribution model.

@sneko
Copy link

sneko commented Jan 27, 2023

Just for people looking for the solution, @glensc made a forked release: https://github.com/glensc/jest-raw-loader

Thank you ;)

(still, no reason to blame @keplersj if he has no more time on it)

@Troy-Yang
Copy link

Troy-Yang commented May 23, 2023

we can create a custom fileTransfomer and use it

// fileTransformer.js
const path = require('path');

module.exports = {
  process(sourceText, sourcePath, options) {
    return {
      code: `module.exports = ${JSON.stringify(path.basename(sourcePath))};`,
    };
  },
};
module.exports = {
  transform: {
    '\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$':
      '<rootDir>/fileTransformer.js',
  },
};

@ThrawnCA
Copy link

we can create a custom fileTransfomer and use it

Thanks, that seems to be working for our project. But why not use the simpler syntax suggested by @korompaiistvan ?

module.exports = {
  process: content => { return { code: "module.exports = " + JSON.stringify(content)}}
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants