makepromise
Advanced tools
Comparing version 1.1.1 to 2.0.0
module.exports = { | ||
"env": { | ||
"es6": true, | ||
"node": true | ||
}, | ||
"extends": "eslint:recommended", | ||
"parserOptions": { | ||
"ecmaVersion": 2017, | ||
"sourceType": "module" | ||
}, | ||
"rules": { | ||
"indent": [ | ||
"error", | ||
4 | ||
], | ||
"linebreak-style": [ | ||
"error", | ||
"unix" | ||
], | ||
"quotes": [ | ||
"error", | ||
"single" | ||
], | ||
"semi": [ | ||
"error", | ||
"never" | ||
], | ||
"comma-dangle": [ | ||
"error", | ||
"always-multiline" | ||
], | ||
"quote-props": [ | ||
"error", | ||
"as-needed" | ||
], | ||
"padded-blocks": [ | ||
"error", | ||
"never" | ||
] | ||
} | ||
"env": { | ||
"es6": true, | ||
"node": true | ||
}, | ||
"extends": "eslint:recommended", | ||
"parserOptions": { | ||
"ecmaVersion": 2018, | ||
"sourceType": "module" | ||
}, | ||
"rules": { | ||
"indent": [ | ||
"error", | ||
2 | ||
], | ||
"linebreak-style": [ | ||
"error", | ||
"unix" | ||
], | ||
"quotes": [ | ||
"error", | ||
"single" | ||
], | ||
"semi": [ | ||
"error", | ||
"never" | ||
], | ||
"comma-dangle": [ | ||
"error", | ||
"always-multiline" | ||
], | ||
"quote-props": [ | ||
"error", | ||
"as-needed" | ||
], | ||
"padded-blocks": [ | ||
"error", | ||
"never" | ||
] | ||
} | ||
}; |
@@ -0,1 +1,8 @@ | ||
## 12 May 2018 | ||
### 2.0.0 | ||
- [ecma] using modules therefore major up. `require('makepromise')` now returns `{ default }` | ||
- [adc] move to [Art Deco Code](https://artdeco.bz) | ||
## 29 December 2017 | ||
@@ -2,0 +9,0 @@ |
@@ -1,10 +0,10 @@ | ||
const fs = require('fs') | ||
const makePromise = require('..'); | ||
import { unlink } from 'fs' | ||
import makePromise from '..' | ||
(async () => { | ||
try { | ||
await makePromise(fs.unlink, 'error-test-file') | ||
} catch ({ stack }) { | ||
console.log(stack) | ||
} | ||
try { | ||
await makePromise(unlink, 'error-test-file') | ||
} catch ({ stack }) { | ||
console.log(stack) | ||
} | ||
})() |
@@ -1,4 +0,4 @@ | ||
const { unlink } = require('fs') | ||
const { createWritable } = require('wrote') | ||
const makePromise = require('../') | ||
import { unlink } from 'fs' | ||
import { createWritable } from 'wrote' | ||
import makePromise from '..' | ||
@@ -9,21 +9,21 @@ // here, just close the stream without makepromise, because it's a different | ||
async function closeWritable(ws) { | ||
await new Promise((resolve, reject) => { | ||
ws.once('close', resolve) | ||
ws.once('error', reject) | ||
ws.close() | ||
}) | ||
await new Promise((resolve, reject) => { | ||
ws.once('close', resolve) | ||
ws.once('error', reject) | ||
ws.close() | ||
}) | ||
} | ||
(async () => { | ||
// create a temp file and open a writable stream | ||
const ws = await createWritable() | ||
await closeWritable(ws) | ||
// create a temp file and open a writable stream | ||
const ws = await createWritable() | ||
await closeWritable(ws) | ||
const { path: file } = ws | ||
const { path: file } = ws | ||
console.log(file) // /var/folders/s0/tmp/T/wrote-69822.data | ||
console.log(file) // /var/folders/s0/tmp/T/wrote-69822.data | ||
// pass 3rd argument as the return value | ||
const resolvedFile = await makePromise(unlink, file, file) | ||
console.log(resolvedFile === file) // true | ||
// pass 3rd argument as the return value | ||
const resolvedFile = await makePromise(unlink, file, file) | ||
console.log(resolvedFile === file) // true | ||
})() |
@@ -1,4 +0,4 @@ | ||
const { unlink } = require('fs') | ||
const { createWritable } = require('wrote') | ||
const makePromise = require('../') | ||
import { unlink } from 'fs' | ||
import { createWritable } from 'wrote' | ||
import makePromise from '..' | ||
@@ -9,19 +9,19 @@ // here, just close the stream without makepromise, because it's a different | ||
async function closeWritable(ws) { | ||
await new Promise((resolve, reject) => { | ||
ws.once('close', resolve) | ||
ws.once('error', reject) | ||
ws.close() | ||
}) | ||
await new Promise((resolve, reject) => { | ||
ws.once('close', resolve) | ||
ws.once('error', reject) | ||
ws.close() | ||
}) | ||
} | ||
(async () => { | ||
// create a temp file and open a writable stream | ||
const ws = await createWritable() | ||
await closeWritable(ws) | ||
// create a temp file and open a writable stream | ||
const ws = await createWritable() | ||
await closeWritable(ws) | ||
const { path: file } = ws | ||
const { path: file } = ws | ||
console.log(file) // /var/folders/s0/tmp/T/wrote-69822.data | ||
console.log(file) // /var/folders/s0/tmp/T/wrote-69822.data | ||
await makePromise(unlink, file) | ||
await makePromise(unlink, file) | ||
})() |
{ | ||
"name": "makepromise", | ||
"version": "1.1.1", | ||
"version": "2.0.0", | ||
"description": "Make a native Promise from a function with a callback", | ||
"main": "src/index", | ||
"main": "build", | ||
"scripts": { | ||
"test": "zoroaster test/spec", | ||
"teb": "LIB_MAIN=../../es5 zoroaster --babel", | ||
"t": "zoroaster --babel", | ||
"test": "zoroaster test/spec --babel", | ||
"test-build": "LIB_MAIN=../../build zoroaster test/spec --babel", | ||
"test-es5-bin": "LIB_MAIN=../../es5 zoroaster test/spec --babel", | ||
"test-es5": "zoroaster es5/test/spec", | ||
"build-src": "babel src --out-dir es5/src", | ||
"build-test": "babel test --out-dir es5/test", | ||
"build": "run-s build-src build-test" | ||
"build": "babel src --out-dir build", | ||
"e": "node examples/run" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/Sobesednik/makepromise.git" | ||
"url": "git+https://github.com/artdecocode/makepromise.git" | ||
}, | ||
@@ -24,22 +27,22 @@ "keywords": [ | ||
], | ||
"author": "Anton <anton@sobesednik.media>", | ||
"author": "Anton <anton@adc.sh>", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/Sobesednik/makepromise/issues" | ||
"url": "https://github.com/artdecocode/makepromise/issues" | ||
}, | ||
"homepage": "https://github.com/Sobesednik/makepromise#readme", | ||
"homepage": "https://github.com/artdecocode/makepromise#readme", | ||
"devDependencies": { | ||
"babel-cli": "6.26.0", | ||
"babel-plugin-transform-rename-import": "2.1.1", | ||
"babel-preset-env": "1.6.1", | ||
"catchment": "1.0.0", | ||
"fast-async": "6.3.0", | ||
"lockfile": "1.0.3", | ||
"npm-run-all": "4.1.2", | ||
"wrote": "1.0.0", | ||
"zoroaster": "0.4.6" | ||
"@babel/cli": "7.0.0-beta.46", | ||
"@babel/core": "7.0.0-beta.46", | ||
"@babel/plugin-syntax-object-rest-spread": "7.0.0-beta.46", | ||
"@babel/plugin-transform-modules-commonjs": "7.0.0-beta.46", | ||
"@babel/register": "7.0.0-beta.46", | ||
"catchment": "2.0.1", | ||
"lockfile": "1.0.4", | ||
"wrote": "1.4.0", | ||
"zoroaster": "1.1.0" | ||
}, | ||
"dependencies": { | ||
"erotic": "0.2.0" | ||
"erotic": "1.0.1" | ||
} | ||
} |
122
README.md
@@ -5,13 +5,4 @@ # makepromise | ||
Make native `Promise` from function with callback. | ||
Make native `Promise` from a function with a callback. | ||
## ES5 | ||
The package uses some newer language features. For your convenience, it's been | ||
transpiled to be compatible with Node 4. You can use the following snippet. | ||
```js | ||
const makePromise = require('makepromise/es5/src/') | ||
``` | ||
## `makePromise(fn:Function(...args, cb:Function(err:Error, res:any))) => Promise<any>` | ||
@@ -26,21 +17,19 @@ | ||
```js | ||
const { unlink } = require('fs') | ||
const { createWritable } = require('wrote') | ||
const makePromise = require('makepromise') | ||
import { unlink } from 'fs' | ||
import { createWritable } from 'wrote' | ||
import makePromise from 'makepromise' | ||
let file | ||
(async () => { | ||
// create a temp file and open a writable stream | ||
const ws = await createWritable() | ||
const { path: file } = ws // /var/folders/s0/tmp/T/wrote-69822.data | ||
// here, just close the stream without makepromise, because it's a different | ||
// kind of constructor, ie. resolve-reject and not error callback handlers | ||
await new Promise((resolve, reject) => { | ||
ws.once('close', resolve) | ||
ws.once('error', reject) | ||
ws.close() | ||
}) | ||
await makePromise(unlink, file) | ||
// create a temp file and open a writable stream | ||
const ws = await createWritable() | ||
const { path: file } = ws // /var/folders/s0/tmp/T/wrote-69822.data | ||
// here, just close the stream without makepromise, because it's a different | ||
// kind of constructor, ie. resolve-reject and not error callback handlers | ||
await new Promise((resolve, reject) => { | ||
ws.once('close', resolve) | ||
ws.once('error', reject) | ||
ws.close() | ||
}) | ||
await makePromise(unlink, file) | ||
})() | ||
@@ -54,22 +43,20 @@ ``` | ||
```js | ||
const fs = require('fs') | ||
const makePromise = require('makepromise'); | ||
import { unlink } from 'fs' | ||
import makePromise from 'makepromise' | ||
(async () => { | ||
try { | ||
await makePromise(fs.unlink, 'error-test-file') | ||
} catch ({ stack }){ | ||
console.log(stack) | ||
/* | ||
Error: ENOENT: no such file or directory, unlink 'error-test-file' | ||
at cb (makepromise/src/index.js:28:31) | ||
at makePromise (makepromise/src/index.js:18:16) | ||
at Object.<anonymous> (makepromise/examples/error-stack.js:4:1) | ||
*/ | ||
// without this feature: | ||
/* | ||
Error: ENOENT: no such file or directory, unlink 'error-test-file' | ||
*/ | ||
} | ||
try { | ||
await makePromise(fs.unlink, 'error-test-file') | ||
} catch ({ stack }){ | ||
console.log(stack) | ||
/* | ||
Error: ENOENT: no such file or directory, unlink 'error-test-file' | ||
at cb (makepromise/src/index.js:28:31) | ||
at makePromise (makepromise/src/index.js:18:16) | ||
at Object.<anonymous> (makepromise/examples/error-stack.js:4:1) | ||
*/ // without this feature: | ||
/* | ||
Error: ENOENT: no such file or directory, unlink 'error-test-file' | ||
*/ | ||
} | ||
})() | ||
@@ -83,18 +70,15 @@ ``` | ||
```js | ||
const { unlink } = require('fs') | ||
const { createWritable } = require('wrote') | ||
const makePromise = require('makepromise'); | ||
import { unlink } from 'fs' | ||
import { createWritable } from 'wrote' | ||
import makePromise from 'makepromise' | ||
(async () => { | ||
// create a temp file and open a writable stream | ||
const ws = await createWritable() | ||
await closeWritable(ws) | ||
const { path: file } = ws | ||
console.log(file) // /var/folders/s0/tmp/T/wrote-69822.data | ||
// pass 3rd argument as the return value | ||
const resolvedFile = await makePromise(unlink, file, file) | ||
console.log(resolvedFile === file) // true | ||
// create a temp file and open a writable stream | ||
const ws = await createWritable() | ||
await closeWritable(ws) | ||
const { path: file } = ws | ||
console.log(file) // /var/folders/s0/tmp/T/wrote-69822.data | ||
// pass 3rd argument as the return value | ||
const resolvedFile = await makePromise(unlink, file, file) | ||
console.log(resolvedFile === file) // true | ||
})() | ||
@@ -107,9 +91,19 @@ ``` | ||
* How to [unlock a file with](https://github.com/Sobesednik/makepromise/blob/master/test/spec/integration.js#L12) | ||
* How to [unlock a file with](https://github.com/artdecocode/makepromise/blob/master/test/spec/integration.js#L12) | ||
[`lockfile` package][2] | ||
* [How to write to a writable stream](https://github.com/Sobesednik/makepromise/blob/master/test/spec/integration.js#L29) | ||
* How to [unlink a file with `fs.unlink`](https://github.com/Sobesednik/makepromise/blob/master/test/spec/integration.js#L38) | ||
* How to [read stat with `fs.stat`](https://github.com/Sobesednik/makepromise/blob/master/test/spec/integration.js#L58) | ||
* How to catch errors after [call `fs.stat` with non-existent file](https://github.com/Sobesednik/makepromise/blob/master/test/spec/integration.js#L66)) | ||
* [How to write to a writable stream](https://github.com/artdecocode/makepromise/blob/master/test/spec/integration.js#L29) | ||
* How to [unlink a file with `fs.unlink`](https://github.com/artdecocode/makepromise/blob/master/test/spec/integration.js#L38) | ||
* How to [read stat with `fs.stat`](https://github.com/artdecocode/makepromise/blob/master/test/spec/integration.js#L58) | ||
* How to catch errors after [call `fs.stat` with non-existent file](https://github.com/artdecocode/makepromise/blob/master/test/spec/integration.js#L66)) | ||
## ES5 | ||
> DO NOT USE THIS. USE NEWER NODE.JS | ||
The package uses some newer language features. For your convenience, it's been | ||
transpiled to be compatible with Node 4. You can use the following snippet. | ||
```js | ||
const makePromise = require('makepromise/es5') | ||
``` | ||
--- | ||
@@ -119,5 +113,5 @@ | ||
*(c) [Sobesednik-Media](https://sobesednik.media) 2017* | ||
*(c) [Art Deco Code](https://artdeco.bz) 2018* | ||
[1]: https://www.npmjs.com/package/wrote | ||
[2]: https://www.npmjs.com/package/lockfile |
@@ -1,7 +0,7 @@ | ||
const erotic = require('erotic') | ||
import erotic from 'erotic' | ||
function checkArgumentIndex(length, i) { | ||
if (i > length - 2) { | ||
throw new Error('Function does not accept that many arguments') | ||
} | ||
if (i > length - 2) { | ||
throw new Error('Function does not accept that many arguments') | ||
} | ||
} | ||
@@ -17,37 +17,36 @@ | ||
*/ | ||
async function makePromise(fn, args, resolveValue) { | ||
const er = erotic() | ||
if (typeof fn !== 'function') { | ||
throw new Error('function must be passed') | ||
export default async function makePromise(fn, args, resolveValue) { | ||
const er = erotic() | ||
if (typeof fn !== 'function') { | ||
throw new Error('function must be passed') | ||
} | ||
const { length: fnLength } = fn | ||
if (!fnLength) { | ||
throw new Error('Function does not accept any arguments') | ||
} | ||
const res = await new Promise((resolve, reject)=> { | ||
const cb = (err, res) => { | ||
if (err) { | ||
const error = er(err) | ||
return reject(error) | ||
} | ||
return resolve(resolveValue || res) | ||
} | ||
const { length: fnLength } = fn | ||
if (!fnLength) { | ||
throw new Error('Function does not accept any arguments') | ||
} | ||
const res = await new Promise((resolve, reject)=> { | ||
const cb = (err, res) => { | ||
if (err) { | ||
const error = er(err) | ||
return reject(error) | ||
} | ||
return resolve(resolveValue || res) | ||
} | ||
let allArgs = [cb] | ||
let allArgs = [cb] | ||
if (Array.isArray(args)) { | ||
args.forEach((arg, i) => { | ||
checkArgumentIndex(fnLength, i) | ||
}) | ||
allArgs = [...args, cb] | ||
} else if (Array.from(arguments).length > 1) { // args passed as a single argument, not array | ||
checkArgumentIndex(fnLength, 0) | ||
allArgs = [args, cb] | ||
} | ||
fn.apply(fn.this, allArgs) | ||
}) | ||
return res | ||
if (Array.isArray(args)) { | ||
args.forEach((arg, i) => { | ||
checkArgumentIndex(fnLength, i) | ||
}) | ||
allArgs = [...args, cb] | ||
} else if (Array.from(arguments).length > 1) { // args passed as a single argument, not array | ||
checkArgumentIndex(fnLength, 0) | ||
allArgs = [args, cb] | ||
} | ||
fn.apply(fn.this, allArgs) | ||
}) | ||
return res | ||
} | ||
module.exports = makePromise |
@@ -1,73 +0,72 @@ | ||
const { equal, deepEqual } = require('assert') | ||
const makePromise = require('../..') | ||
/* global makePromise */ | ||
import { equal, deepEqual } from 'assert' | ||
const argCheck = { | ||
async 'function does not accept arguments'() { | ||
const fn = () => {} | ||
try { | ||
await makePromise(fn) | ||
throw new Error('should have thrown an error') | ||
} catch ({ message }) { | ||
equal(message, 'Function does not accept any arguments') | ||
} | ||
}, | ||
async 'passing arguments when none allowed'() { | ||
const fn = cb => cb() | ||
try { | ||
await makePromise(fn, 'hello-world') | ||
throw new Error('should have thrown an error') | ||
} catch ({ message }) { | ||
equal(message, 'Function does not accept that many arguments') | ||
} | ||
}, | ||
async 'passing more arguments when allowed'() { | ||
const fn = (test, cb) => { cb(null, test) } | ||
try { | ||
await makePromise(fn, ['hello world', 'extra argument']) | ||
throw new Error('should have thrown an error') | ||
} catch ({ message, stack }) { | ||
equal(message, 'Function does not accept that many arguments') | ||
} | ||
}, | ||
async 'function does not accept arguments'() { | ||
const fn = () => {} | ||
try { | ||
await makePromise(fn) | ||
throw new Error('should have thrown an error') | ||
} catch ({ message }) { | ||
equal(message, 'Function does not accept any arguments') | ||
} | ||
}, | ||
async 'passing arguments when none allowed'() { | ||
const fn = cb => cb() | ||
try { | ||
await makePromise(fn, 'hello-world') | ||
throw new Error('should have thrown an error') | ||
} catch ({ message }) { | ||
equal(message, 'Function does not accept that many arguments') | ||
} | ||
}, | ||
async 'passing more arguments when allowed'() { | ||
const fn = (test, cb) => { cb(null, test) } | ||
try { | ||
await makePromise(fn, ['hello world', 'extra argument']) | ||
throw new Error('should have thrown an error') | ||
} catch ({ message, stack }) { | ||
equal(message, 'Function does not accept that many arguments') | ||
} | ||
}, | ||
} | ||
const argumentCheckTestSuite = { | ||
'should return a rejected promise when': argCheck, | ||
async 'should call function with correct arguments'() { | ||
const testArgs = ['test-arg-1', undefined, 'test-arg-2'] | ||
let args | ||
const fn = function TestFn(testArgA, testArgB, testArgC, cb) { | ||
args = arguments | ||
cb(null) | ||
} | ||
await makePromise(fn, testArgs) | ||
const argArray = Array.from(args) | ||
const realArgs = argArray.splice(0, argArray.length - 1) | ||
deepEqual(realArgs, testArgs) | ||
}, | ||
async 'should call function with correct argument'() { | ||
const testArg = 'test-arg-1' | ||
let args | ||
const fn = function TestFn(testArgA, cb) { | ||
args = arguments | ||
cb(null) | ||
} | ||
await makePromise(fn, testArg) | ||
const argArray = Array.from(args) | ||
equal(argArray.length, 2) | ||
equal(argArray[0], testArg) | ||
}, | ||
async 'should call function with correct undefined argument'() { | ||
const testArg = undefined | ||
let args | ||
const fn = function TestFn(testArgA, cb) { | ||
args = arguments | ||
cb(null) | ||
} | ||
await makePromise(fn, testArg) | ||
const argArray = Array.from(args) | ||
equal(argArray.length, 2) | ||
equal(argArray[0], testArg) | ||
}, | ||
export const argumentCheckTestSuite = { | ||
'returns a rejected promise when': argCheck, | ||
async 'should call function with correct arguments'() { | ||
const testArgs = ['test-arg-1', undefined, 'test-arg-2'] | ||
let args | ||
const fn = function TestFn(testArgA, testArgB, testArgC, cb) { | ||
args = arguments | ||
cb(null) | ||
} | ||
await makePromise(fn, testArgs) | ||
const argArray = Array.from(args) | ||
const realArgs = argArray.splice(0, argArray.length - 1) | ||
deepEqual(realArgs, testArgs) | ||
}, | ||
async 'should call function with correct argument'() { | ||
const testArg = 'test-arg-1' | ||
let args | ||
const fn = function TestFn(testArgA, cb) { | ||
args = arguments | ||
cb(null) | ||
} | ||
await makePromise(fn, testArg) | ||
const argArray = Array.from(args) | ||
equal(argArray.length, 2) | ||
equal(argArray[0], testArg) | ||
}, | ||
async 'should call function with correct undefined argument'() { | ||
const testArg = undefined | ||
let args | ||
const fn = function TestFn(testArgA, cb) { | ||
args = arguments | ||
cb(null) | ||
} | ||
await makePromise(fn, testArg) | ||
const argArray = Array.from(args) | ||
equal(argArray.length, 2) | ||
equal(argArray[0], testArg) | ||
}, | ||
} | ||
module.exports = argumentCheckTestSuite |
@@ -1,13 +0,13 @@ | ||
const context = require('../context/MakePromise') | ||
const makePromise = require('../..') | ||
/* global makePromise */ | ||
import context from '../context' | ||
const boundFunctionsTestSuite = { | ||
context, | ||
async 'should resolve with function result'({ getWs }) { | ||
const ws = await getWs() | ||
const boundFn = ws.close.bind(ws) | ||
await makePromise(boundFn) | ||
}, | ||
const T = { | ||
context, | ||
async 'should resolve with function result'({ getWs }) { | ||
const ws = await getWs() | ||
const boundFn = ws.close.bind(ws) | ||
await makePromise(boundFn) | ||
}, | ||
} | ||
module.exports = boundFunctionsTestSuite | ||
export default T |
@@ -1,59 +0,57 @@ | ||
const assert = require('assert') | ||
const context = require('../context/MakePromise') | ||
const makePromise = require('../..') | ||
/* global makePromise */ | ||
import { equal, ok } from 'assert' | ||
import context from '../context' | ||
const { equal } = assert | ||
const makePromiseTestSuite = { | ||
context, | ||
async 'should create a promise'({ nullErrFn }) { | ||
const res =makePromise(nullErrFn) | ||
assert(res instanceof Promise) | ||
try { | ||
await res | ||
} catch (err) { /* test-error */ } | ||
}, | ||
async 'should resolve with undefined'({ nullErrFn }) { | ||
const res = await makePromise(nullErrFn) | ||
equal(res, undefined) | ||
}, | ||
async 'should resolve with function result'({ nullErrFn, testArg }) { | ||
const res = await makePromise(nullErrFn, testArg) | ||
equal(res, testArg) | ||
}, | ||
async 'should reject if not a function'() { | ||
const notAFunction = 'this is not a function' | ||
try { | ||
await makePromise(notAFunction) | ||
throw new Error('should have been rejected') | ||
} catch ({ message }) { | ||
assert(/function must be passed/.test(message)) | ||
} | ||
}, | ||
async 'should resolve with supplied argument'(ctx) { | ||
const testValue = 'test-value' | ||
const res = await makePromise(ctx.nullErrFn, ctx.testArg, testValue) | ||
equal(res, testValue) | ||
}, | ||
async 'should reject with error'({ errFn }) { | ||
const testError = new Error('test-error') | ||
try { | ||
await makePromise(errFn, testError) | ||
throw new Error('expected to have been rejected') | ||
} catch({ message }) { | ||
equal(message, testError.message) | ||
} | ||
}, | ||
async 'should reject with error with a correct stack'(ctx) { | ||
const testError = new Error('test-error') | ||
delete testError.stack | ||
try { | ||
await makePromise(ctx.errFn, testError) | ||
throw new Error('expected to have been rejected') | ||
} catch ({ stack }) { | ||
assert(stack.indexOf('should reject with error with a correct stack') != -1) | ||
} | ||
}, | ||
const T = { | ||
context, | ||
async 'should create a promise'({ nullErrFn }) { | ||
const res = makePromise(nullErrFn) | ||
ok(res instanceof Promise) | ||
try { | ||
await res | ||
} catch (err) { /* test-error */ } | ||
}, | ||
async 'should resolve with undefined'({ nullErrFn }) { | ||
const res = await makePromise(nullErrFn) | ||
equal(res, undefined) | ||
}, | ||
async 'should resolve with function result'({ nullErrFn, testArg }) { | ||
const res = await makePromise(nullErrFn, testArg) | ||
equal(res, testArg) | ||
}, | ||
async 'should reject if not a function'() { | ||
const notAFunction = 'this is not a function' | ||
try { | ||
await makePromise(notAFunction) | ||
throw new Error('should have been rejected') | ||
} catch ({ message }) { | ||
ok(/function must be passed/.test(message)) | ||
} | ||
}, | ||
async 'should resolve with supplied argument'(ctx) { | ||
const testValue = 'test-value' | ||
const res = await makePromise(ctx.nullErrFn, ctx.testArg, testValue) | ||
equal(res, testValue) | ||
}, | ||
async 'should reject with error'({ errFn }) { | ||
const testError = new Error('test-error') | ||
try { | ||
await makePromise(errFn, testError) | ||
throw new Error('expected to have been rejected') | ||
} catch({ message }) { | ||
equal(message, testError.message) | ||
} | ||
}, | ||
async 'should reject with error with a correct stack'(ctx) { | ||
const testError = new Error('test-error') | ||
delete testError.stack | ||
try { | ||
await makePromise(ctx.errFn, testError) | ||
throw new Error('expected to have been rejected') | ||
} catch ({ stack }) { | ||
ok(stack.indexOf('should reject with error with a correct stack') != -1) | ||
} | ||
}, | ||
} | ||
module.exports = makePromiseTestSuite | ||
export default T |
@@ -1,78 +0,76 @@ | ||
const assert = require('assert') | ||
const { stat, unlink, Stats } = require('fs') | ||
const { unlock } = require('lockfile') | ||
const { erase, read } = require('wrote') | ||
const context = require('../context/MakePromise') | ||
const makePromise = require('../..') | ||
/* global makePromise */ | ||
import { equal, ok } from 'assert' | ||
import { stat, unlink, Stats } from 'fs' | ||
import { unlock } from 'lockfile' | ||
import { erase, read } from 'wrote' | ||
import context from '../context' | ||
const { equal } = assert | ||
const T = { | ||
context, | ||
async 'should be able to unlock a file'({ getWs }) { | ||
const ws = await getWs() | ||
ok(ws.writable) | ||
await makePromise(stat, ws.path) | ||
await makePromise(unlock, ws.path) | ||
ok(!ws.closed) // stream not closed | ||
ok(!ws._writableState.ended) | ||
try { | ||
await makePromise(stat, ws.path) | ||
throw new Error('should have been rejected') | ||
} catch ({ code }) { | ||
ok(code, 'ENOENT') | ||
} | ||
await makePromise(ws.end.bind(ws)) | ||
ok(ws.closed) | ||
ok(ws._writableState.ended) | ||
}, | ||
async 'should write to a file stream'({ getWs }) { | ||
const testData = 'some-test-data' | ||
const ws = await getWs() | ||
await makePromise(ws.write.bind(ws), testData) | ||
const IntegrationTestSuite = { | ||
context, | ||
async 'should be able to unlock a file'({ getWs }) { | ||
const ws = await getWs() | ||
assert(ws.writable) | ||
await makePromise(stat, ws.path) | ||
await makePromise(unlock, ws.path) | ||
assert(!ws.closed) // stream not closed | ||
assert(!ws._writableState.ended) | ||
try { | ||
await makePromise(stat, ws.path) | ||
throw new Error('should have been rejected') | ||
} catch ({ code }) { | ||
assert(code, 'ENOENT') | ||
} | ||
await makePromise(ws.end.bind(ws)) | ||
assert(ws.closed) | ||
assert(ws._writableState.ended) | ||
}, | ||
async 'should write to a file stream'({ getWs }) { | ||
const testData = 'some-test-data' | ||
const ws = await getWs() | ||
await makePromise(ws.write.bind(ws), testData) | ||
// read file | ||
const actual = await read(ws.path) | ||
equal(actual, testData) | ||
}, | ||
async 'should unlink a file'({ getWs }) { | ||
const ws = await getWs() | ||
await new Promise((resolve, reject) => { | ||
ws.once('close', resolve) | ||
ws.once('error', reject) | ||
ws.close() | ||
}) | ||
await makePromise(unlink, ws.path) | ||
try { | ||
await read(ws.path) | ||
throw new Error('should have thrown') | ||
} catch ({ code }) { | ||
equal(code, 'ENOENT') | ||
} | ||
}, | ||
async 'should end stream'({ getWs }) { | ||
const ws = await getWs() | ||
await makePromise(ws.end.bind(ws)) | ||
ok(ws._writableState.ended) | ||
}, | ||
async 'should read stats'({ getWs }) { | ||
const ws = await getWs() | ||
ok(!ws._writableState.ended) | ||
const res = await makePromise(stat, ws.path) | ||
ok(res instanceof Stats) | ||
// read file | ||
const actual = await read(ws.path) | ||
equal(actual, testData) | ||
}, | ||
async 'should unlink a file'({ getWs }) { | ||
const ws = await getWs() | ||
await new Promise((resolve, reject) => { | ||
ws.once('close', resolve) | ||
ws.once('error', reject) | ||
ws.close() | ||
}) | ||
await makePromise(unlink, ws.path) | ||
try { | ||
await read(ws.path) | ||
throw new Error('should have thrown') | ||
} catch ({ code }) { | ||
equal(code, 'ENOENT') | ||
} | ||
}, | ||
async 'should end stream'({ getWs }) { | ||
const ws = await getWs() | ||
await makePromise(ws.end.bind(ws)) | ||
assert(ws._writableState.ended) | ||
}, | ||
async 'should read stats'({ getWs }) { | ||
const ws = await getWs() | ||
assert(!ws._writableState.ended) | ||
const res = await makePromise(stat, ws.path) | ||
assert(res instanceof Stats) | ||
await erase(ws) // move to context | ||
}, | ||
async 'should not read stats of non-existent file'({ getWs }) { | ||
const ws = await getWs() | ||
await erase(ws) | ||
try { | ||
await makePromise(stat, ws.path) | ||
throw new Error('should have been rejected') | ||
} catch ({ code }) { | ||
equal(code, 'ENOENT') | ||
} | ||
}, | ||
await erase(ws) // move to context | ||
}, | ||
async 'should not read stats of non-existent file'({ getWs }) { | ||
const ws = await getWs() | ||
await erase(ws) | ||
try { | ||
await makePromise(stat, ws.path) | ||
throw new Error('should have been rejected') | ||
} catch ({ code }) { | ||
equal(code, 'ENOENT') | ||
} | ||
}, | ||
} | ||
module.exports = IntegrationTestSuite | ||
export default T |
Sorry, the diff of this file is not supported yet
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
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
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
53058
27
1216
112
10
+ Addederotic@1.0.1(transitive)
- Removederotic@0.2.0(transitive)
Updatederotic@1.0.1