javascripttestingjestjsraw-data

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?


Solution

  • 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