Skip to content

Commit dde8940

Browse files
author
Lukáš Horák
committed
✅ Mock require.resolve in plugins loading tests
1 parent d83e8d2 commit dde8940

File tree

5 files changed

+81
-15
lines changed

5 files changed

+81
-15
lines changed

jest.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ module.exports = {
2121
diagnostics: { warnOnly: true },
2222
},
2323
},
24+
resolver: "<rootDir>/test-utils/resolver",
2425
};

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@
2121
"update-gitmoji-config": "node ./scripts/update-gitmoji-config.js"
2222
},
2323
"devDependencies": {
24+
"@types/glob": "^8.0.1",
2425
"chai": "^4.2.0",
2526
"eslint": "^7.32.0",
2627
"eslint-config-oclif": "^4.0.0",
2728
"eslint-config-oclif-typescript": "^1.0.2",
2829
"eslint-config-prettier": "^8.3.0",
30+
"glob": "^8.1.0",
2931
"husky": "^4.3.0",
3032
"lint-staged": "^10.5.1",
3133
"prettier": "^2.1.1",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"@lokse/plugin1": "@lokse/plugin1",
3+
"@lokse/plugin2": "@lokse/plugin2",
4+
"@lokse/plugin3": "@lokse/plugin3"
5+
}

test-utils/resolver.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Custom resolver resolves ability to mock require.resolve
3+
* https://github.com/facebook/jest/issues/9543#issuecomment-616358056
4+
*/
5+
// eslint-disable-next-line node/no-unpublished-require
6+
const glob = require("glob");
7+
const path = require("path");
8+
9+
let mapping = {};
10+
11+
const globPattern = path.join(
12+
__dirname,
13+
"../packages/*/src/**/__tests__/modules-resolution.json"
14+
);
15+
16+
// Looks for "module-resolution.json" files in all the `__tests__` directories
17+
glob
18+
.sync(globPattern)
19+
// eslint-disable-next-line unicorn/no-array-for-each
20+
.forEach((file) => {
21+
// For each of them, merges them in the "mapping" object
22+
mapping = { ...mapping, ...require(file) };
23+
});
24+
25+
function resolver(path, options) {
26+
// console.log(path, mapping);
27+
// If the path corresponds to a key in the mapping object, returns the fakely resolved path
28+
// otherwise it calls the Jest's default resolver
29+
return mapping[path] || options.defaultResolver(path, options);
30+
}
31+
32+
module.exports = resolver;

yarn.lock

+41-15
Original file line numberDiff line numberDiff line change
@@ -2215,7 +2215,7 @@
22152215
resolved "https://registry.yarnpkg.com/@types/chai/-/chai-4.3.1.tgz#e2c6e73e0bdeb2521d00756d099218e9f5d90a04"
22162216
integrity sha512-/zPMqDkzSZ8t3VtxOa4KPq7uzzW978M9Tvh+j7GHKuo6k6GTLxPJ4J5gE5cjfJ26pnXst0N5Hax8Sr0T2Mi9zQ==
22172217

2218-
"@types/configstore@*":
2218+
"@types/configstore@*", "@types/configstore@5":
22192219
version "5.0.1"
22202220
resolved "https://registry.yarnpkg.com/@types/configstore/-/configstore-5.0.1.tgz#7be34d28ce29a408c98e717ada0488664eaf6173"
22212221
integrity sha512-c/QCznvk7bLKGhHETj29rqKufui3jaAxjBhK4R2zUrMG5UG0qTwfWYxBoUbH8JCyDjdCWMIxPJ7/Fdz1UcAnWg==
@@ -2264,6 +2264,14 @@
22642264
"@types/minimatch" "*"
22652265
"@types/node" "*"
22662266

