diff --git a/CHANGELOG.md b/CHANGELOG.md index 5648c334..f4defe55 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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, diff --git a/README.md b/README.md index 0af62e5c..1032a270 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 () => { diff --git a/package.json b/package.json index 59ed8568..4caf329d 100644 --- a/package.json +++ b/package.json @@ -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 ", "main": "./src/esmock.js", "exports": { @@ -51,7 +51,10 @@ "modules", "mocking", "proxyquire", - "rewire" + "rewire", + "global", + "fetch", + "mock fetch" ], "engines": { "node": ">=14.16.0"