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