Gathering detailed insights and metrics for esmock
Gathering detailed insights and metrics for esmock
Gathering detailed insights and metrics for esmock
Gathering detailed insights and metrics for esmock
npm install esmock
Typescript
Module System
Min. Node Version
Node Version
NPM Version
99.3
Supply Chain
99.5
Quality
84.5
Maintenance
100
Vulnerability
100
License
JavaScript (92.6%)
TypeScript (7.4%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
ISC License
206 Stars
1,124 Commits
20 Forks
4 Watchers
2 Branches
14 Contributors
Updated on Jul 01, 2025
Latest Version
2.7.1
Package Id
esmock@2.7.1
Unpacked Size
25.62 kB
Size
9.85 kB
File Count
6
NPM Version
11.3.0
Node Version
22.16.0
Published on
Jul 01, 2025
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
No dependencies detected.
1+ ██╗ 2+ ██████╗ ███████╗ █████═████╗ ██████╗ ██████╗██║ ██╗ 3+██╔═══██╗██╔═════╝██╔══██╔══██╗██╔═══██╗██╔════╝██║ ██╔╝ 4+████████║╚██████╗ ██║ ██║ ██║██║ ██║██║ ██████╔╝ 5+██╔═════╝ ╚════██╗██║ ██║ ██║██║ ██║██║ ██╔══██╗ 6+╚███████╗███████╔╝██║ ██║ ██║╚██████╔╝╚██████╗██║ ╚██╗ 7+ ╚══════╝╚══════╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═════╝╚═╝ ╚═╝
esmock provides native ESM import and globals mocking for unit tests. Use examples below as a quick-start guide, see the descriptive and friendly esmock guide here, or browse esmock's test runner examples.
Note: For versions of node prior to v20.6.0, "--loader" command line arguments must be used with esmock
as demonstrated in the wiki. Current versions of node do not require "--loader".
Note: due to --loader issues, support for ava
and jest
are dropped.
esmock
has the below signature
1await esmock( 2 './to/module.js', // path to target module being tested 3 { ...childmocks }, // mock definitions imported by target module 4 { ...globalmocks }) // mock definitions imported everywhere
esmock
examples
1import test from 'node:test' 2import assert from 'node:assert' 3import esmock from 'esmock' 4 5test('package, alias and local file mocks', async () => { 6 const cookup = await esmock('../src/cookup.js', { 7 addpkg: (a, b) => a + b, 8 '#icon': { coffee: '☕', bacon: '🥓' }, 9 '../src/breakfast.js': { 10 default: () => ['coffee', 'bacon'], 11 addSalt: meal => meal + '🧂' 12 } 13 }) 14 15 assert.equal(cookup('breakfast'), '☕🥓🧂') 16}) 17 18test('full import tree mocks —third param', async () => { 19 const { getFile } = await esmock('../src/main.js', {}, { 20 // mocks *every* fs.readFileSync inside the import tree 21 fs: { readFileSync: () => 'returned to 🌲 every caller in the tree' } 22 }) 23 24 assert.equal(getFile(), 'returned to 🌲 every caller in the tree') 25}) 26 27test('mock fetch, Date, setTimeout and any globals', async () => { 28 // https://github.com/iambumblehead/esmock/wiki#call-esmock-globals 29 const { userCount } = await esmock('../Users.js', { 30 '../req.js': await esmock('../req.js', { 31 import: { // define globals like 'fetch' on the import namespace 32 fetch: async () => ({ 33 status: 200, 34 json: async () => [['jim','😄'],['jen','😊']] 35 }) 36 } 37 }) 38 }) 39 40 assert.equal(await userCount(), 2) 41}) 42 43test('mocks "await import()" using esmock.p', async () => { 44 // using esmock.p, mock definitions are kept in cache 45 const doAwaitImport = await esmock.p('../awaitImportLint.js', { 46 eslint: { ESLint: cfg => cfg } 47 }) 48 49 // mock definition is returned from cache, when import is called 50 assert.equal(await doAwaitImport('cfg🛠️'), 'cfg🛠️') 51 // a bit more info are found in the wiki guide 52}) 53 54test('esmock.strict mocks', async () => { 55 // replace original module definitions and do not merge them 56 const pathWrapper = await esmock.strict('../src/pathWrapper.js', { 57 path: { dirname: () => '/path/to/file' } 58 }) 59 60 // error, because "path" mock above does not define path.basename 61 assert.rejects(() => pathWrapper.basename('/dog.🐶.png'), { 62 name: 'TypeError', 63 message: 'path.basename is not a function' 64 }) 65})
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
0 existing vulnerabilities detected
Reason
2 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 1
Reason
Found 0/8 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More