@js-bits/xpromise
Advanced tools
Comparing version 0.2.2 to 0.3.0
@@ -74,3 +74,3 @@ import enumerate from '@js-bits/enumerate'; | ||
this[ø.resolve](...args); | ||
// return this; // don't do this | ||
return this; | ||
} | ||
@@ -84,5 +84,3 @@ | ||
this[ø.reject](...args); | ||
// returning anything can lead to a subsequent exceptions | ||
// for cases like promise.catch(xpromise.reject.bind(xpromise)) | ||
// return this; // don't do this | ||
return this; | ||
} | ||
@@ -89,0 +87,0 @@ } |
@@ -58,2 +58,46 @@ /* eslint-disable max-classes-per-file */ | ||
}); | ||
describe('when extended', () => { | ||
describe('when rejected in a constructor', () => { | ||
test('should throw an async error', async () => { | ||
expect.assertions(3); | ||
class MyPromise extends ExtendablePromise { | ||
constructor() { | ||
super((resolve, reject) => { | ||
reject('async error'); | ||
}); | ||
this.execute(); | ||
} | ||
} | ||
promise = undefined; | ||
let result = 'unchanged'; | ||
try { | ||
promise = new MyPromise(); | ||
result = await promise; | ||
} catch (error) { | ||
expect(error).toEqual('async error'); | ||
} | ||
expect(promise).toEqual(expect.any(ExtendablePromise)); | ||
expect(result).toEqual('unchanged'); | ||
}); | ||
}); | ||
describe('when resolved in a constructor', () => { | ||
test('should return resolved value', async () => { | ||
expect.assertions(2); | ||
class MyPromise extends ExtendablePromise { | ||
constructor() { | ||
super(resolve => { | ||
resolve('async value'); | ||
}); | ||
this.execute(); | ||
} | ||
} | ||
promise = undefined; | ||
promise = new MyPromise(); | ||
const result = await promise; | ||
expect(promise).toEqual(expect.any(MyPromise)); | ||
expect(result).toEqual('async value'); | ||
}); | ||
}); | ||
}); | ||
}); | ||
@@ -139,5 +183,15 @@ }); | ||
}); | ||
test('should return nothing', () => { | ||
expect(promise.resolve(123)).toBeUndefined(); | ||
test('should return promise instance', () => { | ||
expect(promise.resolve(123)).toBe(promise); | ||
}); | ||
describe('when bound to another promise', () => { | ||
test('should return resolved promise', async () => { | ||
expect.assertions(3); | ||
const anotherPromise = Promise.resolve(234); | ||
const result = await anotherPromise.then(promise.resolve.bind(promise)); | ||
await expect(anotherPromise).resolves.toEqual(234); | ||
await expect(promise).resolves.toEqual(234); | ||
expect(result).toEqual(234); | ||
}); | ||
}); | ||
}); | ||
@@ -168,6 +222,30 @@ | ||
}); | ||
test('should return nothing', () => { | ||
promise.catch(() => {}); | ||
expect(promise.reject(123)).toBeUndefined(); | ||
test('should return promise instance', async () => { | ||
expect.assertions(2); | ||
expect(promise.reject('async error')).toBe(promise); | ||
try { | ||
await promise; | ||
} catch (error) { | ||
expect(error).toEqual('async error'); | ||
} | ||
}); | ||
describe('when bound to another promise', () => { | ||
test('should return rejected promise', async () => { | ||
expect.assertions(4); | ||
let reject; | ||
const anotherPromise = new Promise((...args) => { | ||
[, reject] = args; | ||
}); | ||
reject('error'); | ||
let result; | ||
try { | ||
result = await anotherPromise.catch(promise.reject.bind(promise)); | ||
} catch (reason) { | ||
expect(reason).toEqual('error'); | ||
} | ||
await expect(anotherPromise).rejects.toEqual('error'); | ||
await expect(promise).rejects.toEqual('error'); | ||
expect(result).toBeUndefined(); | ||
}); | ||
}); | ||
}); | ||
@@ -244,1 +322,54 @@ | ||
}); | ||
describe('Promise', () => { | ||
describe('immediately resoled', () => { | ||
test('should return async value', async () => { | ||
expect.assertions(1); | ||
const promise = new Promise(resolve => { | ||
resolve(12345); | ||
}); | ||
const result = await promise; | ||
expect(result).toEqual(12345); | ||
}); | ||
}); | ||
describe('immediately rejected', () => { | ||
test('should throw an error', async () => { | ||
expect.assertions(3); | ||
const promise = new Promise((resolve, reject) => { | ||
// eslint-disable-next-line prefer-promise-reject-errors | ||
reject('async error'); | ||
}); | ||
let result = 'unchanged'; | ||
try { | ||
result = await promise; | ||
} catch (error) { | ||
expect(error).toEqual('async error'); | ||
} | ||
expect(promise).toBeInstanceOf(Promise); | ||
expect(result).toEqual('unchanged'); | ||
}); | ||
}); | ||
describe('Promise.resolve', () => { | ||
test('should return async value', async () => { | ||
expect.assertions(1); | ||
const promise = Promise.resolve(9876); | ||
const result = await promise; | ||
expect(result).toEqual(9876); | ||
}); | ||
}); | ||
describe('Promise.reject', () => { | ||
test('should throw an error', async () => { | ||
expect.assertions(3); | ||
// eslint-disable-next-line prefer-promise-reject-errors | ||
const promise = Promise.reject('async error'); | ||
let result = 'unchanged'; | ||
try { | ||
result = await promise; | ||
} catch (error) { | ||
expect(error).toEqual('async error'); | ||
} | ||
expect(promise).toBeInstanceOf(Promise); | ||
expect(result).toEqual('unchanged'); | ||
}); | ||
}); | ||
}); |
{ | ||
"name": "@js-bits/xpromise", | ||
"version": "0.2.2", | ||
"version": "0.3.0", | ||
"description": "Extendable Promise", | ||
@@ -25,3 +25,2 @@ "keywords": [ | ||
"test-cjs": "jest --verbose", | ||
"test-coverage": "yarn test --coverage", | ||
"test-watch": "yarn test --watch", | ||
@@ -40,6 +39,7 @@ "lint": "eslint '**/*.{js,jsx}'", | ||
"@js-bits/log-in-color": "^0.3.1", | ||
"@types/jest": "^26.0.22", | ||
"@types/jest": "^29.1.1", | ||
"husky": "^8.0.1", | ||
"jest": "^26.6.3", | ||
"rollup": "^2.55.1" | ||
"jest": "^29.1.2", | ||
"jest-environment-jsdom": "^29.1.2", | ||
"rollup": "^2.79.1" | ||
}, | ||
@@ -46,0 +46,0 @@ "engines": { |
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
20881
11
564
7