From be3e446595a04ef9604bcd60de3d0b0b986ef932 Mon Sep 17 00:00:00 2001 From: Sebastian Pape Date: Thu, 17 Jun 2021 12:51:17 +0200 Subject: [PATCH] 0.11.1: use global in jest context --- README.md | 7 ++----- dist/cjs/index.js | 6 +++++- dist/es/index.js | 6 +++++- dist/umd/index.js | 6 +++++- package.json | 2 +- src/Web3Mock.js | 6 +++++- tests/units/web3mock.spec.js | 7 ++++++- 7 files changed, 29 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index a360c10b..633de03c 100644 --- a/README.md +++ b/README.md @@ -338,8 +338,5 @@ npm publish #### Jest -In your jest test environment `global` is `window`, so in order to have your implementation still rely on `window` (e.g. `window.ethereum`) pass `global` as `window` to `Web3Mock`: - -```javascript -Web3Mock({ window: global, mocks: 'ethereum' }); -``` +In your jest test environment `global` is `window` +so `Web3Mock` takes jest's `global` automatically when using `Web3Mock` in a jest test environment. diff --git a/dist/cjs/index.js b/dist/cjs/index.js index db8f140d..380c5ad9 100644 --- a/dist/cjs/index.js +++ b/dist/cjs/index.js @@ -356,7 +356,11 @@ let mock = function ({ configuration, window, provider }) { } }; -let Web3Mock = ({ mocks, provider, window = window }) => { +let Web3Mock = ({ + mocks, + provider, + window = typeof window == 'object' ? window : typeof global == 'object' ? global : undefined, +}) => { if (mocks === undefined || mocks.length === 0) { throw 'Web3Mock: No mocks defined!' } diff --git a/dist/es/index.js b/dist/es/index.js index d05df46d..37107094 100644 --- a/dist/es/index.js +++ b/dist/es/index.js @@ -352,7 +352,11 @@ let mock = function ({ configuration, window, provider }) { } }; -let Web3Mock = ({ mocks, provider, window = window }) => { +let Web3Mock = ({ + mocks, + provider, + window = typeof window == 'object' ? window : typeof global == 'object' ? global : undefined, +}) => { if (mocks === undefined || mocks.length === 0) { throw 'Web3Mock: No mocks defined!' } diff --git a/dist/umd/index.js b/dist/umd/index.js index 26f1d566..e210a04f 100644 --- a/dist/umd/index.js +++ b/dist/umd/index.js @@ -356,7 +356,11 @@ } }; - let Web3Mock = ({ mocks, provider, window = window }) => { + let Web3Mock = ({ + mocks, + provider, + window = typeof window == 'object' ? window : typeof global == 'object' ? global : undefined, + }) => { if (mocks === undefined || mocks.length === 0) { throw 'Web3Mock: No mocks defined!' } diff --git a/package.json b/package.json index 1bbd2872..4bd5e73e 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "depay-web3mock", "moduleName": "Web3Mock", - "version": "0.11.0", + "version": "0.11.1", "description": "JavaScript library to mock web3 responses either by emulating blockchain wallets or RPC calls.", "main": "./dist/cjs/index.js", "files": [ diff --git a/src/Web3Mock.js b/src/Web3Mock.js index d4cd3178..b93f038f 100644 --- a/src/Web3Mock.js +++ b/src/Web3Mock.js @@ -22,7 +22,11 @@ let mock = function ({ configuration, window, provider }) { } } -let Web3Mock = ({ mocks, provider, window = window }) => { +let Web3Mock = ({ + mocks, + provider, + window = typeof window == 'object' ? window : typeof global == 'object' ? global : undefined, +}) => { if (mocks === undefined || mocks.length === 0) { throw 'Web3Mock: No mocks defined!' } diff --git a/tests/units/web3mock.spec.js b/tests/units/web3mock.spec.js index 7bc4bc55..9d83f5f9 100644 --- a/tests/units/web3mock.spec.js +++ b/tests/units/web3mock.spec.js @@ -8,7 +8,6 @@ describe('Web3Mock', () => { }); - it('does not change existing values in the passed window object', () => { let window = { someVar: 1, ethereum: { existedAlready: 1 } } @@ -56,4 +55,10 @@ describe('Web3Mock', () => { }).toThrow('Web3Mock: Unknown blockchain!'); }); + + it('takes jest "global" automatically if there is no window but a global', () => { + + Web3Mock({ mocks: ['ethereum'] }); + + }); });