muk-require 1.1.1 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -5
- package/dist/index.d.ts +13 -0
- package/dist/index.js +82 -0
- package/dist/index.js.map +1 -0
- package/package.json +19 -11
- package/lib/index.js +0 -51
package/README.md
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
# muk-require
|
|
2
2
|
|
|
3
|
-
[](https://www.npmjs.com/package/muk-require)
|
|
4
|
+
[](https://github.com/fent/node-muk-require/actions/workflows/nodejs.yml)
|
|
5
5
|
[](https://codecov.io/gh/fent/node-muk-require)
|
|
6
6
|
|
|
7
7
|

|
|
8
8
|
|
|
9
9
|
# Usage
|
|
10
10
|
|
|
11
|
-
Mock dependencies.
|
|
11
|
+
Mock CommonJS dependencies. Note that this module only mocks `require()` calls in CommonJS modules, not ES module `import`s.
|
|
12
12
|
|
|
13
13
|
**foo.js**
|
|
14
14
|
```js
|
|
@@ -21,11 +21,16 @@ module.exports = (url) => {
|
|
|
21
21
|
|
|
22
22
|
**test.js**
|
|
23
23
|
```js
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
const request = require('request')
|
|
25
|
+
|
|
26
|
+
const mockedRequest = (url, options, callback) => {
|
|
27
|
+
// Log all requests.
|
|
28
|
+
console.log('Request made:', url);
|
|
29
|
+
request(url, options, callback)
|
|
26
30
|
};
|
|
27
31
|
|
|
28
32
|
const foo = muk('./foo', {
|
|
33
|
+
// Will overwrite all requires of "request" with our own version.
|
|
29
34
|
request: mockedRequest
|
|
30
35
|
});
|
|
31
36
|
```
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mocks a call to `require()`
|
|
3
|
+
*/
|
|
4
|
+
declare function mukRequire<T = unknown>(filename: string, deps: Record<string, unknown>): T;
|
|
5
|
+
declare namespace mukRequire {
|
|
6
|
+
type MukRequire = typeof mukRequire;
|
|
7
|
+
}
|
|
8
|
+
type MukRequireWithStatic = typeof mukRequire & {
|
|
9
|
+
mukRequire: typeof mukRequire;
|
|
10
|
+
default: typeof mukRequire;
|
|
11
|
+
};
|
|
12
|
+
declare const exportedMukRequire: MukRequireWithStatic;
|
|
13
|
+
export = exportedMukRequire;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
const node_module_1 = __importDefault(require("node:module"));
|
|
6
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
7
|
+
// This module uses a few privately defined methods inside of the Module module.
|
|
8
|
+
// The types for the module don't contain these non-public methods, so we define them here
|
|
9
|
+
// and use `@ts-expect-error`.
|
|
10
|
+
//
|
|
11
|
+
// Note that this API could go away one day :(
|
|
12
|
+
function resolveFilename(filename, parent) {
|
|
13
|
+
// @ts-expect-error
|
|
14
|
+
return node_module_1.default._resolveFilename(filename, parent);
|
|
15
|
+
}
|
|
16
|
+
function nodeModulePaths(pathname) {
|
|
17
|
+
// @ts-expect-error
|
|
18
|
+
return node_module_1.default._nodeModulePaths(pathname);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Mocks a call to `require()`
|
|
22
|
+
*/
|
|
23
|
+
function mukRequire(filename, deps) {
|
|
24
|
+
filename = resolveFilename(filename, module.parent);
|
|
25
|
+
// @ts-expect-error
|
|
26
|
+
const m = new node_module_1.default(filename, module.parent);
|
|
27
|
+
require.cache[filename] = m;
|
|
28
|
+
m.filename = filename;
|
|
29
|
+
m.paths = nodeModulePaths(node_path_1.default.dirname(filename));
|
|
30
|
+
// Load children.
|
|
31
|
+
const previousCache = {};
|
|
32
|
+
const children = {};
|
|
33
|
+
for (const key in deps) {
|
|
34
|
+
const childpath = resolveFilename(key, m);
|
|
35
|
+
previousCache[childpath] = require.cache[childpath];
|
|
36
|
+
const child = new node_module_1.default(childpath, m);
|
|
37
|
+
children[childpath] = child;
|
|
38
|
+
require.cache[childpath] = child;
|
|
39
|
+
child.paths = nodeModulePaths(node_path_1.default.dirname(childpath));
|
|
40
|
+
child.loaded = true;
|
|
41
|
+
child.exports = deps[key];
|
|
42
|
+
}
|
|
43
|
+
m.require = (path) => {
|
|
44
|
+
const childpath = resolveFilename(path, m);
|
|
45
|
+
const child = children[childpath];
|
|
46
|
+
if (child) {
|
|
47
|
+
return child.exports;
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
// @ts-expect-error
|
|
51
|
+
return node_module_1.default._load(path, m);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
// Load module.
|
|
55
|
+
// @ts-expect-error
|
|
56
|
+
m.load(filename);
|
|
57
|
+
// Clear so mocked modules can be required normally.
|
|
58
|
+
delete require.cache[filename];
|
|
59
|
+
for (const key in deps) {
|
|
60
|
+
const childpath = resolveFilename(key, m);
|
|
61
|
+
if (previousCache[childpath]) {
|
|
62
|
+
require.cache[childpath] = previousCache[childpath];
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
delete require.cache[childpath];
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return m.exports;
|
|
69
|
+
}
|
|
70
|
+
const exportedMukRequire = mukRequire;
|
|
71
|
+
exportedMukRequire.mukRequire = exportedMukRequire;
|
|
72
|
+
exportedMukRequire.default = exportedMukRequire;
|
|
73
|
+
// Statically recognizable exports for Node.js cjs-module-lexer (ESM named import support)
|
|
74
|
+
module.exports = exportedMukRequire;
|
|
75
|
+
module.exports.default = exportedMukRequire;
|
|
76
|
+
module.exports.mukRequire = exportedMukRequire;
|
|
77
|
+
// Delete this module from the cache so that the next time it gets
|
|
78
|
+
// require()'d it will be aware of the new parent
|
|
79
|
+
// in case it gets require()'d from a different directory
|
|
80
|
+
delete require.cache[require.resolve(__filename)];
|
|
81
|
+
module.exports = exportedMukRequ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CAAC;IAED,OAAO,CAAC,CAAC,OAAO,CAAC;AACnB,CAAC;AAWD,MAAM,kBAAkB,GAAG,UAAkC,CAAC;AAC9D,kBAAkB,CAAC,UAAU,GAAG,kBAAkB,CAAC;AACnD,kBAAkB,CAAC,OAAO,GAAG,kBAAkB,CAAC;AAEhD,0FAA0F;AAC1F,MAAM,CAAC,OAAO,GAAG,kBAAkB,CAAC;AACpC,MAAM,CAAC,OAAO,CAAC,OAAO,GAAG,kBAAkB,CAAC;AAC5C,MAAM,CAAC,OAAO,CAAC,UAAU,GAAG,kBAAkB,CAAC;AAI/C,kEAAkE;AAClE,iDAAiD;AACjD,yDAAyD;AACzD,OAAO,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC;AALlD,iBAAS,kBAAkB,CAAC"}
|
package/package.json
CHANGED
|
@@ -6,28 +6,36 @@
|
|
|
6
6
|
"mock",
|
|
7
7
|
"dependency"
|
|
8
8
|
],
|
|
9
|
-
"version": "1.
|
|
9
|
+
"version": "1.2.1",
|
|
10
10
|
"repository": {
|
|
11
11
|
"type": "git",
|
|
12
12
|
"url": "git://github.com/fent/node-muk-require.git"
|
|
13
13
|
},
|
|
14
|
-
"author": "fent (https://github.com/fent)",
|
|
15
|
-
"main": "./
|
|
14
|
+
"author": "fent <fentbox@gmail.com> (https://github.com/fent)",
|
|
15
|
+
"main": "./dist/index.js",
|
|
16
16
|
"scripts": {
|
|
17
|
-
"
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
"
|
|
17
|
+
"prepare": "tsc -p tsconfig.json",
|
|
18
|
+
"build": "tsc -p tsconfig.json",
|
|
19
|
+
"lint": "biome check .",
|
|
20
|
+
"lint:fix": "biome check --write .",
|
|
21
|
+
"test": "nyc --extension .ts --reporter=lcov --reporter=text-summary npm run test:unit && npm run test:package",
|
|
22
|
+
"test:unit": "mocha --require ts-node/register --ignore test/export-test.ts test/*-test.ts",
|
|
23
|
+
"test:package": "mocha --require ts-node/register test/export-test.ts"
|
|
21
24
|
},
|
|
22
25
|
"devDependencies": {
|
|
23
|
-
"
|
|
24
|
-
"mocha": "^
|
|
26
|
+
"@biomejs/biome": "^2.5.15",
|
|
27
|
+
"@types/mocha": "^10.0.10",
|
|
28
|
+
"@types/node": "^20.1.2",
|
|
29
|
+
"mocha": "^12.0.3",
|
|
30
|
+
"nyc": "^18.0.0",
|
|
31
|
+
"ts-node": "^10.9.2",
|
|
32
|
+
"typescript": "^5.6.3"
|
|
25
33
|
},
|
|
26
34
|
"engines": {
|
|
27
|
-
"node": ">=
|
|
35
|
+
"node": ">=20"
|
|
28
36
|
},
|
|
29
37
|
"license": "MIT",
|
|
30
38
|
"files": [
|
|
31
|
-
"
|
|
39
|
+
"dist"
|
|
32
40
|
]
|
|
33
41
|
}
|
package/lib/index.js
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const Module = require('module');
|
|
4
|
-
const path = require('path');
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Mocks a call to `require()`
|
|
9
|
-
*
|
|
10
|
-
* @param {string} filename
|
|
11
|
-
* @param {Object} deps
|
|
12
|
-
*/
|
|
13
|
-
module.exports = (filename, deps) => {
|
|
14
|
-
filename = Module._resolveFilename(filename, module.parent);
|
|
15
|
-
var m = require.cache[filename] = new Module(filename, module.parent);
|
|
16
|
-
m.filename = filename;
|
|
17
|
-
m.paths = Module._nodeModulePaths(path.dirname(filename));
|
|
18
|
-
|
|
19
|
-
// Load children.
|
|
20
|
-
var children = {};
|
|
21
|
-
Object.keys(deps).forEach((key) => {
|
|
22
|
-
var childpath = Module._resolveFilename(key, m);
|
|
23
|
-
var child = children[childpath] = new Module(childpath, m);
|
|
24
|
-
child.paths = Module._nodeModulePaths(path.dirname(childpath));
|
|
25
|
-
child.loaded = true;
|
|
26
|
-
child.exports = deps[key];
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
m.require = (path) => {
|
|
30
|
-
var childpath = Module._resolveFilename(path, m);
|
|
31
|
-
var child = children[childpath];
|
|
32
|
-
if (child) {
|
|
33
|
-
return child.exports;
|
|
34
|
-
} else {
|
|
35
|
-
return Module._load(path, m);
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
// Load module.
|
|
40
|
-
m.load(filename);
|
|
41
|
-
|
|
42
|
-
// Delete module from cache so it can be required normally.
|
|
43
|
-
delete require.cache[filename];
|
|
44
|
-
|
|
45
|
-
return m.exports;
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
// Delete this module from the cache so that the next time it gets
|
|
49
|
-
// require()'d it will be aware of the new parent
|
|
50
|
-
// in case it gets require()'d from a different directory
|
|
51
|
-
delete require.cache[require.resolve(__filename)];
|