muk-require 1.2.0 → 1.2.2

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 CHANGED
@@ -1,14 +1,14 @@
1
1
  # muk-require
2
2
 
3
- [![Build Status](https://secure.travis-ci.org/fent/node-muk-require.svg)](http://travis-ci.org/fent/node-muk-require)
4
- [![Dependency Status](https://david-dm.org/fent/node-muk-require.svg)](https://david-dm.org/fent/node-muk-require)
3
+ [![npm](https://img.shields.io/npm/v/muk-require)](https://www.npmjs.com/package/muk-require)
4
+ [![Node.js CI](https://github.com/fent/node-muk-require/actions/workflows/nodejs.yml/badge.svg)](https://github.com/fent/node-muk-require/actions/workflows/nodejs.yml)
5
5
  [![codecov](https://codecov.io/gh/fent/node-muk-require/branch/master/graph/badge.svg)](https://codecov.io/gh/fent/node-muk-require)
6
6
 
7
7
  ![muk](muk.gif)
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
@@ -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,86 @@
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
+ m.filename = filename;
28
+ m.paths = nodeModulePaths(node_path_1.default.dirname(filename));
29
+ const previousCache = {};
30
+ const children = {};
31
+ const restore = () => {
32
+ delete require.cache[filename];
33
+ for (const childpath of Object.keys(previousCache)) {
34
+ if (previousCache[childpath]) {
35
+ require.cache[childpath] = previousCache[childpath];
36
+ }
37
+ else {
38
+ delete require.cache[childpath];
39
+ }
40
+ }
41
+ };
42
+ // Always clear mocks so failed resolutions or loads don't leak.
43
+ try {
44
+ require.cache[filename] = m;
45
+ for (const key in deps) {
46
+ const childpath = resolveFilename(key, m);
47
+ previousCache[childpath] = require.cache[childpath];
48
+ const child = new node_module_1.default(childpath, m);
49
+ children[childpath] = child;
50
+ require.cache[childpath] = child;
51
+ child.paths = nodeModulePaths(node_path_1.default.dirname(childpath));
52
+ child.loaded = true;
53
+ child.exports = deps[key];
54
+ }
55
+ m.require = (path) => {
56
+ const childpath = resolveFilename(path, m);
57
+ const child = children[childpath];
58
+ if (child) {
59
+ return child.exports;
60
+ }
61
+ else {
62
+ // @ts-expect-error
63
+ return node_module_1.default._load(path, m);
64
+ }
65
+ };
66
+ // @ts-expect-error
67
+ m.load(filename);
68
+ }
69
+ finally {
70
+ restore();
71
+ }
72
+ return m.exports;
73
+ }
74
+ const exportedMukRequire = mukRequire;
75
+ exportedMukRequire.mukRequire = exportedMukRequire;
76
+ exportedMukRequire.default = exportedMukRequire;
77
+ // Statically recognizable exports for Node.js cjs-module-lexer (ESM named import support)
78
+ module.exports = exportedMukRequire;
79
+ module.exports.default = exportedMukRequire;
80
+ module.exports.mukRequire = exportedMukRequire;
81
+ // Delete this module from the cache so that the next time it gets
82
+ // require()'d it will be aware of the new parent
83
+ // in case it gets require()'d from a different directory
84
+ delete require.cache[require.resolve(__filename)];
85
+ module.exports = exportedMukRequire;
86
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;AAAA,8DAAiC;AACjC,0DAA6B;AAE7B,gFAAgF;AAChF,0FAA0F;AAC1F,8BAA8B;AAC9B,EAAE;AACF,8CAA8C;AAC9C,SAAS,eAAe,CAAC,QAAgB,EAAE,MAAsB;IAC/D,mBAAmB;IACnB,OAAO,qBAAM,CAAC,gBAAgB,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;AACnD,CAAC;AAED,SAAS,eAAe,CAAC,QAAgB;IACvC,mBAAmB;IACnB,OAAO,qBAAM,CAAC,gBAAgB,CAAC,QAAQ,CAAC,CAAC;AAC3C,CAAC;AAED;;GAEG;AACH,SAAS,UAAU,CACjB,QAAgB,EAChB,IAA6B;IAE7B,QAAQ,GAAG,eAAe,CAAC,QAAQ,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC;IACpD,mBAAmB;IACnB,MAAM,CAAC,GAAG,IAAI,qBAAM,CAAC,QAAQ,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC;IAC9C,CAAC,CAAC,QAAQ,GAAG,QAAQ,CAAC;IACtB,CAAC,CAAC,KAAK,GAAG,eAAe,CAAC,mBAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC;IAElD,MAAM,aAAa,GAAuC,EAAE,CAAC;IAC7D,MAAM,QAAQ,GAA2B,EAAE,CAAC;IAC5C,MAAM,OAAO,GAAG,GAAG,EAAE;QACnB,OAAO,OAAO,CAAC,KAAK,CAAC,QA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V,OAAO,KAAK,CAAC,OAAO,CAAC;YACvB,CAAC;iBAAM,CAAC;gBACN,mBAAmB;gBACnB,OAAO,qBAAM,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC;YAC/B,CAAC;QACH,CAAC,CAAC;QAEF,mBAAmB;QACnB,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IACnB,CAAC;YAAS,CAAC;QACT,OAAO,EAAE,CAAC;IACZ,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.2.0",
9
+ "version": "1.2.2",
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": "./lib/index.js",
14
+ "author": "fent <fentbox@gmail.com> (https://github.com/fent)",
15
+ "main": "./dist/index.js",
16
16
  "scripts": {
17
- "test": "istanbul cover node_modules/.bin/_mocha -- -R spec test/*-test.js"
18
- },
19
- "directories": {
20
- "lib": "./lib"
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
- "istanbul": "^0.4.5",
24
- "mocha": "^5.0.0"
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": ">=6"
35
+ "node": ">=20"
28
36
  },
29
37
  "license": "MIT",
30
38
  "files": [
31
- "lib"
39
+ "dist"
32
40
  ]
33
41
  }
package/lib/index.js DELETED
@@ -1,54 +0,0 @@
1
- const Module = require('module');
2
- const path = require('path');
3
-
4
-
5
- /**
6
- * Mocks a call to `require()`
7
- *
8
- * @param {string} filename
9
- * @param {Object} deps
10
- */
11
- module.exports = (filename, deps) => {
12
- filename = Module._resolveFilename(filename, module.parent);
13
- const m = require.cache[filename] = new Module(filename, module.parent);
14
- m.filename = filename;
15
- m.paths = Module._nodeModulePaths(path.dirname(filename));
16
-
17
- // Load children.
18
- const children = {};
19
- for (let key in deps) {
20
- const childpath = Module._resolveFilename(key, m);
21
- const child = children[childpath] = new Module(childpath, m);
22
- require.cache[childpath] = child;
23
- child.paths = Module._nodeModulePaths(path.dirname(childpath));
24
- child.loaded = true;
25
- child.exports = deps[key];
26
- }
27
-
28
- m.require = (path) => {
29
- const childpath = Module._resolveFilename(path, m);
30
- const child = children[childpath];
31
- if (child) {
32
- return child.exports;
33
- } else {
34
- return Module._load(path, m);
35
- }
36
- };
37
-
38
- // Load module.
39
- m.load(filename);
40
-
41
- // Clear so mocked modules can be required normally.
42
- delete require.cache[filename];
43
- for (let key in deps) {
44
- let childpath = Module._resolveFilename(key, m);
45
- delete require.cache[childpath];
46
- }
47
-
48
- return m.exports;
49
- };
50
-
51
- // Delete this module from the cache so that the next time it gets
52
- // require()'d it will be aware of the new parent
53
- // in case it gets require()'d from a different directory
54
- delete require.cache[require.resolve(__filename)];