2267+
"@types/glob@^8.0.1":
2268+
version "8.0.1"
2269+
resolved "https://registry.yarnpkg.com/@types/glob/-/glob-8.0.1.tgz#6e3041640148b7764adf21ce5c7138ad454725b0"
2270+
integrity sha512-8bVUjXZvJacUFkJXHdyZ9iH1Eaj5V7I8c4NdH5sQJsdXkqT4CA5Dhb4yb4VE/3asyx4L9ayZr1NIhTsWHczmMw==
2271+
dependencies:
2272+
"@types/minimatch" "^5.1.2"
2273+
"@types/node" "*"
2274+
22672275
"@types/google-spreadsheet@^3.0.1":
22682276
version "3.2.2"
22692277
resolved "https://registry.yarnpkg.com/@types/google-spreadsheet/-/google-spreadsheet-3.2.2.tgz#1d7a75982c89caa3ac38828d2f3268b5bc67e641"
@@ -2365,6 +2373,11 @@
23652373
resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.5.tgz#1001cc5e6a3704b83c236027e77f2f58ea010f40"
23662374
integrity sha512-Klz949h02Gz2uZCMGwDUSDS1YBlTdDDgbWHi+81l29tQALUtvz4rAYi5uoVhE5Lagoq6DeqAUlbrHvW/mXDgdQ==
23672375

2376+
"@types/minimatch@^5.1.2":
2377+
version "5.1.2"
2378+
resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-5.1.2.tgz#07508b45797cb81ec3f273011b054cd0755eddca"
2379+
integrity sha512-K0VQKziLUWkVKiRVrx4a40iPaxTUefQmjtkQofBkYRcoaaL/8rhwDWww9qWbrgicNOgnpIsMxyNIUM4+n6dUIA==
2380+
23682381
"@types/minimist@^1.2.0":
23692382
version "1.2.2"
23702383
resolved "https://registry.yarnpkg.com/@types/minimist/-/minimist-1.2.2.tgz#ee771e2ba4b3dc5b372935d549fd9617bf345b8c"
@@ -2424,7 +2437,7 @@
24242437
resolved "https://registry.yarnpkg.com/@types/parse5/-/parse5-6.0.3.tgz#705bb349e789efa06f43f128cef51240753424cb"
24252438
integrity sha512-SuT16Q1K51EAVPz1K29DJ/sXjhSQ0zjvsypYJ6tlwVsRV9jwW5Adq2ch8Dq8kDBCkYnELS7N7VNCSB5nC56t/g==
24262439

2427-
"@types/prettier@^2.0.0":
2440+
"@types/prettier@2.6.0", "@types/prettier@^2.0.0":
24282441
version "2.6.0"
24292442
resolved "https://registry.yarnpkg.com/@types/prettier/-/prettier-2.6.0.tgz#efcbd41937f9ae7434c714ab698604822d890759"
24302443
integrity sha512-G/AdOadiZhnJp0jXCaBQU449W2h716OW/EoXeYkCytxKL06X1WCXB4DZpp8TpZ8eyIJVS1cw4lrlkkSYU21cDw==
@@ -3236,6 +3249,13 @@ brace-expansion@^1.1.7:
32363249
balanced-match "^1.0.0"
32373250
concat-map "0.0.1"
32383251

3252+
brace-expansion@^2.0.1:
3253+
version "2.0.1"
3254+
resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-2.0.1.tgz#1edc459e0f0c548486ecf9fc99f2221364b9a0ae"
3255+
integrity sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA==
3256+
dependencies:
3257+
balanced-match "^1.0.0"
3258+
32393259
braces@^2.3.1:
32403260
version "2.3.2"
32413261
resolved "https://registry.yarnpkg.com/braces/-/braces-2.3.2.tgz#5979fd3f14cd531565e5fa2df1abfff1dfaee729"
@@ -5609,6 +5629,17 @@ glob@^7.1.1, glob@^7.1.2, glob@^7.1.3, glob@^7.1.4, glob@^7.1.6:
56095629
once "^1.3.0"
56105630
path-is-absolute "^1.0.0"
56115631

5632+
glob@^8.1.0:
5633+
version "8.1.0"
5634+
resolved "https://registry.yarnpkg.com/glob/-/glob-8.1.0.tgz#d388f656593ef708ee3e34640fdfb99a9fd1c33e"
5635+
integrity sha512-r8hpEjiQEYlF2QU0df3dS+nxxSIreXQS1qRhMJM0Q5NDdR386C7jb7Hwwod8Fgiuex+k0GFjgft18yvxm5XoCQ==
5636+
dependencies:
5637+
fs.realpath "^1.0.0"
5638+
inflight "^1.0.4"
5639+
inherits "2"
5640+
minimatch "^5.0.1"
5641+
once "^1.3.0"
5642+
56125643
global-dirs@^3.0.0:
56135644
version "3.0.0"
56145645
resolved "https://registry.yarnpkg.com/global-dirs/-/global-dirs-3.0.0.tgz#70a76fe84ea315ab37b1f5576cbde7d48ef72686"
@@ -8349,6 +8380,13 @@ minimatch@^3.0.4, minimatch@^3.1.1:
83498380
dependencies:
83508381
brace-expansion "^1.1.7"
83518382

