Skip to content

Commit fc2f9ca

Browse files
committed
Upgraded to 2.0
1 parent 147187d commit fc2f9ca

File tree

5 files changed

+122
-11
lines changed

5 files changed

+122
-11
lines changed

README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ npm install promise-mock
1919
### Simple use with Mocha or Jasmine
2020
Get the result of a Promise `PromiseMock.getResult`
2121
```js
22-
var PromiseMock = require('promise-mock');
22+
import PromiseMock from 'promise-mock';
2323

2424
describe('testing', function() {
2525
beforeEach(function() {
@@ -38,7 +38,7 @@ describe('testing', function() {
3838

3939
Handle errors synchronously
4040
```js
41-
var PromiseMock = require('promise-mock');
41+
import PromiseMock from 'promise-mock';
4242

4343
describe('testing', function() {
4444
beforeEach(function() {
@@ -57,7 +57,7 @@ describe('testing', function() {
5757

5858
Execute a single async callback
5959
```js
60-
var PromiseMock = require('promise-mock');
60+
import PromiseMock from 'promise-mock';
6161

6262
describe('testing', function() {
6363
beforeEach(function() {
@@ -79,7 +79,7 @@ describe('testing', function() {
7979
```
8080
Resolve all pending Promises
8181
```js
82-
var PromiseMock = require('promise-mock');
82+
import PromiseMock from 'promise-mock';
8383

8484
describe('testing', function() {
8585
beforeEach(function() {
@@ -108,7 +108,12 @@ describe('testing', function() {
108108
});
109109
```
110110

111+
### For commonjs/require webpack and rollup
112+
Since the move to ES6 modules. You need to add `.default`
113+
```js
114+
const PromiseMock = require('promise-mock').default;
111115

116+
```
112117

113118
### Testing
114119
```

lib/index.js

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
'use strict';
2+
3+
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
4+
5+
var Promise = _interopDefault(require('promise-polyfill'));
6+
7+
const root = typeof window === 'undefined' ? global : window;
8+
9+
10+
function PromiseMock() {
11+
Promise.apply(this, arguments);
12+
}
13+
PromiseMock.prototype = Object.create(Promise.prototype);
14+
Object.keys(Promise).forEach(function (key) {
15+
PromiseMock[key] = Promise[key];
16+
});
17+
18+
// Queue of waiting callbacks
19+
PromiseMock.waiting = [];
20+
21+
22+
/**
23+
* Execute a pending Promise
24+
*/
25+
PromiseMock.run = function run(count) {
26+
var runTimes = count ? count : 1;
27+
28+
if (PromiseMock.waiting.length === 0) {
29+
throw new Error('No Promises waiting. Can\'t Promise.run()')
30+
}
31+
32+
while (runTimes > 0 && PromiseMock.waiting.length > 0) {
33+
PromiseMock.waiting.shift()();
34+
runTimes--;
35+
}
36+
};
37+
38+
/**
39+
* Execute all pending Promises
40+
*/
41+
PromiseMock.runAll = function runAll() {
42+
if (PromiseMock.waiting.length === 0) {
43+
throw new Error('No Promises waiting. Can\'t Promise.run()')
44+
}
45+
46+
while (PromiseMock.waiting.length > 0) {
47+
PromiseMock.run();
48+
}
49+
};
50+
51+
PromiseMock._orginal = null;
52+
PromiseMock.install = function install() {
53+
PromiseMock._original = root.Promise;
54+
PromiseMock._originalImmediate = Promise._immediateFn;
55+
// Update the immediate function to push to queue
56+
Promise._immediateFn = function mockImmediateFn(fn) {
57+
PromiseMock.waiting.push(fn);
58+
};
59+
60+
root.Promise = PromiseMock;
61+
};
62+
63+
PromiseMock.uninstall = function uninstall() {
64+
PromiseMock.clear();
65+
if (PromiseMock._original) {
66+
root.Promise = PromiseMock._original;
67+
}
68+
69+
if (PromiseMock._originalImmediate) {
70+
Promise._immediateFn = PromiseMock._originalImmediate;
71+
}
72+
};
73+
74+
/**
75+
* Get the result of a Promise synchronously, throws on Promise reject
76+
* @param {Promise} promise
77+
* @returns {*}
78+
*/
79+
PromiseMock.getResult = function result(promise) {
80+
var result, error;
81+
promise.then(function (promResult) {
82+
result = promResult;
83+
}, function (promError) {
84+
error = promError;
85+
});
86+
PromiseMock.runAll();
87+
if (error) {
88+
throw error;
89+
}
90+
return result;
91+
};
92+
93+
/**
94+
* Clear all pending Promises
95+
*/
96+
PromiseMock.clear = function clear() {
97+
PromiseMock.waiting = [];
98+
};
99+
100+
module.exports = PromiseMock;

package.json

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
{
22
"name": "promise-mock",
3-
"version": "1.1.2",
3+
"version": "2.0.0",
44
"description": "Promise mock library for synchronous Promise testing",
5-
"main": "index.js",
5+
"main": "lib/index.js",
6+
"module": "src/index.js",
67
"scripts": {
7-
"test": "JASMINE_CONFIG_PATH=jasmine.json ./node_modules/jasmine/bin/jasmine.js;"
8+
"test": "JASMINE_CONFIG_PATH=jasmine.json ./node_modules/jasmine/bin/jasmine.js;",
9+
"build": "run-p build:**",
10+
"build:cjs": "rollup -i src/index.js -o lib/index.js -f cjs"
811
},
912
"repository": {
1013
"type": "git",
@@ -17,7 +20,9 @@
1720
},
1821
"homepage": "https://github.com/taylorhakes/promise-mock",
1922
"devDependencies": {
20-
"jasmine": "^2.3.1"
23+
"jasmine": "^2.3.1",
24+
"npm-run-all": "^4.1.2",
25+
"rollup": "^0.52.0"
2126
},
2227
"keywords": [
2328
"promise",

index.js renamed to src/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
var Promise = require('promise-polyfill'),
2-
root = typeof window === 'undefined' ? global : window;
1+
import Promise from 'promise-polyfill';
2+
3+
const root = typeof window === 'undefined' ? global : window;
34

45

56
function PromiseMock() {

tests/index.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var Promise = require('../index');
1+
var Promise = require('../src/index');
22

33
describe('Promise mock', function() {
44
var oldPromise;

0 commit comments

Comments
 (0)