@js-bits/xpromise
Advanced tools
Comparing version 0.2.1 to 0.2.2
26
index.js
import enumerate from '@js-bits/enumerate'; | ||
const { Prefix } = enumerate; | ||
// pseudo-private properties emulation in order to avoid source code transpiling | ||
@@ -11,2 +13,7 @@ // TODO: replace with #privateField syntax when it gains wide support | ||
const ERRORS = enumerate(Prefix('ExtendablePromise|'))` | ||
InstantiationError | ||
ExecutionError | ||
`; | ||
/** | ||
@@ -23,4 +30,4 @@ * @class | ||
let reject; | ||
super((...funcs) => { | ||
[resolve, reject] = funcs; | ||
super((...args) => { | ||
[resolve, reject] = args; | ||
}); | ||
@@ -33,3 +40,5 @@ this[ø.resolve] = resolve; | ||
} else { | ||
throw new Error('Invalid executor type'); | ||
const error = new Error(`Invalid executor type: ${executor === null ? 'null' : typeof executor}`); | ||
error.name = ERRORS.InstantiationError; | ||
throw error; | ||
} | ||
@@ -49,3 +58,10 @@ } | ||
if (this[ø.executor]) { | ||
this[ø.executor](this.resolve.bind(this), this.reject.bind(this), ...args); | ||
try { | ||
this[ø.executor](this.resolve.bind(this), this.reject.bind(this), ...args); | ||
} catch (cause) { | ||
const error = new Error('Promise execution failed. See "cause" property for details'); | ||
error.cause = cause; | ||
error.name = ERRORS.ExecutionError; | ||
this.reject(error); | ||
} | ||
this[ø.executor] = undefined; | ||
@@ -77,2 +93,4 @@ } | ||
Object.assign(ExtendablePromise, ERRORS); | ||
// https://stackoverflow.com/a/60328122 | ||
@@ -79,0 +97,0 @@ Object.defineProperty(ExtendablePromise, Symbol.species, { get: () => Promise }); |
@@ -8,3 +8,3 @@ /* eslint-disable max-classes-per-file */ | ||
describe(`ExtendablePromise`, () => { | ||
describe('ExtendablePromise', () => { | ||
let executorFunc; | ||
@@ -15,2 +15,3 @@ let promise; | ||
promise = new ExtendablePromise(executorFunc); | ||
jest.resetAllMocks(); | ||
}); | ||
@@ -33,7 +34,28 @@ | ||
describe('when an invalid executor is passed', () => { | ||
test('should throw an error', () => { | ||
expect(() => { | ||
promise = new ExtendablePromise(); | ||
}).toThrow('Invalid executor type'); | ||
describe('when null is passed', () => { | ||
test('should throw a sync error', () => { | ||
expect.assertions(3); | ||
promise = undefined; | ||
try { | ||
promise = new ExtendablePromise(null); | ||
} catch (error) { | ||
expect(error.name).toEqual('ExtendablePromise|InstantiationError'); | ||
expect(error.message).toEqual('Invalid executor type: null'); | ||
} | ||
expect(promise).toBeUndefined(); | ||
}); | ||
}); | ||
describe('when invalid type is passed', () => { | ||
test('should throw a sync error', () => { | ||
expect.assertions(3); | ||
promise = undefined; | ||
try { | ||
promise = new ExtendablePromise(123); | ||
} catch (error) { | ||
expect(error.name).toEqual('ExtendablePromise|InstantiationError'); | ||
expect(error.message).toEqual('Invalid executor type: number'); | ||
} | ||
expect(promise).toBeUndefined(); | ||
}); | ||
}); | ||
}); | ||
@@ -59,2 +81,36 @@ }); | ||
}); | ||
describe('when executed with error', () => { | ||
test('should reject the promise', async () => { | ||
expect.assertions(3); | ||
expect(executorFunc).not.toHaveBeenCalled(); | ||
executorFunc.mockImplementation(() => { | ||
throw new Error('Executor error'); | ||
}); | ||
promise.execute(); | ||
await expect(promise).rejects.toThrow('Promise execution failed. See "cause" property for details'); | ||
expect(executorFunc).toHaveBeenCalledWith(expect.any(Function), expect.any(Function)); | ||
}); | ||
describe('try/catch', () => { | ||
test('should reject the promise', async () => { | ||
expect.assertions(7); | ||
expect(executorFunc).not.toHaveBeenCalled(); | ||
executorFunc.mockImplementation(() => { | ||
throw new Error('Executor error'); | ||
}); | ||
let result; | ||
try { | ||
result = await promise.execute(); | ||
} catch (error) { | ||
expect(error.name).toEqual('ExtendablePromise|ExecutionError'); | ||
expect(error.message).toEqual('Promise execution failed. See "cause" property for details'); | ||
expect(error.cause).toEqual(expect.any(Error)); | ||
expect(error.cause.message).toEqual('Executor error'); | ||
} | ||
expect(result).toBeUndefined(); | ||
expect(executorFunc).toHaveBeenCalledWith(expect.any(Function), expect.any(Function)); | ||
}); | ||
}); | ||
}); | ||
}); | ||
@@ -61,0 +117,0 @@ |
{ | ||
"name": "@js-bits/xpromise", | ||
"version": "0.2.1", | ||
"version": "0.2.2", | ||
"description": "Extendable Promise", | ||
@@ -28,5 +28,3 @@ "keywords": [ | ||
"lint": "eslint '**/*.{js,jsx}'", | ||
"husky:pre-commit": "npx husky add .husky/pre-commit \"yarn build\" && npx husky add .husky/pre-commit \"git add dist/**.*\" && git add .husky/pre-commit", | ||
"husky:pre-push": "npx husky add .husky/pre-push \"yarn lint\" && npx husky add .husky/pre-push \"yarn test\" && git add .husky/pre-push", | ||
"husky:init": "npx husky install && yarn husky:pre-commit && yarn husky:pre-push" | ||
"prepare": "husky install" | ||
}, | ||
@@ -40,6 +38,6 @@ "repository": { | ||
"devDependencies": { | ||
"@js-bits/formalinter": "^0.1.2", | ||
"@js-bits/formalinter": "^0.3.1", | ||
"@js-bits/log-in-color": "^0.3.1", | ||
"@types/jest": "^26.0.22", | ||
"husky": "^7.0.1", | ||
"husky": "^8.0.1", | ||
"jest": "^26.6.3", | ||
@@ -68,4 +66,4 @@ "rollup": "^2.55.1" | ||
"dependencies": { | ||
"@js-bits/enumerate": "^0.5.1" | ||
"@js-bits/enumerate": "^0.8.0" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15814
408
+ Added@js-bits/enumerate@0.8.4(transitive)
- Removed@js-bits/enumerate@0.5.2(transitive)
Updated@js-bits/enumerate@^0.8.0