Skip to content

Commit

Permalink
tests: added run prettier tests
Browse files Browse the repository at this point in the history
  • Loading branch information
knagaitsev committed Apr 23, 2020
1 parent cc7ebff commit ca0eef5
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions packages/utils/__tests__/run-prettier.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use strict';

import fs from 'fs';
import path from 'path';
//eslint-disable-next-line node/no-extraneous-import
import rimraf from 'rimraf';
import { runPrettier } from '../src/run-prettier';

const outputPath = path.join(__dirname, 'test-assets');
const outputFile = path.join(outputPath, 'test.js');
const stdoutSpy = jest.spyOn(process.stdout, 'write');

describe('runPrettier', () => {
beforeEach(() => {
rimraf.sync(outputPath);
fs.mkdirSync(outputPath);
stdoutSpy.mockClear();
});

afterAll(() => {
rimraf.sync(outputPath);
});

it('should run prettier on JS string and write file', () => {
runPrettier(outputFile, 'console.log("1");console.log("2");');
expect(fs.existsSync(outputFile)).toBeTruthy();
const data = fs.readFileSync(outputFile, 'utf8');
expect(data).toContain("console.log('1');\n");

expect(stdoutSpy.mock.calls.length).toEqual(0);
});

it('prettier should fail on invalid JS, with file still written', () => {
runPrettier(outputFile, '"');
expect(fs.existsSync(outputFile)).toBeTruthy();
const data = fs.readFileSync(outputFile, 'utf8');
expect(data).toContain('"');

expect(stdoutSpy.mock.calls.length).toEqual(1);
expect(stdoutSpy.mock.calls[0][0]).toContain('WARNING: Could not apply prettier');
});
});

0 comments on commit ca0eef5

Please sign in to comment.