Skip to content

Commit

Permalink
[Tests] fix eslint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
sharmilajesupaul authored and ljharb committed Feb 1, 2019
1 parent f7ef780 commit f2fe177
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 48 deletions.
9 changes: 9 additions & 0 deletions .eslintrc
Expand Up @@ -5,4 +5,13 @@
"max-len": 0,
"no-console": 0,
},
"overrides": [{
"files": ["test/**/*.js"],
"env": {
"jest": true,
},
"rules": {
"global-require": "off",
},
}],
}
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -12,7 +12,7 @@
"postbuild": "cp src/*.json ./",
"build:watch": "npm run build -- --watch",
"clean": "rimraf *.js {commands,helpers,traversal} schema.json",
"lint": "eslint --ext=js,jsx src",
"lint": "eslint --ext=js,jsx --ignore-path=.gitignore .",
"prepublish": "npm run build",
"pretest": "npm run lint",
"tests-only": "jest --coverage",
Expand Down
8 changes: 4 additions & 4 deletions test/helpers/getComponents.js
Expand Up @@ -3,11 +3,11 @@ import getComponents from '../../src/helpers/getComponents';

jest.mock('../../src/helpers/validateProject', () => jest.fn());
jest.mock('../../src/helpers/globToFiles', () => jest.fn(() => ['a', 'b', 'n']));
jest.mock('../../src/helpers/requireFiles', () => jest.fn(paths => paths.reduce((obj, path) => ({
jest.mock('../../src/helpers/requireFiles', () => jest.fn(paths => paths.reduce((obj, filePath) => ({
...obj,
[path]: {
actualPath: path,
Module: path >= 'foo' ? { default() {} } : { named() {} },
[filePath]: {
actualPath: filePath,
Module: filePath >= 'foo' ? { default() {} } : { named() {} },
},
}), {})));

Expand Down
9 changes: 6 additions & 3 deletions test/helpers/getDescriptorFromProvider.js
Expand Up @@ -69,7 +69,12 @@ describe('getDescriptorFromProvider', () => {
const projectRoot = { root: '' };
const getProjectExtras = require('../../src/helpers/getProjectExtras');

getDescriptorFromProvider(provider, { Components, projectConfig, projectRoot, getExtras });
getDescriptorFromProvider(provider, {
Components,
projectConfig,
projectRoot,
getExtras,
});

expect(getProjectExtras).toHaveBeenCalledTimes(1);
expect(getProjectExtras).toHaveBeenCalledWith(expect.objectContaining({
Expand All @@ -83,7 +88,6 @@ describe('getDescriptorFromProvider', () => {
const descriptor = { a: 1 };
const provider = () => descriptor;
const Components = { components: '' };
const getExtras = () => {};
const projectRoot = { root: '' };

const result = getDescriptorFromProvider(provider, {
Expand All @@ -105,7 +109,6 @@ describe('getDescriptorFromProvider', () => {
const descriptor = { a: 1, metadata: { b: 3, c: 4 } };
const provider = () => descriptor;
const Components = { components: '' };
const getExtras = () => {};
const projectRoot = { root: '' };

const result = getDescriptorFromProvider(provider, {
Expand Down
8 changes: 4 additions & 4 deletions test/helpers/getVariationProviders.js
Expand Up @@ -3,11 +3,11 @@ import getVariationProviders from '../../src/helpers/getVariationProviders';

jest.mock('../../src/helpers/validateProject', () => jest.fn());
jest.mock('../../src/helpers/globToFiles', () => jest.fn(() => ['a', 'b', 'n']));
jest.mock('../../src/helpers/requireFiles', () => jest.fn(paths => paths.reduce((obj, path) => ({
jest.mock('../../src/helpers/requireFiles', () => jest.fn(paths => paths.reduce((obj, filePath) => ({
...obj,
[path]: {
actualPath: path,
Module: path >= 'foo' ? { default() {} } : { named() {} },
[filePath]: {
actualPath: filePath,
Module: filePath >= 'foo' ? { default() {} } : { named() {} },
},
}), {})));

Expand Down
2 changes: 1 addition & 1 deletion test/helpers/isValidProjectName.js
Expand Up @@ -22,4 +22,4 @@ describe('isValidProjectName', () => {
it('returns true if `projects` has a key for `project`', () => {
expect(isValidProjectName({ foo: true }, 'foo')).toBe(true);
});
});
});
29 changes: 17 additions & 12 deletions test/traversal/forEachDescriptor.js
@@ -1,7 +1,8 @@
import interopRequireDefault from '../../src/helpers/interopRequireDefault';
import forEachDescriptor from '../../src/traversal/forEachDescriptor';

let mockComponents, mockVariations;
let mockComponents;
let mockVariations;

jest.mock('../../src/helpers/getComponents', () => jest.fn(() => mockComponents));
jest.mock('../../src/helpers/getVariationProviders', () => jest.fn(() => mockVariations));
Expand Down Expand Up @@ -40,7 +41,9 @@ describe('forEachDescriptor', () => {
});

expect(() => forEachDescriptor(mockProjectConfig, { getDescriptor() {} })).toThrow(TypeError);
expect(() => forEachDescriptor(mockProjectConfig, { getDescriptor(a, b, c) {} })).toThrow(TypeError);
expect(() => forEachDescriptor(mockProjectConfig, {
getDescriptor(a, b, c) { return (a, b, c); },
})).toThrow(TypeError);
});

it('returns a function', () => {
Expand Down Expand Up @@ -70,10 +73,12 @@ describe('forEachDescriptor', () => {
describe('traversal function', () => {
const projectRoot = 'some root';
const descriptor = {};
let getExtras, getDescriptor;
let getExtras;
let getDescriptor;
beforeEach(() => {
getExtras = jest.fn();
getDescriptor = jest.fn((x) => descriptor);
// eslint-disable-next-line no-unused-vars
getDescriptor = jest.fn(x => descriptor);
});

it('throws when `callback` is not a 1-arg function', () => {
Expand All @@ -88,9 +93,9 @@ describe('forEachDescriptor', () => {
});

expect(() => traverse(() => {})).toThrow(TypeError);
expect(() => traverse((a) => {})).not.toThrow(TypeError);
expect(() => traverse((a, b) => {})).not.toThrow(TypeError);
expect(() => traverse((a, b, c) => {})).toThrow(TypeError);
expect(() => traverse(a => ({ a }))).not.toThrow(TypeError);
expect(() => traverse((a, b) => ({ a, b }))).not.toThrow(TypeError);
expect(() => traverse((a, b, c) => ({ a, b, c }))).toThrow(TypeError);
});

it('throws with no components', () => {
Expand Down Expand Up @@ -146,7 +151,7 @@ describe('forEachDescriptor', () => {
const Components = require('../../src/helpers/getComponents')();
const variations = require('../../src/helpers/getVariationProviders')();

const callback = jest.fn((x) => {});
const callback = jest.fn(x => ({ x }));
traverse(callback);

expect(getDescriptor).toHaveBeenCalledTimes(2);
Expand Down Expand Up @@ -176,15 +181,15 @@ describe('forEachDescriptor', () => {
variationProvider: {
hierarchy: 'path/to/a',
path: variationPathA,
resolvedPath: mockVariations[variationPathA].actualPath
resolvedPath: mockVariations[variationPathA].actualPath,
},
variationPath: variationPathA,
}]);
expect(second).toEqual([descriptor, {
variationProvider: {
hierarchy: 'path/to/b',
path: variationPathB,
resolvedPath: mockVariations[variationPathB].actualPath
resolvedPath: mockVariations[variationPathB].actualPath,
},
variationPath: variationPathB,
}]);
Expand All @@ -202,7 +207,7 @@ describe('forEachDescriptor', () => {
projectRoot,
});

const callback = jest.fn((x) => {});
const callback = jest.fn(x => ({ x }));
expect(() => traverse(callback)).toThrow(TypeError);
});

Expand All @@ -219,7 +224,7 @@ describe('forEachDescriptor', () => {
projectRoot,
});

const callback = jest.fn((x) => {});
const callback = jest.fn(x => ({ x }));
traverse(callback);

expect(getDescriptor).toHaveBeenCalledTimes(1);
Expand Down
20 changes: 10 additions & 10 deletions test/traversal/forEachProject.js
Expand Up @@ -7,10 +7,10 @@ describe('forEachProject', () => {

it('throws when it receives a non-empty `projects` array', () => {
[undefined, null, '', 'foo', {}, () => {}, true, 42, false].forEach((nonArray) => {
expect(() => forEachProject({}, nonArray, (x) => {})).toThrow(TypeError);
expect(() => forEachProject({}, nonArray, x => ({ x }))).toThrow(TypeError);
});

expect(() => forEachProject({}, [], (x) => {})).toThrow(TypeError);
expect(() => forEachProject({}, [], x => ({ x }))).toThrow(TypeError);
});

const mockProjectConfig = {
Expand All @@ -19,10 +19,10 @@ describe('forEachProject', () => {
};

it('validates the project config', () => {
expect(() => forEachProject({ a: {}, b: mockProjectConfig }, ['a', 'b'], (x) => {})).toThrow(SyntaxError);
expect(() => forEachProject({ a: mockProjectConfig, b: {} }, ['a', 'b'], (x) => {})).toThrow(SyntaxError);
expect(() => forEachProject({ a: mockProjectConfig, b: {} }, ['b'], (x) => {})).toThrow(SyntaxError);
expect(() => forEachProject({ a: mockProjectConfig, b: {} }, ['a'], (x) => {})).not.toThrow();
expect(() => forEachProject({ a: {}, b: mockProjectConfig }, ['a', 'b'], x => ({ x }))).toThrow(SyntaxError);
expect(() => forEachProject({ a: mockProjectConfig, b: {} }, ['a', 'b'], x => ({ x }))).toThrow(SyntaxError);
expect(() => forEachProject({ a: mockProjectConfig, b: {} }, ['b'], x => ({ x }))).toThrow(SyntaxError);
expect(() => forEachProject({ a: mockProjectConfig, b: {} }, ['a'], x => ({ x }))).not.toThrow();
});

it('throws if the callback is not a function with a length of 1 or 2', () => {
Expand All @@ -31,11 +31,11 @@ describe('forEachProject', () => {
});

expect(() => forEachProject({ a: mockProjectConfig }, ['a'], () => {})).toThrow(TypeError);
expect(() => forEachProject({ a: mockProjectConfig }, ['a'], (a, b, c) => {})).toThrow(TypeError);
expect(() => forEachProject({ a: mockProjectConfig }, ['a'], (a, b, c) => ({ a, b, c }))).toThrow(TypeError);
});

it('calls the callback with the expected arguments', () => {
const callback = jest.fn((x) => {});
const callback = jest.fn(x => ({ x }));

const bProjectConfig = { ...mockProjectConfig, require: ['test'] };

Expand All @@ -47,7 +47,7 @@ describe('forEachProject', () => {

expect(callback).toHaveBeenCalledTimes(2);
const { calls: [aArgs, bArgs] } = callback.mock;

expect(aArgs).toEqual([
'a',
mockProjectConfig,
Expand All @@ -57,4 +57,4 @@ describe('forEachProject', () => {
bProjectConfig,
]);
});
});
});
21 changes: 9 additions & 12 deletions test/traversal/forEachProjectVariation.js
Expand Up @@ -36,15 +36,13 @@ jest.mock('../../src/helpers/getVariationProviders', () => jest.fn(() => ({
const mockProjectNames = [mockProjectName, mockProjectName2];

jest.mock('../../src/traversal/forEachVariation');
jest.mock('../../src/helpers/getProjectRootConfig', () => (projectRoot, configPath) => {
return {
projects: {
[mockProjectName]: { ...mockProjectConfig },
[mockProjectName2]: { ...mockProjectConfig },
},
projectNames: mockProjectNames,
};
});
jest.mock('../../src/helpers/getProjectRootConfig', () => () => ({
projects: {
[mockProjectName]: { ...mockProjectConfig },
[mockProjectName2]: { ...mockProjectConfig },
},
projectNames: mockProjectNames,
}));

describe('forEachProjectVariation', () => {
it('is a function', () => {
Expand All @@ -64,15 +62,14 @@ describe('forEachProjectVariation', () => {
});

expect(() => traverse(() => {})).toThrow(TypeError);
expect(() => traverse((a, b) => {})).toThrow(TypeError);
expect(() => traverse((a, b) => ({ a, b }))).toThrow(TypeError);
});
});

it('invokes `forEachVariation` with the expected arguments', () => {
const traverse = forEachProjectVariation(consumer);
const callback = (x) => {};
const callback = x => ({ x });
const forEachVariation = require('../../src/traversal/forEachVariation');
const rootConfig = require('../../src/helpers/getProjectRootConfig')();

traverse(callback);

Expand Down
2 changes: 1 addition & 1 deletion test/traversal/forEachVariation.js
Expand Up @@ -37,7 +37,7 @@ describe('forEachVariation', () => {
});

expect(() => forEachVariation(mockDescriptor, consumer, () => {})).toThrow(TypeError);
expect(() => forEachVariation(mockDescriptor, consumer, (a, b) => {})).toThrow(TypeError);
expect(() => forEachVariation(mockDescriptor, consumer, (a, b) => ({ a, b }))).toThrow(TypeError);
});

describe('callback function', () => {
Expand Down

0 comments on commit f2fe177

Please sign in to comment.