diff --git a/__snapshots__/format-comment.test.js.snap b/__snapshots__/format-comment.test.js.snap deleted file mode 100644 index 94da2c2..0000000 --- a/__snapshots__/format-comment.test.js.snap +++ /dev/null @@ -1,123 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`Formats multiple code blocks 1`] = ` -" -\`\`\`js -foo( - reallyLongArg(), - omgSoManyParameters(), - IShouldRefactorThis(), - isThereSeriouslyAnotherOne() -); -\`\`\` - -\`\`\`js -foo( - reallyLongArg(), - omgSoManyParameters(), - IShouldRefactorThis(), - isThereSeriouslyAnotherOne() -); -\`\`\`" -`; - -exports[`Overrides notice if the comment has been formatted previously 1`] = ` -" - -\`\`\`js -bar(); -\`\`\`" -`; - -exports[`languages Formats a CSS code block 1`] = ` -" -\`\`\`css -.foo { - color: red; -} -\`\`\`" -`; - -exports[`languages Formats a GraphQL code block 1`] = ` -" -\`\`\`graphql -{ - language(name: \\"GraphQL\\") { - isSupported - } -} -\`\`\`" -`; - -exports[`languages Formats a JSON code block 1`] = ` -" -\`\`\`json -{ \\"supportJSON\\": true } -\`\`\`" -`; - -exports[`languages Formats a JavaScript code block 1`] = ` -" -\`\`\`js -foo( - reallyLongArg(), - omgSoManyParameters(), - IShouldRefactorThis(), - isThereSeriouslyAnotherOne() -); -\`\`\`" -`; - -exports[`languages Formats a TypeScript code block 1`] = ` -" -\`\`\`ts -interface ITypeScript { - supported: boolean; -} -\`\`\`" -`; diff --git a/__tests__/format-comment.test.js b/__tests__/format-comment.test.js new file mode 100644 index 0000000..c35baba --- /dev/null +++ b/__tests__/format-comment.test.js @@ -0,0 +1,126 @@ +const m = '../format-comment.js'; + +beforeEach(() => { + jest.resetModules(); +}); + +test('Runs Prettier', () => { + const comment = '```js\n__CODE__\n```'; + + const format = jest.fn().mockReturnValue(''); + jest.doMock('prettier', () => ({format})); + const formatComment = require(m); + + formatComment(comment); + expect(format).toHaveBeenCalled(); +}); + +test('Passes code block to Prettier', () => { + const comment = '```js\n__CODE__\n```'; + + const format = jest.fn().mockReturnValue(''); + jest.doMock('prettier', () => ({format})); + const formatComment = require(m); + + formatComment(comment); + expect(format.mock.calls[0][0]).toBe('__CODE__'); +}); + +test('Passes language to Prettier via `filepath`', () => { + const comment = '```js\n__CODE__\n```'; + + const format = jest.fn().mockReturnValue(''); + jest.doMock('prettier', () => ({format})); + const formatComment = require(m); + + formatComment(comment); + expect(format.mock.calls[0][1].filepath).toBe('.js'); +}); + +test('Outputs formatted code', () => { + const comment = '```js\n__CODE__\n```'; + + const format = jest.fn().mockReturnValue('__CODE_1__'); + jest.doMock('prettier', () => ({format})); + const formatComment = require(m); + + expect(formatComment(comment)).toMatch(/```js\n__CODE_1__[\s\S]*\n```$/); +}); + +test('Trims trailing newline', () => { + const comment = '```js\n__CODE__\n```'; + + const format = jest.fn().mockReturnValue('__CODE_1__\n'); + jest.doMock('prettier', () => ({format})); + const formatComment = require(m); + + expect(formatComment(comment)).toMatch(/```js\n__CODE_1__\n```$/); +}); + +test('Outputs notice comment', () => { + const comment = '```js\n__CODE__\n```'; + + const format = jest.fn().mockReturnValue(''); + jest.doMock('prettier', () => ({format})); + const formatComment = require(m); + + expect(formatComment(comment)).toMatch(/^/); +}); + +test('Outputs old code in notice comment', () => { + const comment = '```js\n__CODE__\n```'; + + const format = jest.fn().mockReturnValue(''); + jest.doMock('prettier', () => ({format})); + const formatComment = require(m); + + expect(formatComment(comment)).toMatch(/^/); +}); + +test('Overrides existing notice comments', () => { + const comment = '```js\n__CODE__\n```'; + + const format = jest.fn().mockReturnValueOnce('__CODE_1__').mockReturnValue(''); + jest.doMock('prettier', () => ({format})); + const formatComment = require(m); + + const processedComment = formatComment(formatComment(comment)); + expect(processedComment).toMatch(/^/); + expect(processedComment).not.toMatch(//); +}); + +test('Ignores comments without code blocks', () => { + const comment = 'stuff and things'; + + const format = jest.fn().mockReturnValue(''); + jest.doMock('prettier', () => ({format})); + const formatComment = require(m); + + const processedComment = formatComment(comment); + expect(format).not.toHaveBeenCalled(); + expect(processedComment).toBe(comment); +}); + +test('Ignores incompatible code blocks', () => { + const comment = '```sh\n__CODE__\n```'; + + const format = jest.fn().mockReturnValue(''); + jest.doMock('prettier', () => ({format})); + const formatComment = require(m); + + const processedComment = formatComment(comment); + expect(format).not.toHaveBeenCalled(); + expect(processedComment).toBe(comment); +}); + +test('Ignores comments containing ``', () => { + const comment = '\n```js\n__CODE__\n```'; + + const format = jest.fn().mockReturnValue(''); + jest.doMock('prettier', () => ({format})); + const formatComment = require(m); + + const processedComment = formatComment(comment); + expect(format).not.toHaveBeenCalled(); + expect(processedComment).toBe(comment); +}); diff --git a/format-comment.test.js b/format-comment.test.js deleted file mode 100644 index c4eefa7..0000000 --- a/format-comment.test.js +++ /dev/null @@ -1,90 +0,0 @@ -const formatComment = require('./format-comment.js'); - -describe('languages', () => { - test('Formats a JavaScript code block', () => { - const comment = '```js\nfoo(reallyLongArg(), omgSoManyParameters(), IShouldRefactorThis(), isThereSeriouslyAnotherOne());\n```'; - - expect(formatComment(comment)).toMatchSnapshot(); - }); - - test('Formats a CSS code block', () => { - const comment = '```css\n.foo {color: red;}\n```'; - - expect(formatComment(comment)).toMatchSnapshot(); - }); - - test('Formats a TypeScript code block', () => { - const comment = '```ts\ninterface ITypeScript {supported: boolean}\n```'; - - expect(formatComment(comment)).toMatchSnapshot(); - }); - - test('Formats a JSON code block', () => { - const comment = '```json\n{"supportJSON": true}\n```'; - - expect(formatComment(comment)).toMatchSnapshot(); - }); - - test('Formats a GraphQL code block', () => { - const comment = '```graphql\n{language(name: "GraphQL") {isSupported}}\n```'; - - expect(formatComment(comment)).toMatchSnapshot(); - }); -}); - -test('Formats multiple code blocks', () => { - const comment = '```js\nfoo(reallyLongArg(), omgSoManyParameters(), IShouldRefactorThis(), isThereSeriouslyAnotherOne());\n```\n```js\nfoo(reallyLongArg(), omgSoManyParameters(), IShouldRefactorThis(), isThereSeriouslyAnotherOne());\n```'; - - expect(formatComment(comment)).toMatchSnapshot(); -}); - -test('Overrides notice if the comment has been formatted previously', () => { - const comment = '\n```js\nbar()\n```'; - - expect(formatComment(comment)).toMatchSnapshot(); -}); - -test('Ignores no code blocks', () => { - const comment = 'Nothing to format here\n'; - - expect(formatComment(comment)).toBe(comment); -}); - -test('Ignores incompatible code blocks', () => { - const comment = '```sh\necho "Nothing to format here"\n```'; - - expect(formatComment(comment)).toBe(comment); -}); - -test('Ignores correctly formatted code blocks', () => { - const comment = '```js\nconst all = "good";\n```'; - - expect(formatComment(comment)).toBe(comment); -}); - -test('Ignores comments containing ``', () => { - const comment = '\n\n```js\nfoo(reallyLongArg(), omgSoManyParameters(), IShouldRefactorThis(), isThereSeriouslyAnotherOne());\n```'; - - expect(formatComment(comment)).toBe(comment); -}); - -describe('invalid code blocks', () => { - beforeEach(() => { - global.console = { - warn: jest.fn() - }; - }); - - test('Warns about invalid code blocks', () => { - const comment = '```js\ninvalid javascript\n```'; - formatComment(comment); - - expect(global.console.warn).toHaveBeenCalled(); - }); - - test('Ignores invalid code blocks', () => { - const comment = '```js\ninvalid javascript\n```'; - - expect(formatComment(comment)).toBe(comment); - }); -});