From 43682c17fca145d21557de7efd4c9d9aa3448ae9 Mon Sep 17 00:00:00 2001 From: chris Date: Thu, 1 Jun 2023 13:25:53 -0700 Subject: [PATCH 1/6] use more realistic example for fetch mock --- README.md | 25 +++++++++++++++++-------- package.json | 2 +- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 0af62e5c..66ffdea5 100644 --- a/README.md +++ b/README.md @@ -65,21 +65,30 @@ test('package, alias and local file mocks', async () => { }) 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","😊"]]' - } + // https://github.com/iambumblehead/esmock/wiki#call-esmock-globals + const Users = await esmock('../Users.js', { + // this nested-esmock will define '../req.js' for '../Users.js' 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(await reqUsers(), '[["jim","πŸ˜„"],["jen","😊"]]') + assert.deepEqual(await Users.count(), 2) }) -test('global instance mocks β€”third param', async () => { +test('global import tree mocks β€”third param', async () => { const { getFile } = await esmock('../src/main.js', {}, { - fs: { readFileSync: () => 'returns this 🌎 globally' } + // mocks *every* fs.readFileSync inside the import tree + fs: { readFileSync: () => 'returned to 🌲 every caller in the tree' } }) - assert.strictEqual(getFile(), 'returns this 🌎 globally') + assert.strictEqual(getFile(), 'returned to 🌲 every caller in the tree') }) test('mocks "await import()" using esmock.p', async () => { diff --git a/package.json b/package.json index 59ed8568..a4d76971 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "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", From a3451e87fb4e631bd5cde374039c8c1881ca935e Mon Sep 17 00:00:00 2001 From: chris Date: Thu, 1 Jun 2023 13:34:39 -0700 Subject: [PATCH 2/6] reposition test elements in readme --- README.md | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 66ffdea5..48c23a3d 100644 --- a/README.md +++ b/README.md @@ -64,10 +64,19 @@ test('package, alias and local file mocks', async () => { assert.strictEqual(cookup('breakfast'), 'β˜•πŸ₯“πŸ§‚') }) +test('global 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(getFile(), 'returned to 🌲 every caller in the tree') +}) + test('global mocks fetch, Date, setTimeout etc', async () => { // https://github.com/iambumblehead/esmock/wiki#call-esmock-globals const Users = await esmock('../Users.js', { - // this nested-esmock will define '../req.js' for '../Users.js' only + // 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 @@ -82,15 +91,6 @@ test('global mocks fetch, Date, setTimeout etc', async () => { assert.deepEqual(await Users.count(), 2) }) -test('global 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(getFile(), 'returned to 🌲 every caller in the tree') -}) - test('mocks "await import()" using esmock.p', async () => { // using esmock.p, mock definitions are kept in cache const doAwaitImport = await esmock.p('../awaitImportLint.js', { From 90b47109c4686ef838fcd2e99f3561358ba30d6c Mon Sep 17 00:00:00 2001 From: chris Date: Thu, 1 Jun 2023 13:38:27 -0700 Subject: [PATCH 3/6] remove word globals from full import tree mock example, try to not confuse with global variables working in subsequent example --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 48c23a3d..34aa4e5c 100644 --- a/README.md +++ b/README.md @@ -64,7 +64,7 @@ test('package, alias and local file mocks', async () => { assert.strictEqual(cookup('breakfast'), 'β˜•πŸ₯“πŸ§‚') }) -test('global import tree mocks β€”third param', async () => { +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' } @@ -73,7 +73,7 @@ test('global import tree mocks β€”third param', async () => { assert.strictEqual(getFile(), 'returned to 🌲 every caller in the tree') }) -test('global mocks fetch, Date, setTimeout etc', async () => { +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* From eed6d9bef3c70fc6f2e28dd220889063574571ab Mon Sep 17 00:00:00 2001 From: chris Date: Thu, 1 Jun 2023 13:44:36 -0700 Subject: [PATCH 4/6] update CHANGELOG --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) 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, From 46f27ec953d148566d840fff13036c4c6c16491b Mon Sep 17 00:00:00 2001 From: chris Date: Thu, 1 Jun 2023 13:47:20 -0700 Subject: [PATCH 5/6] remove whitespace --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 34aa4e5c..50605d0f 100644 --- a/README.md +++ b/README.md @@ -87,7 +87,7 @@ test('mock fetch, Date, setTimeout and any globals', async () => { } }) }) - + assert.deepEqual(await Users.count(), 2) }) From b2347f2739d99a8e8ebdec86e04cd482423ed599 Mon Sep 17 00:00:00 2001 From: chris Date: Thu, 1 Jun 2023 13:50:32 -0700 Subject: [PATCH 6/6] update wording and description in README and package.json --- README.md | 2 +- package.json | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 50605d0f..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 diff --git a/package.json b/package.json index a4d76971..4caf329d 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "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"