muk-require 1.2.0 → 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 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,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 = exportedMukRequire;
82
+ //# 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,OAAO,CAAC,KAAK,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;IAC5B,CAAC,CAAC,QAAQ,GAAG,QAAQ,CAAC;IACtB,CAAC,CAAC,KAAK,GAAG,eAAe,CAAC,mBAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC;IAElD,iBAAiB;IACjB,MAAM,aAAa,GAAuC,EAAE,CAAC;IAC7D,MAAM,QAAQ,GAA2B,EAAE,CAAC;IAC5C,KAAK,MAAM,GAAG,IAAI,IAA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AAC;YAC7B,OAAO,CAAC,KAAK,CAAC,SAAS,CAAC,GAAG,aAAa,CAAC,SAAS,CAAC,CAAC;QACtD,CAAC;aAAM,CAAC;YACN,OAAO,OAAO,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;QAClC,CAAC;IACH,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.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": "./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)];