samedi 30 novembre 2019

Regular expression not working in Jest moduleNameMapper

I am trying to test a Vue CLI project with vue-test-utils and Jest. I am using some icons from vue-material-design-icons, but they are not getting transformed when I run Jest. This is the error that I get when I run Jest:

    /path/to/node_modules/vue-material-design-icons/Sitemap.vue:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest) 
    {<template functional>
     ^

    SyntaxError: Unexpected token <

      101 | <script>
      102 | import { mapGetters, mapActions } from "vuex";
    > 103 | import ControllerIcon from "vue-material-design-icons/Sitemap.vue";
          | ^
      104 | import SensorIcon from "vue-material-design-icons/AccessPoint.vue";
      105 | import AlphaBoostIcon from "vue-material-design-icons/Alpha.vue";
      106 | 

After reading the README on the jest-transform-stub GitHub page, I tried to stub out the vue-material-design-icons in my jest.config.js file with this configuration:

...
moduleNameMapper: {
  "/vue-material-design-icons\/[\w]+.vue/": "jest-transform-stub",
  "^@/(.*)$": "<rootDir>/src/$1"
},
...

...but it is not working. I have tested the regular expression in both https://regex101.com/ and https://www.regextester.com/ and the regular expression works as expected in both sites.

If I hard code the file path into the config, then it works:

...
moduleNameMapper: {
  "vue-material-design-icons/Sitemap.vue": "jest-transform-stub",
  "^@/(.*)$": "<rootDir>/src/$1"
},
...

Obviously I don't want to hard code the file path for every icon in the project, though.

Does anyone know why the regular expression is not working?

Thank you in advance!

Aucun commentaire:

Enregistrer un commentaire