Skip to content

Commit

Permalink
Merge pull request #207 from iambumblehead/use-more-realistic-example…
Browse files Browse the repository at this point in the history
…-for-fetch-mock-at-readme

use more realistic example for fetch mock
  • Loading branch information
iambumblehead authored Jun 1, 2023
2 parents 3fab421 + b2347f2 commit a7615f7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# changelog

* 2.3.1 _Jun.01.2023_
* [improve README example](https://github.com/iambumblehead/esmock/pull/207) for mocking global values
* use the word 'global' in the global values mocking example only, to improve clarity (hopefully)
* 2.3.0 _May.31.2023_
* [add initial support](https://github.com/iambumblehead/esmock/pull/205) for the solution to "globalThis" mocks,
* support injecting definitions into the mock import tree,
Expand Down
33 changes: 21 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
```
[![npm][9]][7] [![coverage][8]][2] [![install size][6]][5] [![downloads][10]][7]

**esmock provides native ESM import mocking for unit tests.** Use examples below as a quick-start guide, see the [descriptive and friendly esmock guide here,][4] or browse [esmock's test runner examples.][3]
**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,][4] or browse [esmock's test runner examples.][3]


`esmock` is used with node's --loader
Expand Down Expand Up @@ -64,22 +64,31 @@ test('package, alias and local file mocks', async () => {
assert.strictEqual(cookup('breakfast'), '☕🥓🧂')
})

test('global mocks fetch, Date, setTimeout etc', async () => {
const reqUsers = await esmock('../reqUsers.js', {
import: { // define the 'fetch' mock, see wiki for more info
fetch: () => '[["jim","😄"],["jen","😊"]]'
}
test('full import tree mocks —third param', async () => {
const { getFile } = await esmock('../src/main.js', {}, {
// mocks *every* fs.readFileSync inside the import tree
fs: { readFileSync: () => 'returned to 🌲 every caller in the tree' }
})
assert.strictEqual(await reqUsers(), '[["jim","😄"],["jen","😊"]]')

assert.strictEqual(getFile(), 'returned to 🌲 every caller in the tree')
})

test('global instance mocks —third param', async () => {
const { getFile } = await esmock('../src/main.js', {}, {
fs: { readFileSync: () => 'returns this 🌎 globally' }
test('mock fetch, Date, setTimeout and any globals', async () => {
// https://github.com/iambumblehead/esmock/wiki#call-esmock-globals
const Users = await esmock('../Users.js', {
// nested esmock defines 'fetch' at req.js' import tree *only*
'../req.js': await esmock('../req.js', {
import: {
// define globals, such as 'fetch', using the import namespace
fetch: async () => ({
status: 200,
json: async () => [["jim","😄"],["jen","😊"]]
})
}
})
})

assert.strictEqual(getFile(), 'returns this 🌎 globally')
assert.deepEqual(await Users.count(), 2)
})

test('mocks "await import()" using esmock.p', async () => {
Expand Down
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "esmock",
"type": "module",
"version": "2.3.0",
"version": "2.3.1",
"license": "ISC",
"readmeFilename": "README.md",
"description": "provides native ESM import mocking for unit tests",
"description": "provides native ESM import and globals mocking for unit tests",
"author": "chris <[email protected]>",
"main": "./src/esmock.js",
"exports": {
Expand Down Expand Up @@ -51,7 +51,10 @@
"modules",
"mocking",
"proxyquire",
"rewire"
"rewire",
"global",
"fetch",
"mock fetch"
],
"engines": {
"node": ">=14.16.0"
Expand Down

0 comments on commit a7615f7

Please sign in to comment.