@@ -1763,6 +1763,23 @@ added:
1763
1763
Resets the implementation of the mock function to its original behavior. The
1764
1764
mock can still be used after calling this function.
1765
1765
1766
+ ## Class: ` MockModuleContext `
1767
+
1768
+ <!-- YAML
1769
+ added: REPLACEME
1770
+ -->
1771
+
1772
+ The ` MockModuleContext ` class is used to manipulate the behavior of module mocks
1773
+ created via the [ ` MockTracker ` ] [ ] APIs.
1774
+
1775
+ ### ` ctx.restore() `
1776
+
1777
+ <!-- YAML
1778
+ added: REPLACEME
1779
+ -->
1780
+
1781
+ Resets the implementation of the mock module.
1782
+
1766
1783
## Class: ` MockTracker `
1767
1784
1768
1785
<!-- YAML
@@ -1896,6 +1913,66 @@ test('spies on an object method', (t) => {
1896
1913
});
1897
1914
```
1898
1915
1916
+ ### ` mock.module(specifier[, options]) `
1917
+
1918
+ <!-- YAML
1919
+ added: REPLACEME
1920
+ -->
1921
+
1922
+ * ` specifier ` {string} A string identifying the module to mock.
1923
+ * ` options ` {Object} Optional configuration options for the mock module. The
1924
+ following properties are supported:
1925
+ * ` cache ` {boolean} If ` false ` , each call to ` require() ` or ` import() `
1926
+ generates a new mock module. If ` true ` , subsequent calls will return the same
1927
+ module mock, and the mock module is inserted into the CommonJS cache.
1928
+ ** Default:** false.
1929
+ * ` defaultExport ` {any} An optional value used as the mocked module's default
1930
+ export. If this value is not provided, ESM mocks do not include a default
1931
+ export. If the mock is a CommonJS or builtin module, this setting is used as
1932
+ the value of ` module.exports ` . If this value is not provided, CJS and builtin
1933
+ mocks use an empty object as the value of ` module.exports ` .
1934
+ * ` namedExports ` {Object} An optional object whose keys and values are used to
1935
+ create the named exports of the mock module. If the mock is a CommonJS or
1936
+ builtin module, these values are copied onto ` module.exports ` . Therefor, if a
1937
+ mock is created with both named exports and a non-object default export, the
1938
+ mock will create an exception when used as a CJS or builtin module.
1939
+ * Returns: {MockModuleContext} An object that can be used to manipulate the mock.
1940
+
1941
+ This function is used to mock the exports of ECMAScript modules, CommonJS
1942
+ modules, and Node.js builtin modules. Any references to the original module
1943
+ prior to mocking are not impacted. The following example demonstrates how a mock
1944
+ is created for a module.
1945
+
1946
+ ``` js
1947
+ test (' mocks a builtin module in both module systems' , async (t ) => {
1948
+ // Create a mock of 'node:readline' with a named export named 'fn', which
1949
+ // does not exist in the original 'node:readline' module.
1950
+ const mock = t .mock .module (' node:readline' , {
1951
+ namedExports: { fn () { return 42 ; } },
1952
+ });
1953
+
1954
+ let esmImpl = await import (' node:readline' );
1955
+ let cjsImpl = require (' node:readline' );
1956
+
1957
+ // cursorTo() is an export of the original 'node:readline' module.
1958
+ assert .strictEqual (esmImpl .cursorTo , undefined );
1959
+ assert .strictEqual (cjsImpl .cursorTo , undefined );
1960
+ assert .strictEqual (esmImpl .fn (), 42 );
1961
+ assert .strictEqual (cjsImpl .fn (), 42 );
1962
+
1963
+ mock .restore ();
1964
+
1965
+ // The mock is restored, so the original builtin module is returned.
1966
+ esmImpl = await import (' node:readline' );
1967
+ cjsImpl = require (' node:readline' );
1968
+
1969
+ assert .strictEqual (typeof esmImpl .cursorTo , ' function' );
1970
+ assert .strictEqual (typeof cjsImpl .cursorTo , ' function' );
1971
+ assert .strictEqual (esmImpl .fn , undefined );
1972
+ assert .strictEqual (cjsImpl .fn , undefined );
1973
+ });
1974
+ ```
1975
+
1899
1976
### ` mock.reset() `
1900
1977
1901
1978
<!-- YAML
0 commit comments