@@ -1735,6 +1735,25 @@ added:
1735
1735
Resets the implementation of the mock function to its original behavior. The
1736
1736
mock can still be used after calling this function.
1737
1737
1738
+ ## Class: ` MockModuleContext `
1739
+
1740
+ <!-- YAML
1741
+ added: REPLACEME
1742
+ -->
1743
+
1744
+ > Stability: 1.0 - Early development
1745
+
1746
+ The ` MockModuleContext ` class is used to manipulate the behavior of module mocks
1747
+ created via the [ ` MockTracker ` ] [ ] APIs.
1748
+
1749
+ ### ` ctx.restore() `
1750
+
1751
+ <!-- YAML
1752
+ added: REPLACEME
1753
+ -->
1754
+
1755
+ Resets the implementation of the mock module.
1756
+
1738
1757
## Class: ` MockTracker `
1739
1758
1740
1759
<!-- YAML
@@ -1868,6 +1887,68 @@ test('spies on an object method', (t) => {
1868
1887
});
1869
1888
```
1870
1889
1890
+ ### ` mock.module(specifier[, options]) `
1891
+
1892
+ <!-- YAML
1893
+ added: REPLACEME
1894
+ -->
1895
+
1896
+ > Stability: 1.0 - Early development
1897
+
1898
+ * ` specifier ` {string} A string identifying the module to mock.
1899
+ * ` options ` {Object} Optional configuration options for the mock module. The
1900
+ following properties are supported:
1901
+ * ` cache ` {boolean} If ` false ` , each call to ` require() ` or ` import() `
1902
+ generates a new mock module. If ` true ` , subsequent calls will return the same
1903
+ module mock, and the mock module is inserted into the CommonJS cache.
1904
+ ** Default:** false.
1905
+ * ` defaultExport ` {any} An optional value used as the mocked module's default
1906
+ export. If this value is not provided, ESM mocks do not include a default
1907
+ export. If the mock is a CommonJS or builtin module, this setting is used as
1908
+ the value of ` module.exports ` . If this value is not provided, CJS and builtin
1909
+ mocks use an empty object as the value of ` module.exports ` .
1910
+ * ` namedExports ` {Object} An optional object whose keys and values are used to
1911
+ create the named exports of the mock module. If the mock is a CommonJS or
1912
+ builtin module, these values are copied onto ` module.exports ` . Therefore, if a
1913
+ mock is created with both named exports and a non-object default export, the
1914
+ mock will throw an exception when used as a CJS or builtin module.
1915
+ * Returns: {MockModuleContext} An object that can be used to manipulate the mock.
1916
+
1917
+ This function is used to mock the exports of ECMAScript modules, CommonJS
1918
+ modules, and Node.js builtin modules. Any references to the original module
1919
+ prior to mocking are not impacted. The following example demonstrates how a mock
1920
+ is created for a module.
1921
+
1922
+ ``` js
1923
+ test (' mocks a builtin module in both module systems' , async (t ) => {
1924
+ // Create a mock of 'node:readline' with a named export named 'fn', which
1925
+ // does not exist in the original 'node:readline' module.
1926
+ const mock = t .mock .module (' node:readline' , {
1927
+ namedExports: { fn () { return 42 ; } },
1928
+ });
1929
+
1930
+ let esmImpl = await import (' node:readline' );
1931
+ let cjsImpl = require (' node:readline' );
1932
+
1933
+ // cursorTo() is an export of the original 'node:readline' module.
1934
+ assert .strictEqual (esmImpl .cursorTo , undefined );
1935
+ assert .strictEqual (cjsImpl .cursorTo , undefined );
1936
+ assert .strictEqual (esmImpl .fn (), 42 );
1937
+ assert .strictEqual (cjsImpl .fn (), 42 );
1938
+
1939
+ mock .restore ();
1940
+
1941
+ // The mock is restored, so the original builtin module is returned.
1942
+ esmImpl = await import (' node:readline' );
1943
+ cjsImpl = require (' node:readline' );
1944
+
1945
+ assert .strictEqual (typeof esmImpl .cursorTo , ' function' );
1946
+ assert .strictEqual (typeof cjsImpl .cursorTo , ' function' );
1947
+ assert .strictEqual (esmImpl .fn , undefined );
1948
+ assert .strictEqual (cjsImpl .fn , undefined );
1949
+ });
1950
+ ```
1951
+
1871
1952
### ` mock.reset() `
1872
1953
1873
1954
<!-- YAML
0 commit comments