From c03417cf5f94a205f0656dc765fda083924065d5 Mon Sep 17 00:00:00 2001 From: Jonas Gierer Date: Mon, 3 Jul 2017 21:28:44 +0200 Subject: [PATCH 1/2] Rework tests --- __snapshots__/format-comment.test.js.snap | 123 ------------------ format-comment.test.js | 147 +++++++++++++--------- 2 files changed, 91 insertions(+), 179 deletions(-) delete mode 100644 __snapshots__/format-comment.test.js.snap 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/format-comment.test.js b/format-comment.test.js index c4eefa7..299f9dc 100644 --- a/format-comment.test.js +++ b/format-comment.test.js @@ -1,90 +1,125 @@ -const formatComment = require('./format-comment.js'); - -describe('languages', () => { - test('Formats a JavaScript code block', () => { - const comment = '```js\nfoo(reallyLongArg(), omgSoManyParameters(), IShouldRefactorThis(), isThereSeriouslyAnotherOne());\n```'; +// const formatComment = require('./format-comment.js'); +beforeEach(() => { + jest.resetModules(); +}); - expect(formatComment(comment)).toMatchSnapshot(); - }); +test('Runs Prettier', () => { + const comment = '```js\n__CODE__\n```'; - test('Formats a CSS code block', () => { - const comment = '```css\n.foo {color: red;}\n```'; + const format = jest.fn().mockReturnValue(''); + jest.doMock('prettier', () => ({format})); + const formatComment = require('./format-comment.js'); - expect(formatComment(comment)).toMatchSnapshot(); - }); + formatComment(comment); + expect(format).toHaveBeenCalled(); +}); - test('Formats a TypeScript code block', () => { - const comment = '```ts\ninterface ITypeScript {supported: boolean}\n```'; +test('Passes code block to Prettier', () => { + const comment = '```js\n__CODE__\n```'; - expect(formatComment(comment)).toMatchSnapshot(); - }); + const format = jest.fn().mockReturnValue(''); + jest.doMock('prettier', () => ({format})); + const formatComment = require('./format-comment.js'); - test('Formats a JSON code block', () => { - const comment = '```json\n{"supportJSON": true}\n```'; + formatComment(comment); + expect(format.mock.calls[0][0]).toBe('__CODE__'); +}); - expect(formatComment(comment)).toMatchSnapshot(); - }); +test('Passes language to Prettier via `filepath`', () => { + const comment = '```js\n__CODE__\n```'; - test('Formats a GraphQL code block', () => { - const comment = '```graphql\n{language(name: "GraphQL") {isSupported}}\n```'; + const format = jest.fn().mockReturnValue(''); + jest.doMock('prettier', () => ({format})); + const formatComment = require('./format-comment.js'); - expect(formatComment(comment)).toMatchSnapshot(); - }); + formatComment(comment); + expect(format.mock.calls[0][1].filepath).toBe('.js'); }); -test('Formats multiple code blocks', () => { - const comment = '```js\nfoo(reallyLongArg(), omgSoManyParameters(), IShouldRefactorThis(), isThereSeriouslyAnotherOne());\n```\n```js\nfoo(reallyLongArg(), omgSoManyParameters(), IShouldRefactorThis(), isThereSeriouslyAnotherOne());\n```'; +test('Outputs formatted code', () => { + const comment = '```js\n__CODE__\n```'; - expect(formatComment(comment)).toMatchSnapshot(); + const format = jest.fn().mockReturnValue('__CODE_1__'); + jest.doMock('prettier', () => ({format})); + const formatComment = require('./format-comment.js'); + + expect(formatComment(comment)).toMatch(/```js\n__CODE_1__[\s\S]*\n```$/); }); -test('Overrides notice if the comment has been formatted previously', () => { - const comment = '\n```js\nbar()\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('./format-comment.js'); - expect(formatComment(comment)).toMatchSnapshot(); + expect(formatComment(comment)).toMatch(/```js\n__CODE_1__\n```$/); }); -test('Ignores no code blocks', () => { - const comment = 'Nothing to format here\n'; +test('Outputs notice comment', () => { + const comment = '```js\n__CODE__\n```'; - expect(formatComment(comment)).toBe(comment); + const format = jest.fn().mockReturnValue(''); + jest.doMock('prettier', () => ({format})); + const formatComment = require('./format-comment.js'); + + expect(formatComment(comment)).toMatch(/^/); }); -test('Ignores incompatible code blocks', () => { - const comment = '```sh\necho "Nothing to format here"\n```'; +test('Outputs old code in notice comment', () => { + const comment = '```js\n__CODE__\n```'; - expect(formatComment(comment)).toBe(comment); + const format = jest.fn().mockReturnValue(''); + jest.doMock('prettier', () => ({format})); + const formatComment = require('./format-comment.js'); + + expect(formatComment(comment)).toMatch(/^/); }); -test('Ignores correctly formatted code blocks', () => { - const comment = '```js\nconst all = "good";\n```'; +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('./format-comment.js'); - expect(formatComment(comment)).toBe(comment); + const processedComment = formatComment(formatComment(comment)); + expect(processedComment).toMatch(/^/); + expect(processedComment).not.toMatch(//); }); -test('Ignores comments containing ``', () => { - const comment = '\n\n```js\nfoo(reallyLongArg(), omgSoManyParameters(), IShouldRefactorThis(), isThereSeriouslyAnotherOne());\n```'; +test('Ignores comments without code blocks', () => { + const comment = 'stuff and things'; + + const format = jest.fn().mockReturnValue(''); + jest.doMock('prettier', () => ({format})); + const formatComment = require('./format-comment.js'); - expect(formatComment(comment)).toBe(comment); + const processedComment = formatComment(comment); + expect(format).not.toHaveBeenCalled(); + expect(processedComment).toBe(comment); }); -describe('invalid code blocks', () => { - beforeEach(() => { - global.console = { - warn: jest.fn() - }; - }); +test('Ignores incompatible code blocks', () => { + const comment = '```sh\n__CODE__\n```'; + + const format = jest.fn().mockReturnValue(''); + jest.doMock('prettier', () => ({format})); + const formatComment = require('./format-comment.js'); - test('Warns about invalid code blocks', () => { - const comment = '```js\ninvalid javascript\n```'; - formatComment(comment); + const processedComment = formatComment(comment); + expect(format).not.toHaveBeenCalled(); + expect(processedComment).toBe(comment); +}); - expect(global.console.warn).toHaveBeenCalled(); - }); +test('Ignores comments containing ``', () => { + const comment = '\n```js\n__CODE__\n```'; - test('Ignores invalid code blocks', () => { - const comment = '```js\ninvalid javascript\n```'; + const format = jest.fn().mockReturnValue(''); + jest.doMock('prettier', () => ({format})); + const formatComment = require('./format-comment.js'); - expect(formatComment(comment)).toBe(comment); - }); + const processedComment = formatComment(comment); + expect(format).not.toHaveBeenCalled(); + expect(processedComment).toBe(comment); }); From 8c3cb434d85334ae2e49b71d39de8fde97590856 Mon Sep 17 00:00:00 2001 From: Jonas Gierer Date: Mon, 3 Jul 2017 21:34:43 +0200 Subject: [PATCH 2/2] Move tests into dedicated folder --- .../format-comment.test.js | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) rename format-comment.test.js => __tests__/format-comment.test.js (83%) diff --git a/format-comment.test.js b/__tests__/format-comment.test.js similarity index 83% rename from format-comment.test.js rename to __tests__/format-comment.test.js index 299f9dc..c35baba 100644 --- a/format-comment.test.js +++ b/__tests__/format-comment.test.js @@ -1,4 +1,5 @@ -// const formatComment = require('./format-comment.js'); +const m = '../format-comment.js'; + beforeEach(() => { jest.resetModules(); }); @@ -8,7 +9,7 @@ test('Runs Prettier', () => { const format = jest.fn().mockReturnValue(''); jest.doMock('prettier', () => ({format})); - const formatComment = require('./format-comment.js'); + const formatComment = require(m); formatComment(comment); expect(format).toHaveBeenCalled(); @@ -19,7 +20,7 @@ test('Passes code block to Prettier', () => { const format = jest.fn().mockReturnValue(''); jest.doMock('prettier', () => ({format})); - const formatComment = require('./format-comment.js'); + const formatComment = require(m); formatComment(comment); expect(format.mock.calls[0][0]).toBe('__CODE__'); @@ -30,7 +31,7 @@ test('Passes language to Prettier via `filepath`', () => { const format = jest.fn().mockReturnValue(''); jest.doMock('prettier', () => ({format})); - const formatComment = require('./format-comment.js'); + const formatComment = require(m); formatComment(comment); expect(format.mock.calls[0][1].filepath).toBe('.js'); @@ -41,7 +42,7 @@ test('Outputs formatted code', () => { const format = jest.fn().mockReturnValue('__CODE_1__'); jest.doMock('prettier', () => ({format})); - const formatComment = require('./format-comment.js'); + const formatComment = require(m); expect(formatComment(comment)).toMatch(/```js\n__CODE_1__[\s\S]*\n```$/); }); @@ -51,7 +52,7 @@ test('Trims trailing newline', () => { const format = jest.fn().mockReturnValue('__CODE_1__\n'); jest.doMock('prettier', () => ({format})); - const formatComment = require('./format-comment.js'); + const formatComment = require(m); expect(formatComment(comment)).toMatch(/```js\n__CODE_1__\n```$/); }); @@ -61,7 +62,7 @@ test('Outputs notice comment', () => { const format = jest.fn().mockReturnValue(''); jest.doMock('prettier', () => ({format})); - const formatComment = require('./format-comment.js'); + const formatComment = require(m); expect(formatComment(comment)).toMatch(/^/); }); @@ -71,7 +72,7 @@ test('Outputs old code in notice comment', () => { const format = jest.fn().mockReturnValue(''); jest.doMock('prettier', () => ({format})); - const formatComment = require('./format-comment.js'); + const formatComment = require(m); expect(formatComment(comment)).toMatch(/^/); }); @@ -81,7 +82,7 @@ test('Overrides existing notice comments', () => { const format = jest.fn().mockReturnValueOnce('__CODE_1__').mockReturnValue(''); jest.doMock('prettier', () => ({format})); - const formatComment = require('./format-comment.js'); + const formatComment = require(m); const processedComment = formatComment(formatComment(comment)); expect(processedComment).toMatch(/^/); @@ -93,7 +94,7 @@ test('Ignores comments without code blocks', () => { const format = jest.fn().mockReturnValue(''); jest.doMock('prettier', () => ({format})); - const formatComment = require('./format-comment.js'); + const formatComment = require(m); const processedComment = formatComment(comment); expect(format).not.toHaveBeenCalled(); @@ -105,7 +106,7 @@ test('Ignores incompatible code blocks', () => { const format = jest.fn().mockReturnValue(''); jest.doMock('prettier', () => ({format})); - const formatComment = require('./format-comment.js'); + const formatComment = require(m); const processedComment = formatComment(comment); expect(format).not.toHaveBeenCalled(); @@ -117,7 +118,7 @@ test('Ignores comments containing ``', () => { const format = jest.fn().mockReturnValue(''); jest.doMock('prettier', () => ({format})); - const formatComment = require('./format-comment.js'); + const formatComment = require(m); const processedComment = formatComment(comment); expect(format).not.toHaveBeenCalled();