Security News
pnpm 10.0.0 Blocks Lifecycle Scripts by Default
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Isolate your ES Modules for testing.
npm i -D modpod
import {test} from "node:test";
import modpod from "modpod";
test("easy mode", async () => {
const instance = await modpod("./target.js", {
"./dep.js": {
// Replaces `export default function(){...}` in ./dep.js
default: () => "mocked default",
// Replaces `export function otherThing(){...}` in ./dep.js
otherThing: () => "mocked named export 'otherThing'"
}
});
// Exercise instance
// Assert what you expected would happen
});
The default export is a function that sets up a mock environment, imports the target, and cleans up.
modpod()
is a shallow mock. It'll only replace dependencies one level deep.
modpod.strict()
will make sure that all dependencies of ./target.js
are mocked. If an import is missing a mock, an error will be thrown.
modpod.deep()
will replace ./dep.js
anywhere it is imported. Even if it's a dependency of a dependency.
Note: If a mock is declared, but never used, then an error will be thrown.
If you need more control over the mock lifecycle (like dynamic imports), then you'll need to use the Pod
class directly.
Let's consider a few files:
target.js
export async function doSomething() {
const dep = await import("./dep.js");
return dep();
}
dep.js
export default () => "dep";
Now we can write a test that handles the dynamic import.
import assert from "node:assert";
import {test, mock} from "node:test";
import {Pod} from "modpod";
test("hard mode", async (t) => {
const p = new Pod({strict: true});
t.after(() => p.dispose()); // Clean up
const dep = mock.fn(() => "mocked");
// NOTE: dep will be wrapped {default: dep} because it's a function.
p.mock("./dep.js", dep);
const instance = await p.import("./target.js");
assert.strictEqual(dep.mock.calls.length, 0);
const result = await instance.doSomething();
assert.strictEqual(dep.mock.calls.length, 1);
});
FAQs
Isolate your ES Modules for testing.
The npm package modpod receives a total of 1 weekly downloads. As such, modpod popularity was classified as not popular.
We found that modpod demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.