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

Commit

Permalink
Merge pull request #745 from Shopify/prettier-config-test
Browse files Browse the repository at this point in the history
Add tests to prettier config items
  • Loading branch information
t-kelly authored Sep 11, 2018
2 parents 9906033 + 012d0cd commit 8c575ef
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
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

0 comments on commit 8c575ef

Please sign in to comment.