Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test raw loaded data with React

I need to load javascript raw data from a .js file. I have this helper to do such thing:

import {
  reduceFunctionGarbage
} from "./reduce-function-garbage";

// eslint-disable-next-line import/no-webpack-loader-syntax
export const getRawChallenge = num =>
  reduceFunctionGarbage(require(`!raw-loader!../challenges/${num}.js`));

When I try to run the tests, I get the following error:

Code error

How can I test a file which imports RAW data? I've tried with jest-raw-loader (https://github.com/keplersj/jest-raw-loader) but it didn't work for me.

Anyone who could point me to the right direction?

like image 882
Jose A. Ayllón Avatar asked May 30 '26 01:05

Jose A. Ayllón


1 Answers

you can mock your files like this

jest.mock(`!raw-loader!../challenges/4.js`, () => 'hello-raw-data');

in case they don't exists you can provide { virtual: true }

jest.mock(`!raw-loader!../challenges/4.js`, () => 'hello-raw-data', {
  virtual: true,
});

working example

like image 143
Teneff Avatar answered May 31 '26 13:05

Teneff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!