Cannot run Jest tests for components (Failed to load html files)

See here for the original answer.

This answer helped me to solve this problem. As link-only answers are discouraged, below I have included the original answer by bnjmnhndrsn.

I encountered this specific problem recently and creating your own transform preprocesser will solve it. This was my set up:

package.json

"jest": {
  "moduleFileExtensions": [
    "js",
    "html"
  ],
  "transform": {
    "^.+\\.js$": "babel-jest",
    "^.+\\.html$": "<rootDir>/test/utils/htmlLoader.js"
  }
}

NOTE: babel-jest is normally included by default, but if you specify a custom transform preprocessor, you seem to have to include it manually.

test/utils/htmlLoader.js:

const htmlLoader = require("html-loader");

module.exports = {
  process(src, filename, config, options) {
    return htmlLoader(src);
  }
}

Tags

  1. angular (Private)
  2. jest (Private)
  3. testing (Private)
  4. stack-overflow (Private)
  5. answer (Private)