/
githubmirror
/
deno
Обзор
Документация
Войти
/
githubmirror
/
deno
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
tests/specs/node/node_test_mock_module/main_cache.mjs
39 строк
1 KB
Bartek Iwańczuk
feat(ext/node): implement node:test mock.module (#35329)
20 июн 2026, 22:23
Не верифицирован
20 июн 2026, 22:23
e38a472
Код
Авторство
О чём код?
// Copyright 2018-2026 the Deno authors. MIT license. // The `cache` option controls whether repeated imports of a mocked module // return the same instance (cache: true) or a fresh one (cache: false, the // default). import assert from "node:assert/strict"; import { mock } from "node:test"; // cache: true keeps a single mocked instance. const cached = mock.module("./basic-esm.mjs", { namedExports: { fn() { return 1; }, }, cache: true, }); const a = await import("./basic-esm.mjs"); const b = await import("./basic-esm.mjs"); assert.strictEqual(a, b); assert.strictEqual(a.fn(), 1); cached.restore(); // cache: false (explicit) gives a fresh instance every time. const fresh = mock.module("./basic-esm.mjs", { namedExports: { fn() { return 2; }, }, cache: false, }); const c = await import("./basic-esm.mjs"); const d = await import("./basic-esm.mjs"); assert.notStrictEqual(c, d); assert.strictEqual(c.fn(), 2); assert.strictEqual(d.fn(), 2); fresh.restore(); console.log("cache ok");