8383+
minimatch@^5.0.1:
8384+
version "5.1.6"
8385+
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-5.1.6.tgz#1cfcb8cf5522ea69952cd2af95ae09477f122a96"
8386+
integrity sha512-lKwV/1brpG6mBUFHtb7NUmtABCb2WZZmm2wNiOA5hAb8VdCS4B3dtMWyvcoViccwAW/COERjXLt0zP1zXUN26g==
8387+
dependencies:
8388+
brace-expansion "^2.0.1"
8389+
83528390
83538391
version "4.1.0"
83548392
resolved "https://registry.yarnpkg.com/minimist-options/-/minimist-options-4.1.0.tgz#c0655713c53a8a2ebd77ffa247d342c40f010619"
@@ -10213,20 +10251,13 @@ run-parallel@^1.1.9:
1021310251
dependencies:
1021410252
queue-microtask "^1.2.2"
1021510253

10216-
rxjs@^6.6.0:
10254+
rxjs@6, rxjs@^6.6.0, rxjs@^7.2.0, rxjs@^7.5.1, rxjs@^7.5.5:
1021710255
version "6.6.7"
1021810256
resolved "https://registry.yarnpkg.com/rxjs/-/rxjs-6.6.7.tgz#90ac018acabf491bf65044235d5863c4dab804c9"
1021910257
integrity sha512-hTdwr+7yYNIT5n4AMYp85KA6yw2Va0FLa3Rguvbpa4W3I5xynaBZo41cM3XM+4Q6fRMj3sBYIR1VAmZMXYJvRQ==
1022010258
dependencies:
1022110259
tslib "^1.9.0"
1022210260

10223-
rxjs@^7.2.0, rxjs@^7.5.1, rxjs@^7.5.5:
10224-
version "7.8.0"
10225-
resolved "https://registry.yarnpkg.com/rxjs/-/rxjs-7.8.0.tgz#90a938862a82888ff4c7359811a595e14e1e09a4"
10226-
integrity sha512-F2+gxDshqmIub1KdvZkaEfGDwLNpPvk9Fs6LD/MyQxNgMds/WH9OdDDXOmxUZpME+iSK3rQCctkL0DYyytUqMg==
10227-
dependencies:
10228-
tslib "^2.1.0"
10229-
1023010261
1023110262
version "0.0.15"
1023210263
resolved "https://registry.yarnpkg.com/s.color/-/s.color-0.0.15.tgz#6b32cd22d8dba95703a5122ddede2020a1560186"
@@ -11250,11 +11281,6 @@ tslib@^2.0.0, tslib@^2.0.1, tslib@^2.0.3, tslib@^2.3.1, tslib@^2.4.0:
1125011281
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.4.0.tgz#7cecaa7f073ce680a05847aa77be941098f36dc3"
1125111282
integrity sha512-d6xOpEDfsi2CZVlPQzGeux8XMwLT9hssAsaPYExaQMuYskwb+x1x7J371tWlbBdWHroy99KnVB6qIkUbs5X3UQ==
1125211283

11253-
tslib@^2.1.0:
11254-
version "2.5.0"
11255-
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.5.0.tgz#42bfed86f5787aeb41d031866c8f402429e0fddf"
11256-
integrity sha512-336iVw3rtn2BUK7ORdIAHTyxHGRIHVReokCR3XjbckJMK7ms8FysBfhLR8IXnAgy7T0PTPNBWKiH514FOW/WSg==
11257-
1125811284
tsm@^2.1.4:
1125911285
version "2.2.2"
1126011286
resolved "https://registry.yarnpkg.com/tsm/-/tsm-2.2.2.tgz#0edacd62bbe53a87e8fc9c260ab65a7dbac5de06"

0 commit comments

Comments
 (0)