Skip to content
This repository has been archived by the owner on Jul 19, 2021. It is now read-only.

Add tests to prettier config items #745

Merged
merged 1 commit into from
Sep 11, 2018
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
Add tests to prettier config items
- Pass 'prettier.config' to --config flag
- Pass 'prettier.ignorePath' to --ignore-path
- Add tests for all config items
  • Loading branch information
t-kelly committed Sep 11, 2018
commit 012d0cd841415dd412bd894f6005c196502167e3
4 changes: 2 additions & 2 deletions packages/slate-tools/slate-tools.schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ module.exports = {
'prettier.bin': path.resolve(__dirname, 'node_modules/.bin/prettier'),

// Path to .prettierrc file
'prettier.rc': (config) =>
'prettier.config': (config) =>
path.resolve(config.get('paths.theme'), '.prettierrc'),

// Path to .prettierignore file
'prettier.ignore': (config) =>
'prettier.ignorePath': (config) =>
path.resolve(config.get('paths.theme'), '.prettierignore'),

// Path to self-signed SSL certificate which is used when developing
Expand Down
31 changes: 29 additions & 2 deletions packages/slate-tools/tools/prettier/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,48 @@ jest.mock('child_process', () => ({exec: jest.fn()}));

const {exec} = require.requireMock('child_process');

describe('eslint()', () => {
describe('Prettier()', () => {
beforeEach(() => {
exec.mockClear();
});

test('executes the Prettier bin found in slate-tools/node_modules', () => {
test(`executes the Prettier bin from the path specified in the 'prettier.bin' config`, () => {
const {prettier} = require('../index');
const SlateConfig = require('@shopify/slate-config');
const config = new SlateConfig(require('../../../slate-tools.schema'));

prettier();

expect(exec).toHaveBeenCalledTimes(1);
expect(exec).toHaveBeenCalledWith(
expect.stringContaining(config.get('prettier.bin')),
expect.anything(),
);
});

test(`executes Prettier with the --config flag set to 'prettier.config' config`, () => {
const {prettier} = require('../index');
const SlateConfig = require('@shopify/slate-config');
const config = new SlateConfig(require('../../../slate-tools.schema'));

prettier();

expect(exec).toHaveBeenCalledWith(
expect.stringContaining(`--config ${config.get('prettier.config')}`),
expect.anything(),
);
});

test(`executes Prettier with the --ignore-path flag set to 'prettier.ignorePath' config`, () => {
const {prettier} = require('../index');
const SlateConfig = require('@shopify/slate-config');
const config = new SlateConfig(require('../../../slate-tools.schema'));
prettier();
expect(exec).toHaveBeenCalledWith(
expect.stringContaining(
`--ignore-path ${config.get('prettier.ignorePath')}`,
),
expect.anything(),
);
});
});
11 changes: 10 additions & 1 deletion packages/slate-tools/tools/prettier/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const fs = require('fs');
const {exec} = require('child_process');
const {promisify} = require('util');
const SlateConfig = require('@shopify/slate-config');
Expand All @@ -6,6 +7,10 @@ const config = new SlateConfig(require('../../slate-tools.schema'));

async function prettier({scripts, styles, json} = {}) {
const executable = config.get('prettier.bin');
const prettierConfig = `--config ${config.get('prettier.config')}`;
const ignorePath = fs.existsSync(config.get('prettier.ignorePath'))
? `--ignore-path ${config.get('prettier.ignorePath')}`
: '';
const extensions = [
...(scripts ? ['js'] : []),
...(styles ? ['css', 'scss', 'sass'] : []),
Expand All @@ -17,7 +22,11 @@ async function prettier({scripts, styles, json} = {}) {
: `./**/*.${extensions.join(',')}`;

try {
await promisify(exec)(`${JSON.stringify(executable)} "${glob}" --write`);
await promisify(exec)(
`${JSON.stringify(
executable,
)} "${glob}" --write ${prettierConfig} ${ignorePath}`,
);
} catch (error) {
if (typeof error.stdout !== 'string') {
throw error;
Expand Down