Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
esmock provides simple, native ESM import mocking on a per-unit basis.
To get started, simply install esmock
, and update your test script to include it as a loader.
Note: esmock must be used with node's experimental --loader
esmock
:$ npm i -D esmock
test
script in your package.json
:{
"type": "module",
"scripts": {
"test-ava": "ava --node-arguments=\"--loader=esmock\"",
"test-mocha": "mocha --loader=esmock --no-warnings"
}
}
Mocking is very simple and can be done on a per-unit basis. This means that each unit in your test file can be mocked differently, to allow for mocking out various scenarios.
The syntax is very simple:
const targetModuleExports = await esmock(targetModule, childMocks, globalMocks)
targetModule
: The path to the module you'll be testing.targetModuleExports
: Anything that targetModule
exports.childMocks
: Modules imported into targetModule
that you want to mock.globalMocks
: Optional Modules that you always want to mock, even if they're not directly imported by targetModule
.The targetModuleExports
is the default export, or it can be destructured to retrieve named exports.
// Grabbing the default export
const defaultExport = await esmock('../src/my-module.js', childMocks, globalMocks)
// Grabbing both the default export and a named export
const { default: defaultExport, namedExport } = await esmock('../src/my-module.js', childMocks, globalMocks)
The *mocks
parameters follow the below syntax:
childMocks | globalMocks = {
'npm-pkg-name': {
default: __mock_value__,
namedExport: __mock_value__
},
'../relative/path/to/imported/file.js': {
default: __mock_value__,
namedExport: __mock_value__
}
}
Where __mock_value__
could be a string
, function
, class
, or anything else, depending on the module/file you're importing in your target.
Here's an example that demonstrates mocking a named module, as well as a local JS file. In this example, we're testing the ./src/main.js
and mocking the modules and files that it imports.
./src/main.js:
import serializer from 'serializepkg';
import someDefaultExport from './someModule.js';
export default () => {
const json = serializer(someDefaultExport());
return json;
}
./tests/main.js:
test('should mock modules and local files at same time', async t => {
const main = await esmock('../src/main.js', {
serializepkg: {
default: obj => JSON.stringify(obj)
},
'../src/someModule.js' : {
default: () => ({ foo: 'bar' })
}
});
// Because `serializepkg` is mocked as a function that calls JSON.stringify()
// And `someDefaultExport` is mocked as a function that returns { foo: 'bar' }
t.is(main(), JSON.stringify({ foo: 'bar' }));
});
FAQs
provides native ESM import and globals mocking for unit tests
The npm package esmock receives a total of 18,315 weekly downloads. As such, esmock popularity was classified as popular.
We found that esmock demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers 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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.