Skip to content

Commit

Permalink
fix: corrected utils.test.js tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nimish-gupta committed May 9, 2020
1 parent 5b426c9 commit e674bdf
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 11 deletions.
29 changes: 23 additions & 6 deletions src/__tests__/util.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
const test = require('ava');
const PassThroughStream = require('stream').PassThrough;
const getStream = require('get-stream');
const sinon = require('sinon');

const Util = require('../utils');

const successPromise = () => Promise.resolve('success');
const errorPromise = () => Promise.reject(new Error('fail'));

const processStub = sinon.stub();

test.before(() => {
sinon.replace(process, 'exit', processStub);
processStub.returnsArg(0);
});

test('promisify resolves', async (t) => {
const promise = await Util.promisify(successPromise());
t.deepEqual(promise, ['success', null]);
Expand All @@ -18,13 +26,18 @@ test('promisify rejects', async (t) => {
});

test('exitPromise resolves', async (t) => {
const promise = await Util.exitPromise(successPromise(), '');
const promise = await Util.exitPromise('', successPromise());
t.is(promise, 'success');
});

test('exitPromise rejects', async (t) => {
const promise = await Util.exitPromise(errorPromise(), 'test', (x) => x);
t.is(promise, 'Could not query repo for test due to, fail');
try {
const promise = await Util.exitPromise('test', errorPromise());
t.is(promise, 0);
t.is(processStub.calledOnce, true);
} catch (error) {
console.log(error);
}
});

test.beforeEach((t) => {
Expand All @@ -39,9 +52,9 @@ test.beforeEach((t) => {
test('spinnerPromise resolves', async (t) => {
const msg = 'test promise spinner';

const promise = await Util.spinnerPromise(successPromise(), msg, {
const promise = await Util.spinnerPromise(msg, {
stream: t.context.stream,
});
})(successPromise());
t.context.stream.end();
const output = await getStream(t.context.stream);

Expand All @@ -51,6 +64,10 @@ test('spinnerPromise resolves', async (t) => {

test('spinnerPromise reject', async (t) =>
await t.throwsAsync(
async () => Util.spinnerPromise(Promise.reject(new Error('fail')), ''),
async () => Util.spinnerPromise('')(Promise.reject(new Error('fail'))),
{ instanceOf: Error, message: 'fail' }
));

test.after(() => {
sinon.restore();
});
9 changes: 4 additions & 5 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,17 @@ const promisify = async (promise) => {
}
};

const exitPromise = (msg) => async (promise) => {
const exitPromise = R.curry(async (msg, promise) => {
const [result, error] = await promisify(promise);

if (error !== null) {
const errorMessage = `Could not query repo for ${msg} due to, ${error}`;
console.log('\n');
console.log(Log.error(errorMessage));
return onError(errorMessage);
console.log(Log.error(`Could not query repo for ${msg} due to, ${error}`));
return process.exit(0);
}

return result;
};
});

const spinnerPromise = (text, spinnerOptions = {}) => async (promise) => {
const spinner = ora({ text, ...spinnerOptions }).start();
Expand Down
91 changes: 91 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2182,6 +2182,13 @@ pump@^3.0.0:
end-of-stream "^1.1.0"
once "^1.3.1"

pupa@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/pupa/-/pupa-2.0.1.tgz#dbdc9ff48ffbea4a26a069b6f9f7abb051008726"
integrity sha512-hEJH0s8PXLY/cdXh66tNEQGndDrIKNqNC5xmrysZy3i5C3oEoLna7YAOad+7u125+zH1HNXUmGEkrhb3c2VriA==
dependencies:
escape-goat "^2.0.0"

ramda-fantasy@^0.8.0:
version "0.8.0"
resolved "https://registry.yarnpkg.com/ramda-fantasy/-/ramda-fantasy-0.8.0.tgz#9e8c37d93ec0a70796cfc10873dd9c50850390f6"
Expand All @@ -2194,6 +2201,90 @@ ramda@>=0.15.0, ramda@^0.27.0:
resolved "https://registry.yarnpkg.com/ramda/-/ramda-0.27.0.tgz#915dc29865c0800bf3f69b8fd6c279898b59de43"
integrity sha512-pVzZdDpWwWqEVVLshWUHjNwuVP7SfcmPraYuqocJp1yo2U1R7P+5QAfDhdItkuoGqIBnBYrtPp7rEPqDn9HlZA==

rc@^1.2.8:
version "1.2.8"
resolved "https://registry.yarnpkg.com/rc/-/rc-1.2.8.tgz#cd924bf5200a075b83c188cd6b9e211b7fc0d3ed"
integrity sha512-y3bGgqKj3QBdxLbLkomlohkvsA8gdAiUQlSBJnBhfn+BPxg4bc62d8TcBW15wavDfgexCgccckhcZvywyQYPOw==
dependencies:
deep-extend "^0.6.0"
ini "~1.3.0"
minimist "^1.2.0"
strip-json-comments "~2.0.1"

read-pkg@^5.2.0:
version "5.2.0"
resolved "https://registry.yarnpkg.com/read-pkg/-/read-pkg-5.2.0.tgz#7bf295438ca5a33e56cd30e053b34ee7250c93cc"
integrity sha512-Ug69mNOpfvKDAc2Q8DRpMjjzdtrnv9HcSMX+4VsZxD1aZ6ZzrIE7rlzXBtWTyhULSMKg076AW6WR5iZpD0JiOg==
dependencies:
"@types/normalize-package-data" "^2.4.0"
normalize-package-data "^2.5.0"
parse-json "^5.0.0"
type-fest "^0.6.0"

readdirp@~3.4.0:
version "3.4.0"
resolved "https://registry.yarnpkg.com/readdirp/-/readdirp-3.4.0.tgz#9fdccdf9e9155805449221ac645e8303ab5b9ada"
integrity sha512-0xe001vZBnJEK+uKcj8qOhyAKPzIT+gStxWr3LCB0DwcXR5NZJ3IaC+yGnHCYzB/S7ov3m3EEbZI2zeNvX+hGQ==
dependencies:
picomatch "^2.2.1"

registry-auth-token@^4.0.0:
version "4.1.1"
resolved "https://registry.yarnpkg.com/registry-auth-token/-/registry-auth-token-4.1.1.tgz#40a33be1e82539460f94328b0f7f0f84c16d9479"
integrity sha512-9bKS7nTl9+/A1s7tnPeGrUpRcVY+LUh7bfFgzpndALdPfXQBfQV77rQVtqgUV3ti4vc/Ik81Ex8UJDWDQ12zQA==
dependencies:
rc "^1.2.8"

registry-url@^5.0.0:
version "5.1.0"
resolved "https://registry.yarnpkg.com/registry-url/-/registry-url-5.1.0.tgz#e98334b50d5434b81136b44ec638d9c2009c5009"
integrity sha512-8acYXXTI0AkQv6RAOjE3vOaIXZkT9wo4LOFbBKYQEEnnMNBpKqdUrI6S4NT0KPIo/WVvJ5tE/X5LF/TQUf0ekw==
dependencies:
rc "^1.2.8"

release-zalgo@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/release-zalgo/-/release-zalgo-1.0.0.tgz#09700b7e5074329739330e535c5a90fb67851730"
integrity sha1-CXALflB0Mpc5Mw5TXFqQ+2eFFzA=
dependencies:
es6-error "^4.0.1"

require-directory@^2.1.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/require-directory/-/require-directory-2.1.1.tgz#8c64ad5fd30dab1c976e2344ffe7f792a6a6df42"
integrity sha1-jGStX9MNqxyXbiNE/+f3kqam30I=

require-main-filename@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/require-main-filename/-/require-main-filename-2.0.0.tgz#d0b329ecc7cc0f61649f62215be69af54aa8989b"
integrity sha512-NKN5kMDylKuldxYLSUfrbo5Tuzh4hd+2E8NPPX02mZtn1VuREQToYe/ZdlJy+J3uCpfaiGF05e7B8W0iXbQHmg==

resolve-cwd@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/resolve-cwd/-/resolve-cwd-3.0.0.tgz#0f0075f1bb2544766cf73ba6a6e2adfebcb13f2d"
integrity sha512-OrZaX2Mb+rJCpH/6CpSqt9xFVpN++x01XnN2ie9g6P5/3xelLAkXWVADpdz1IHD/KFfEXyE6V0U01OQ3UO2rEg==
dependencies:
resolve-from "^5.0.0"

resolve-from@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/resolve-from/-/resolve-from-5.0.0.tgz#c35225843df8f776df21c57557bc087e9dfdfc69"
integrity sha512-qYg9KP24dD5qka9J47d0aVky0N+b4fTU89LN9iDnjB5waksiC49rvMB0PrUJQGoTmH50XPiqOvAjDfaijGxYZw==

resolve@^1.10.0, resolve@^1.3.2:
version "1.17.0"
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.17.0.tgz#b25941b54968231cc2d1bb76a79cb7f2c0bf8444"
integrity sha512-ic+7JYiV8Vi2yzQGFWOkiZD5Z9z7O2Zhm9XMaTxdJExKasieFCr+yXZ/WmXsckHiKl12ar0y6XiXDx3m4RHn1w==
dependencies:
path-parse "^1.0.6"

responselike@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/responselike/-/responselike-1.0.2.tgz#918720ef3b631c5642be068f15ade5a46f4ba1e7"
integrity sha1-kYcg7ztjHFZCvgaPFa3lpG9Loec=
dependencies:
lowercase-keys "^1.0.0"

restore-cursor@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/restore-cursor/-/restore-cursor-3.1.0.tgz#39f67c54b3a7a58cea5236d95cf0034239631f7e"
Expand Down

0 comments on commit e674bdf

Please sign in to comment.