Skip to content

Commit

Permalink
🤖 Merge PR DefinitelyTyped#69895 feat(node): Add mock module by @rozz…
Browse files Browse the repository at this point in the history
…illa

Signed-off-by: Roberto Bianchi <[email protected]>
  • Loading branch information
rozzilla authored Jul 5, 2024
1 parent 3c78f0f commit 2259615
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
11 changes: 11 additions & 0 deletions types/node/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,17 @@ declare module "node:test" {
* @since v19.1.0, v18.13.0
*/
restoreAll(): void;
/**
* This function is used to mock the exports of ECMAScript modules,
* CommonJS modules, and Node.js builtin modules. Any references to
* the original module prior to mocking are not impacted
* @since v22.3.0
*/
module(speficier: string, options?: {
cache?: boolean;
defaultExport?: any;
namedExports?: object;
}): { restore: () => void };
timers: MockTimers;
}
const mock: MockTracker;
Expand Down
16 changes: 16 additions & 0 deletions types/node/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,22 @@ test("mocks a counting function", (t) => {
const fn = t.mock.fn(addOne, addTwo, { times: 2 });
// $ExpectType number
fn();

const mock = t.mock.module("node:readline", {
namedExports: {
fn() {
return 42;
},
},
defaultExport: {
foo() {
return "bar";
},
},
cache: true,
});
// $ExpectType void
mock.restore();
});

test("spies on an object method", (t) => {
Expand Down

0 comments on commit 2259615

Please sign in to comment.