Skip to content

Commit

Permalink
chore: update package-lock.json
Browse files Browse the repository at this point in the history
  • Loading branch information
ofrobots committed Jun 28, 2018
1 parent 3d0ead2 commit 08989fb
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 25 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

80 changes: 58 additions & 22 deletions test/test-init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import path from 'path';

import {Options} from '../src/cli';
import * as init from '../src/init';
import {nop} from '../src/util';
import {nop, readJsonp as readJson} from '../src/util';

import {withFixtures} from './fixtures';

const OPTIONS: Options = {
Expand All @@ -30,16 +31,27 @@ const OPTIONS: Options = {
no: false,
logger: {log: nop, error: nop, dir: nop}
};
const OPTIONS_YES = Object.assign({}, OPTIONS, {yes: true});
const OPTIONS_NO = Object.assign({}, OPTIONS, {no: true});
const OPTIONS_DRY_RUN = Object.assign({}, OPTIONS, {dryRun: true});

function hasExpectedScripts(packageJson: init.PackageJson): boolean {
return !!packageJson.scripts && [
'check', 'clean', 'compile', 'fix', 'prepare', 'pretest', 'posttest'
].every(s => !!packageJson.scripts![s]);
}

function hasExpectedDependencies(packageJson: init.PackageJson): boolean {
return !!packageJson.devDependencies &&
['gts', 'typescript'].every(d => !!packageJson.devDependencies![d]);
}

test('addScripts should add a scripts section if none exists', async t => {
const pkg: init.PackageJson = {};
const result = await init.addScripts(pkg, OPTIONS);
t.is(result, true); // made edits.
t.truthy(pkg.scripts);
['check', 'clean', 'compile', 'fix', 'prepare', 'pretest', 'posttest']
.forEach(s => {
t.truthy(pkg.scripts![s]);
});
t.truthy(hasExpectedScripts(pkg));
});

test('addScripts should not edit existing scripts on no', async t => {
Expand All @@ -53,8 +65,8 @@ test('addScripts should not edit existing scripts on no', async t => {
posttest: `fake run check`
};
const pkg: init.PackageJson = {scripts: Object.assign({}, SCRIPTS)};
const optionsWithNo = Object.assign({}, OPTIONS, {no: true});
const result = await init.addScripts(pkg, optionsWithNo);

const result = await init.addScripts(pkg, OPTIONS_NO);
t.is(result, false); // no edits.
t.deepEqual(pkg.scripts, SCRIPTS);
});
Expand All @@ -70,8 +82,7 @@ test('addScripts should edit existing scripts on yes', async t => {
posttest: `fake run check`
};
const pkg: init.PackageJson = {scripts: Object.assign({}, SCRIPTS)};
const optionsWithYes = Object.assign({}, OPTIONS, {yes: true});
const result = await init.addScripts(pkg, optionsWithYes);
const result = await init.addScripts(pkg, OPTIONS_YES);
t.is(result, true); // made edits.
t.notDeepEqual(pkg.scripts, SCRIPTS);
});
Expand All @@ -86,29 +97,54 @@ test('addDependencies should add a deps section if none exists', async t => {
test('addDependencies should not edit existing deps on no', async t => {
const DEPS = {gts: 'something', typescript: 'or the other'};
const pkg: init.PackageJson = {devDependencies: Object.assign({}, DEPS)};
const optionsWithNo = Object.assign({}, OPTIONS, {no: true});
const result = await init.addDependencies(pkg, optionsWithNo);
const OPTIONS_NO = Object.assign({}, OPTIONS, {no: true});
const result = await init.addDependencies(pkg, OPTIONS_NO);
t.is(result, false); // no edits.
t.deepEqual(pkg.devDependencies, DEPS);
});

test('addDependencies should edit existing deps on yes', async t => {
const DEPS = {gts: 'something', typescript: 'or the other'};
const pkg: init.PackageJson = {devDependencies: Object.assign({}, DEPS)};
const optionsWithYes = Object.assign({}, OPTIONS, {yes: true});
const result = await init.addDependencies(pkg, optionsWithYes);

const result = await init.addDependencies(pkg, OPTIONS_YES);
t.is(result, true); // made edits.
t.notDeepEqual(pkg.devDependencies, DEPS);
});

// TODO: this test has not been completed yet.
// test.serial('init should read local package.json', t => {
// return withFixtures(
// {'package.json': JSON.stringify({some: 'property'})}, async () => {
// const optionsWithDryRun = Object.assign({}, OPTIONS, {dryRun: true});
// const result = await init.init(optionsWithDryRun);
// t.truthy(result);
// });
// });
// TODO: test generateConfigFile

// init
test.serial('init should read local package.json', t => {
const originalContents = {some: 'property'};
return withFixtures(
{'package.json': JSON.stringify(originalContents)}, async () => {
// TODO: this test causes `npm install` to run in the fixture directory.
// This may make it sensistive to the network, npm resiliency. Find a
// way to mock npm.
const result = await init.init(OPTIONS_YES);
t.truthy(result);
const contents = await readJson('./package.json');

t.not(contents, originalContents, 'the file should have been modified');
t.is(
contents.some, originalContents.some,
'unrelated property should have preserved');
});
});

test.serial('init should handle missing package.json', t => {
return withFixtures({}, async () => {
// TODO: this test causes `npm install` to run in the fixture directory.
// This may make it sensistive to the network, npm resiliency. Find a way to
// mock npm.
const result = await init.init(OPTIONS_YES);
t.truthy(result);
const contents = await readJson('./package.json');
t.truthy(hasExpectedScripts(contents));
t.truthy(hasExpectedDependencies(contents));
});
});


// TODO: need more tests.

0 comments on commit 08989fb

Please sign in to comment.