Skip to content
Advertisement

Using jest with files with glob imports

I utilize webpack with webpack-import-glob-loader to import files using glob pattern. So in one of my files (src/store/resources/module.ts) I have a line:

JavaScript

When I run a test with ts-jest it fails and reports the following message:

Cannot find module ‘../../modules/resources/providers/**/*.resource.ts’ from ‘src/store/resources/module.ts`

I assume it complains because it can’t recognize this import syntax.

How to make jest work for project with glob imports?

Advertisement

Answer

I solved this by manually handling the globs inside of a jest preprocessor. Since you need to control processing of the files to handle globs in this approach, you have to manually initialize your processor.

JavaScript

In this approach you can only glob once per file (because it uses the .map indexes to add numbers to each import module name). You could keep track of a global count variable instead, if you wanted to have several glob imports in one file.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement