Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ignore file extension with Jest

I'm using webpack resolve.extensions to "selectively" bundle my js files. eg.

App.js

import MyComp from 'comp/myComp';

in comp/ folder I have:

MyComp.web.js
MyComp.ios.js
MyComp.android.js

Now I want to write test for App.js, to test how it render as web. But the problem is Jest keep resolving all 3 .js files and that causes dependency error because I'm not running the command in a mobile environment and most mobile modules will not work.

how can I tell jest to only resolve .js and .web.js files just like webpack?

I added this to my package.json and it keep resolving .ios.js and .android.js

"moduleFileExtensions": ["web.js", "js", "jsx"],
    "moduleDirectories": [
      "node_modules",
      "main"]

I tried as suggested with:

"testPathIgnorePatterns": ["<rootDir>/node_modules/", "^.+\\.(android|ios)\\.js$"],

looks no effects :(

like image 517
Yichaoz Avatar asked Mar 10 '23 19:03

Yichaoz


2 Answers

You can also remap files to an empty file

touch empty.js

In your jest config add following

  moduleNameMapper: {
    '\\.(css|jpg|png|scss|less|sass)$': '<rootDir>/empty.js',
  },

Works perfect for me

like image 176
Denis Rybalka Avatar answered Mar 24 '23 04:03

Denis Rybalka


You can add testPathIgnorePatterns to your package.json file. The value is an array of regex patterns. Any file/path that matches an expression will be ignored/skipped from testing.

See these Jest docs for more detail

like image 44
Jordan Bonitatis Avatar answered Mar 24 '23 06:03

Jordan Bonitatis