jest-raw-loader icon indicating copy to clipboard operation
jest-raw-loader copied to clipboard

Import fails with Jest 28

Open korompaiistvan opened this issue 2 years ago • 4 comments

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?

korompaiistvan avatar Jun 13 '22 12:06 korompaiistvan

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.

glensc avatar Sep 22 '22 11:09 glensc

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)

sneko avatar Jan 27 '23 17:01 sneko

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',
  },
};

Troy-Yang avatar May 23 '23 09:05 Troy-Yang

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)}}
};

ThrawnCA avatar Dec 29 '23 00:12 ThrawnCA