How can I use my webpack's html-loader imports in Jest tests?

后端 未结 3 589
一生所求
一生所求 2020-12-16 01:18

I am just getting started with the Jest test framework and while straight up unit tests work fine, I am having massive issues testing any component that in its module (ES mo

3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-16 01:59

    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$": "/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);
        }
    }
    

提交回复
热议问题