Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit tests for the command yarn why. #1544

Merged
merged 1 commit into from
Nov 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions __tests__/commands/why.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/* @flow */

import {BufferReporter} from '../../src/reporters/index.js';
import {run as why} from '../../src/cli/commands/why.js';
import * as reporters from '../../src/reporters/index.js';
import Config from '../../src/config.js';
import assert from 'assert';
import path from 'path';

jasmine.DEFAULT_TIMEOUT_INTERVAL = 90000;

const fixturesLoc = path.join(__dirname, '..', 'fixtures', 'why');

async function runWhy(
flags: Object,
args: Array<string>,
name: string,
checkSteps?: ?(config: Config, reporter: BufferReporter) => ?Promise<void>,
): Promise<void> {
const cwd = path.join(fixturesLoc, name);
const reporter = new reporters.BufferReporter({stdout: null, stdin: null});

try {
const config = new Config(reporter);
await config.init({cwd});

await why(config, reporter, flags, args);

if (checkSteps) {
await checkSteps(config, reporter);
}

} catch (err) {
throw new Error(`${err && err.stack}`);
}
}

test.concurrent('throws error with no arguments', (): Promise<void> => {
const reporter = new reporters.ConsoleReporter({});

return new Promise(async (resolve): Promise<void> => {
try {
await runWhy({}, [], 'basic');
} catch (err) {
expect(err.message).toContain(reporter.lang('missingWhyDependency'));
} finally {
resolve();
}
});
});

test.concurrent('throws error with too many arguments', (): Promise<void> => {
const reporter = new reporters.ConsoleReporter({});

return new Promise(async (resolve): Promise<void> => {
try {
await runWhy({}, ['one', 'two'], 'basic');
} catch (err) {
expect(err.message).toContain(reporter.lang('tooManyArguments', 1));
} finally {
resolve();
}
});
});

test.concurrent('throws error if module does not exist', (): Promise<void> => {
const reporter = new reporters.ConsoleReporter({});

return new Promise(async (resolve): Promise<void> => {
try {
await runWhy({}, ['one'], 'basic');
} catch (err) {
console.log(err);
expect(err.message).toContain(reporter.lang('whyUnknownMatch'));
} finally {
resolve();
}
});
});

test.concurrent('should determine that the module installed because it is in dependencies',
(): Promise<void> => {
return runWhy({}, ['mime-types'], 'basic', (config, reporter) => {
const report = reporter.getBuffer();
assert.equal(report[report.length - 1].data, reporter.lang('whySpecifiedSimple', 'dependencies'));
});
});

test.concurrent('should determine that the module installed because it is in devDependencies',
(): Promise<void> => {
return runWhy({}, ['left-pad'], 'basic', (config, reporter) => {
const report = reporter.getBuffer();
assert.equal(report[report.length - 1].data, reporter.lang('whySpecifiedSimple', 'devDependencies'));
});
});

test.concurrent('should determine that the module installed because mime-types depend on it',
(): Promise<void> => {
return runWhy({}, ['mime-db'], 'basic', (config, reporter) => {
const report = reporter.getBuffer();
assert.equal(report[report.length - 1].data, reporter.lang('whyDependedOnSimple', 'mime-types'));
});
});

test.concurrent('should determine that the module installed because it is hoisted from glob depend on it',
(): Promise<void> => {
return runWhy({}, ['glob#minimatch'], 'basic', (config, reporter) => {
const report = reporter.getBuffer();
assert.equal(report[report.length - 2].data, reporter.lang('whyHoistedTo', 'glob#minimatch'));
});
});
10 changes: 10 additions & 0 deletions __tests__/fixtures/why/basic/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"dependencies": {
"mime-types": "2.1.12",
"glob": "7.1.1",
"uglifyify": "3.0.0"
},
"devDependencies": {
"left-pad": "1.1.3"
}
}