How to ignore test files in webpack with ts-loader

后端 未结 3 757
执念已碎
执念已碎 2021-02-13 19:46

I have a project that runs the test files named \"*.test.ts\" with jest and ts-jest. This is fine but when I launch webpack, I get errors for the test files:

ERR         


        
相关标签:
3条回答
  • 2021-02-13 20:05

    Found it. My version of 'ts-loader' was behind and somehow did not manage to properly handle installed @types.

    Rule of thumb: pack, build, transpile issues: upgrade dev dependencies.

    0 讨论(0)
  • 2021-02-13 20:11

    I ended up resolving this too, but in a different way, namely, adding the following settings to my .tsconfig file:

    "include": [
        "./lib/**/*"
    ],
    "exclude": [
        "./lib/__tests__"
    ]
    

    Not sure why this didn't come up for the OP and company. Perhaps they already had those settings in their .tslint file (e.g. because their project had been generated or forked from another project).

    But it looks like TypeScript (at least, as of 2.3.2) pays attention to these settings and ignores equivalent settings in webconfig.config.js.

    0 讨论(0)
  • 2021-02-13 20:13

    You can specify a function instead of the regular expression for the loader test property; this can give you a more control.

    test: function (modulePath) {
      return modulePath.endsWith('.ts') && !modulePath.endsWith('test.ts');
    }
    
    0 讨论(0)
提交回复
热议问题