From ca0eef58a1239879eb4bac41485315d390346f1e Mon Sep 17 00:00:00 2001 From: Kirill Nagaitsev Date: Thu, 23 Apr 2020 17:41:54 -0500 Subject: [PATCH] tests: added run prettier tests --- packages/utils/__tests__/run-prettier.test.ts | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 packages/utils/__tests__/run-prettier.test.ts diff --git a/packages/utils/__tests__/run-prettier.test.ts b/packages/utils/__tests__/run-prettier.test.ts new file mode 100644 index 00000000000..d39b3cfb774 --- /dev/null +++ b/packages/utils/__tests__/run-prettier.test.ts @@ -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'); + }); +});