3

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?

Teneff
  • 30,564
  • 13
  • 72
  • 103
Jose A. Ayllón
  • 866
  • 6
  • 19

1 Answers1

3

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

Teneff
  • 30,564
  • 13
  • 72
  • 103