Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does $1 do in jest config?

Tags:

webpack

jestjs

The tutorial for using jest with webpack mentions this syntax: $1, and I haven't been able to figure out what it does. I'm working on two projects with the same aliases, but one works with jest config:

"moduleNameMapper": {
  "^myModule(.*)$": "<rootDir>/src/components/react",
}

while one works with

"moduleNameMapper": {
  "^myModule(.*)$": "<rootDir>/src/components/react$1",
}

What does the $1 syntax do?

like image 370
Sam Avatar asked Aug 25 '17 19:08

Sam


1 Answers

Jest will take the key of the object, and wrap it with a RegExp object. so basically you are writing a regular expression string as the key. The $1..$9 in regular expressions are the capture groups captured for the match . A capture group is created by wrapping parenthesis around the pattern you want to "save".

 "^myModule(.*)$": "<rootDir>/src/components/react$1"

so if you have an import of myModule/SOMETHING it will be mapped to:

myModule/SOMETHING => <rootDir>/src/components/react/SOMETHING"
like image 53
Bamieh Avatar answered Oct 16 '22 15:10

Bamieh