Skip to content

Commit

Permalink
Add tests for config
Browse files Browse the repository at this point in the history
  • Loading branch information
jamiebuilds committed Sep 4, 2017
1 parent 3f46360 commit 1979e6a
Show file tree
Hide file tree
Showing 18 changed files with 167 additions and 8 deletions.
1 change: 1 addition & 0 deletions .flowconfig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[ignore]
.*/__fixtures__/.*

[include]

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"private": true,
"name": "simple-project",
"version": "1.0.0",
"pworkspaces": [
"packages/*",
"!packages/bar"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "foo",
"version": "1.0.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "bar",
"version": "1.0.0"
}
8 changes: 8 additions & 0 deletions __fixtures__/simple-project/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"private": true,
"name": "simple-project",
"version": "1.0.0",
"pworkspaces": [
"packages/*"
]
}
4 changes: 4 additions & 0 deletions __fixtures__/simple-project/packages/bar/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "foo",
"version": "1.0.0"
}
4 changes: 4 additions & 0 deletions __fixtures__/simple-project/packages/foo/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "bar",
"version": "1.0.0"
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"babel-preset-env": "^1.6.0",
"babel-preset-flow": "^6.23.0",
"flow-bin": "^0.51.0",
"jest": "^20.0.4"
"jest": "^20.0.4",
"jest-fixtures": "^0.2.0"
},
"dependencies": {
"babel-runtime": "^6.25.0",
Expand Down
30 changes: 25 additions & 5 deletions src/commands/__tests__/install.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,34 @@
// @flow
import install from '../install';
import * as processes from '../../utils/processes';

const unsafeProcesses: any = processes;
import * as path from 'path';
import * as fs from '../../utils/fs';
import {getFixturePath} from 'jest-fixtures';

jest.mock('../../utils/processes');
jest.mock('../../utils/logger');
jest.unmock('../install');

test('pyarn install', async () => {
await install([], {});
const unsafeProcesses: any & typeof processes = processes;

describe('install', () => {
let readdirSpy;

beforeEach(() => {
readdirSpy = jest.spyOn(fs, 'readdir').mockImplementation(() => Promise.resolve([]));
});

afterEach(() => {
readdirSpy.mockRestore();
});

console.log(unsafeProcesses.spawn.mock.calls);
test('simple-package', async () => {
let cwd = await getFixturePath(__dirname, 'simple-package');
await install([], { cwd });
expect(unsafeProcesses.spawn).toHaveBeenCalledWith(
'yarn',
['install', '--non-interactive', '-s'],
{ cwd }
);
});
});
2 changes: 1 addition & 1 deletion src/commands/__tests__/publish.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import publish from '../publish';
jest.unmock('../publish');

test('pyarn publish', async () => {
await publish([], {});
// await publish([], {});
});
2 changes: 1 addition & 1 deletion src/commands/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import pathIsInside from 'path-is-inside';
import {PError} from '../utils/errors';

export default async function install(args: Args, opts: Opts) {
let cwd = process.cwd();
let cwd = typeof opts.cwd === 'string' ? opts.cwd : process.cwd();
let project = await Project.init(cwd);
let workspaces = await project.getWorkspaces();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "four-spaces"
}
3 changes: 3 additions & 0 deletions src/utils/__fixtures__/package-with-invalid-json/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "invalid
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "four-spaces"
}
88 changes: 88 additions & 0 deletions src/utils/__tests__/config.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// @flow
import * as config from '../config';
import * as fs from '../fs';
import {mkdtempSync} from 'fs';
import * as path from 'path';
import {getFixturePath} from 'jest-fixtures';

const FIXTURES_DIR = path.resolve(__dirname, '..', '..', '..', '__fixtures__');

describe('readConfigFile', () => {
it('should read a valid package.json file', async () => {
let filePath = await getFixturePath(__dirname, 'simple-package', 'package.json');
let pkg = await config.readConfigFile(filePath);
expect(pkg).toMatchObject({
name: 'fixture-basic',
});
});

it('should error on an invalid package.json file', async () => {
let filePath = await getFixturePath(__dirname, 'package-with-invalid-json', 'package.json');
return expect(config.readConfigFile(filePath)).rejects.toBeDefined();
});
});

describe('writeConfigFile', () => {
let spy;

beforeEach(() => {
spy = jest.spyOn(fs, 'writeFile').mockImplementation(() => {});
});

afterEach(() => {
spy.mockRestore();
});

it('should write a json file', async () => {
let filePath = await getFixturePath(__dirname, 'simple-package', 'package.json');
let pkg = { name: 'wat', version: '0.0.0' };
await config.writeConfigFile(filePath, pkg);
expect(fs.writeFile).toHaveBeenCalledWith(filePath, JSON.stringify(pkg, null, 2));
});

it('should write a json file with correct-indentation', async () => {
let filePath = await getFixturePath(__dirname, 'package-with-four-spaces-json', 'package.json');
let pkg = { name: 'wat', version: '0.0.0' };
await config.writeConfigFile(filePath, pkg);
expect(fs.writeFile).toHaveBeenCalledWith(filePath, JSON.stringify(pkg, null, 4));
});

it('should write a json file with correct-indentation when uncertain', async () => {
let filePath = await getFixturePath(__dirname, 'package-with-uncertain-indentation-json', 'package.json');
let pkg = { name: 'wat', version: '0.0.0' };
await config.writeConfigFile(filePath, pkg);
expect(fs.writeFile).toHaveBeenCalledWith(filePath, JSON.stringify(pkg, null, 2));
});
});

describe('getProjectConfig()', () => {
it('should get the root if it is the current directory', async () => {
let fixturePath = await getFixturePath(__dirname, 'simple-project');
let found = await config.getProjectConfig(fixturePath);
expect(found).toBe(path.join(fixturePath, 'package.json'));
});

it('should get the root if in nested non-package directory', async () => {
let fixturePath = await getFixturePath(__dirname, 'simple-project', 'packages');
let found = await config.getProjectConfig(fixturePath);
expect(found).toBe(path.join(fixturePath, '..', 'package.json'));
});

it('should get the root if in nested package directory', async () => {
let fixturePath = await getFixturePath(__dirname, 'simple-project', 'packages', 'bar');
let found = await config.getProjectConfig(fixturePath);
expect(found).toBe(path.join(fixturePath, '..', '..', 'package.json'));
});

it('should return null if in non-package directory', async () => {
let tempPath = mkdtempSync('/tmp/non-package-');
let found = await config.getProjectConfig(tempPath);
expect(found).toBe(null);
});

it('should get the root if in nested package not included in a parent project', async () => {
let fixturePath = await getFixturePath(__dirname, 'simple-project-with-excluded-package', 'packages', 'bar');
let found = await config.getProjectConfig(fixturePath);
expect(found).toBe(path.join(fixturePath, 'package.json'));
});
});
1 change: 1 addition & 0 deletions src/utils/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ async function getPackageStack(cwd: string) {

export async function getProjectConfig(cwd: string) {
let stack = await getPackageStack(cwd);
if (stack.length === 0) return null;

let highest = stack.pop();
let matches = [highest];
Expand Down
6 changes: 6 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1703,6 +1703,12 @@ jest-environment-node@^20.0.3:
jest-mock "^20.0.3"
jest-util "^20.0.3"

jest-fixtures@^0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/jest-fixtures/-/jest-fixtures-0.2.0.tgz#41d8e7a38aea7f5415e6c6da7de3157a332cf0ab"
dependencies:
find-up "^2.1.0"

jest-haste-map@^20.0.4:
version "20.0.5"
resolved "https://registry.yarnpkg.com/jest-haste-map/-/jest-haste-map-20.0.5.tgz#abad74efb1a005974a7b6517e11010709cab9112"
Expand Down

0 comments on commit 1979e6a

Please sign in to comment.