Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

rewiremock

Package Overview
Dependencies
Maintainers
1
Versions
71
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rewiremock - npm Package Compare versions

Comparing version 3.11.0 to 3.11.1

webpack.js

19

es/asyncModules.js

@@ -5,8 +5,15 @@ import { getAllAsyncMocks, insertMock } from "./mocks";

var loadInRoll = function loadInRoll(mocks) {
var loadInRoll = function loadInRoll(mocks, middleTick) {
if (mocks.length) {
currentModule = mocks[0];
return Promise.resolve().then(currentModule.creator).then(function () {
return loadInRoll(mocks.slice(1));
});
currentModule.result = currentModule.creator();
if (currentModule.result && currentModule.result.then) {
return middleTick().then(function () {
return currentModule.result;
}).then(function () {
return loadInRoll(mocks.slice(1), middleTick);
});
} else {
return loadInRoll(mocks.slice(1), middleTick);
}
} else {

@@ -33,5 +40,5 @@ return Promise.resolve();

execute: function execute() {
execute: function execute(middleTick) {
var mocks = getAllAsyncMocks();
return loadInRoll(mocks);
return loadInRoll(mocks, middleTick);
}

@@ -38,0 +45,0 @@ };

@@ -58,3 +58,3 @@ function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }

var mocker = registrations({
MOCKS: [enable()].concat(_toConsumableArray(mocks))
MOCKS: [].concat(_toConsumableArray(mocks), [enable()])
});

@@ -61,0 +61,0 @@

@@ -161,3 +161,3 @@ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };

console.error('Rewiremock: listed async modules should finish loading first. Use async API of rewiremock.', asyncModulesLeft.map(function (module) {
return module.creator.toString();
return module.creator;
}));

@@ -164,0 +164,0 @@ /* eslint-enable */

@@ -55,3 +55,3 @@ import path from 'path';

global['_REWIREMOCK_HOISTED_'].forEach(function (cb) {
cb(API.mockModule);
cb(API.mockModule, { plugins: plugins, overrideEntryPoint: overrideEntryPoint });
});

@@ -58,0 +58,0 @@ global['_REWIREMOCK_HOISTED_'] = [];

@@ -114,2 +114,3 @@ var _this = this;

scope();
Module.probeSyncModules();
Module.overloadRequire();

@@ -116,0 +117,0 @@ storeCache();

@@ -20,6 +20,16 @@ import { dirname, resolve } from 'path';

Module._load = _probeAsyncModules.load(this);
return _probeAsyncModules.execute().then(function () {
return _probeAsyncModules.execute(function () {
return Promise.resolve(true);
}).then(function () {
Module._load = load;
});
},
probeSyncModules: function probeSyncModules() {
var load = Module._load;
Module._load = _probeAsyncModules.load(this);
_probeAsyncModules.execute(function () {
return Promise.reject('could not use dynamic imports with sync API');
});
Module._load = load;
},
_resolveFilename: function _resolveFilename(fileName, module) {

@@ -26,0 +36,0 @@ return Module._resolveFilename(fileName, module);

@@ -19,5 +19,8 @@ import childOnly from './childOnly';

var webpack = nodejs;
var exports = {
childOnly: childOnly,
nodejs: nodejs,
webpack: webpack,
protectNodeModules: protectNodeModules,

@@ -24,0 +27,0 @@ relative: relative,

@@ -11,6 +11,11 @@ "use strict";

const loadInRoll = mocks => {
const loadInRoll = (mocks, middleTick) => {
if (mocks.length) {
currentModule = mocks[0];
return Promise.resolve().then(currentModule.creator).then(() => loadInRoll(mocks.slice(1)));
currentModule.result = currentModule.creator();
if (currentModule.result && currentModule.result.then) {
return middleTick().then(() => currentModule.result).then(() => loadInRoll(mocks.slice(1), middleTick));
} else {
return loadInRoll(mocks.slice(1), middleTick);
}
} else {

@@ -34,5 +39,5 @@ return Promise.resolve();

execute() {
execute(middleTick) {
const mocks = (0, _mocks.getAllAsyncMocks)();
return loadInRoll(mocks);
return loadInRoll(mocks, middleTick);
}

@@ -39,0 +44,0 @@ };

@@ -54,3 +54,3 @@ 'use strict';

const mocker = registrations({
MOCKS: [enable(), ...mocks]
MOCKS: [...mocks, enable()]
});

@@ -57,0 +57,0 @@

@@ -166,3 +166,3 @@ 'use strict';

/* eslint-disable no-console */
console.error('Rewiremock: listed async modules should finish loading first. Use async API of rewiremock.', asyncModulesLeft.map(module => module.creator.toString()));
console.error('Rewiremock: listed async modules should finish loading first. Use async API of rewiremock.', asyncModulesLeft.map(module => module.creator));
/* eslint-enable */

@@ -169,0 +169,0 @@ throw new Error('Rewiremock: listed async modules should finish loading first. Use async API of rewiremock.');

@@ -77,3 +77,3 @@ 'use strict';

global['_REWIREMOCK_HOISTED_'].forEach(cb => {
cb(API.mockModule);
cb(API.mockModule, { plugins: _index2.default, overrideEntryPoint });
});

@@ -80,0 +80,0 @@ global['_REWIREMOCK_HOISTED_'] = [];

@@ -129,2 +129,3 @@ 'use strict';

scope();
_module2.default.probeSyncModules();
_module2.default.overloadRequire();

@@ -131,0 +132,0 @@ storeCache();

@@ -39,3 +39,3 @@ 'use strict';

_getModule2.default._load = _asyncModules2.default.load(this);
return _asyncModules2.default.execute().then(() => {
return _asyncModules2.default.execute(() => Promise.resolve(true)).then(() => {
_getModule2.default._load = load;

@@ -45,2 +45,9 @@ });

probeSyncModules() {
const load = _getModule2.default._load;
_getModule2.default._load = _asyncModules2.default.load(this);
_asyncModules2.default.execute(() => Promise.reject('could not use dynamic imports with sync API'));
_getModule2.default._load = load;
},
_resolveFilename(fileName, module) {

@@ -47,0 +54,0 @@ return _getModule2.default._resolveFilename(fileName, module);

@@ -63,5 +63,8 @@ 'use strict';

const webpack = _nodejs2.default;
const _exports = {
childOnly: _childOnly2.default,
nodejs: _nodejs2.default,
webpack,
protectNodeModules: _protectNodeModules2.default,

@@ -68,0 +71,0 @@ relative: _relative2.default,

@@ -0,7 +1,10 @@

global['_REWIREMOCK_HOISTED_'] = global['_REWIREMOCK_HOISTED_'] || [];
global['_REWIREMOCK_HOISTED_'].unshift( function (rewiremock, api) {
api.overrideEntryPoint(module);
rewiremock.addPlugin(api.plugins.nodejs);
});
const mock = require("./lib/index.js");
mock.overrideEntryPoint(module);
mock.addPlugin(mock.plugins.nodejs);
module.exports = Object.assign({}, mock, mock.default);
{
"name": "rewiremock",
"version": "3.11.0",
"version": "3.11.1",
"description": "Advanced dependency mocking device.",

@@ -40,2 +40,3 @@ "main": "lib/index.js",

"node.js",
"webpack.js",
"internal.js",

@@ -42,0 +43,0 @@ "babel.js"

@@ -166,2 +166,4 @@ # rewiremock

any _specific_ code in a "functions", and let JavaScript to hoist it.
- Most of variables, you have define in the file, are not visible to __hoisted__ code, as long they are __not yet defined__.
Only functions would be hoisted.

@@ -254,2 +256,24 @@ 1. Add `rewiremock/babel` into plugin section in `.babelrc`

### Guided mocking
You may use `require` or `import` to let IDE help you to properly write fileName,
and hide all filename resolution and transformation behind the scenes.
But there are things you have to keep in mind
1. Resolution of synchronous API happens on .enable
```js
rewiremock(() => require('./fileToMock1')); // this mock would work
rewiremock.enable();
rewiremock(() => require('./fileToMock2')); //this mock WOULD NOT WORK!
```
2. Using async API will throw an error
```js
rewiremock(() => import('./fileToMock1'));
rewiremock.enable(); // this is an exception
```
3. Async API requires async API
```js
rewiremock.module( () => import('file')) // this is ok
rewiremock.around(..., rw => rw.mock(() => import('file2'))) // this is ok
```
# Type safety

@@ -350,2 +374,8 @@ Rewiremock is able to provide a type-safe mocks. To enable type-safety follow these steps:

#### Hint
For better dev experience include special configuration of webpack
```js
import rewiremock from 'rewiremock/webpack';
```
### webpack troubleshooting

@@ -352,0 +382,0 @@ Currently there are 2 known problems, both for mocha+webpack, ie using nodejs to run webpack bundle:

@@ -6,15 +6,19 @@ const {dirname, resolve} = require('path');

_load: interceptor.load,
_resolveFilename(fileName, parent){
_resolveFilename(fileName, parent) {
const targetFile = resolve(dirname(parent.i), fileName);
const keys = Object
.keys(__webpack_modules__)
.sort((a,b) => a.length - b.length);
.sort((a, b) => a.length - b.length);
const targetFileIndex = targetFile + '/index';
const asFile = keys.find(name => name.indexOf(targetFile) > 0);
const asIndex = keys.find(name => name.indexOf(targetFileIndex) > 0);
const asIs = keys.find(name => name.indexOf(fileName) >= 0);
const asFile = keys.find(name => name.indexOf(targetFile) >= 0);
const asIndex = keys.find(name => name.indexOf(targetFileIndex) >= 0);
if (asFile && asIndex && asFile.substr(targetFile.length+1).indexOf('/') >= 0) {
return asIndex;
if (asFile && asIndex && asFile.substr(targetFile.length + 1).indexOf('/') >= 0) {
return asIndex;
}
if (!asFile && !asIs) {
console.warn('rewiremock: ', fileName, 'requested from', parent.i, 'was not found');
}
return asFile || fileName;

@@ -21,0 +25,0 @@ },

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc