Skip to content

Commit

Permalink
Refactor formatter tests (#4988)
Browse files Browse the repository at this point in the history
* Refactor formatter tests

I think that using `stripIndent()` or adding extra indent for the expected output
of the formatters leads to inaccuracy and poor readability.
So, this change removes needless indent from the expected output
in `lib/formatters/__tests__/*.test.js`.

* Use `.trimStart()` for readability
  • Loading branch information
ybiquitous committed Oct 16, 2020
1 parent fd6e13b commit 6ad94ac
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 119 deletions.
15 changes: 5 additions & 10 deletions lib/formatters/__tests__/compactFormatter.test.js
Expand Up @@ -2,7 +2,6 @@

const compactFormatter = require('../compactFormatter');
const prepareFormatterOutput = require('./prepareFormatterOutput');
const stripIndent = require('common-tags').stripIndent;

describe('compactFormatter', () => {
let actualTTY;
Expand Down Expand Up @@ -55,9 +54,7 @@ describe('compactFormatter', () => {

const output = prepareFormatterOutput(results, compactFormatter);

expect(output).toBe(stripIndent`
path/to/file.css: line 1, col 1, error - Unexpected foo
`);
expect(output).toBe('path/to/file.css: line 1, col 1, error - Unexpected foo');
});

it('outputs warnings without stdout `TTY`', () => {
Expand All @@ -83,9 +80,7 @@ describe('compactFormatter', () => {

const output = prepareFormatterOutput(results, compactFormatter);

expect(output).toBe(stripIndent`
path/to/file.css: line 1, col 1, error - Unexpected foo
`);
expect(output).toBe('path/to/file.css: line 1, col 1, error - Unexpected foo');
});

it('output warnings with more than 80 characters and `process.stdout.columns` equal 90 characters', () => {
Expand Down Expand Up @@ -114,9 +109,9 @@ describe('compactFormatter', () => {

const output = prepareFormatterOutput(results, compactFormatter);

expect(output).toBe(stripIndent`
path/to/file.css: line 1, col 1, error - Unexpected very very very very very very very very very very very very very long foo
`);
expect(output).toBe(
'path/to/file.css: line 1, col 1, error - Unexpected very very very very very very very very very very very very very long foo',
);
});

it('handles ignored file', () => {
Expand Down
Expand Up @@ -2,7 +2,6 @@

const disableOptionsReportStringFormatterTest = require('../disableOptionsReportStringFormatter');
const stripAnsi = require('strip-ansi');
const stripIndent = require('common-tags').stripIndent;

describe('disableOptionsReportStringFormatter', () => {
it('formatter stringified', () => {
Expand Down Expand Up @@ -32,16 +31,15 @@ describe('disableOptionsReportStringFormatter', () => {
),
);

let expected = stripIndent`
foo
wrong disable: baz, start line: 1, end line: 3
wrong disable: all, start line: 7
const expected = `
foo
wrong disable: baz, start line: 1, end line: 3
wrong disable: all, start line: 7
bar
wrong disable: all, start line: 19, end line: 33
wrong disable: baz, start line: 99, end line: 102`;

expected = `\n${expected}\n`;
bar
wrong disable: all, start line: 19, end line: 33
wrong disable: baz, start line: 99, end line: 102
`;

expect(actual).toBe(expected);
});
Expand Down
46 changes: 26 additions & 20 deletions lib/formatters/__tests__/stringFormatter.test.js
Expand Up @@ -2,7 +2,6 @@

const prepareFormatterOutput = require('./prepareFormatterOutput');
const stringFormatter = require('../stringFormatter');
const stripIndent = require('common-tags').stripIndent;

describe('stringFormatter', () => {
let actualTTY;
Expand Down Expand Up @@ -55,10 +54,11 @@ describe('stringFormatter', () => {

const output = prepareFormatterOutput(results, stringFormatter);

expect(output).toBe(stripIndent`
path/to/file.css
1:1 × Unexpected foo bar
`);
expect(output).toBe(
`
path/to/file.css
1:1 × Unexpected foo bar`.trimStart(),
);
});

it('outputs warnings without stdout `TTY`', () => {
Expand All @@ -84,9 +84,11 @@ describe('stringFormatter', () => {

const output = prepareFormatterOutput(results, stringFormatter);

expect(output).toBe(stripIndent`
path/to/file.css
1:1 × Unexpected foo bar`);
expect(output).toBe(
`
path/to/file.css
1:1 × Unexpected foo bar`.trimStart(),
);
});

it('output warnings with more than 80 characters and `process.stdout.columns` equal 90 characters', () => {
Expand Down Expand Up @@ -115,11 +117,12 @@ describe('stringFormatter', () => {

const output = prepareFormatterOutput(results, stringFormatter);

expect(output).toBe(stripIndent`
path/to/file.css
1:1 × Unexpected very very very very very very very bar-very-very-very-very-very-long
very very very very very very long foo
`);
expect(output).toBe(
`
path/to/file.css
1:1 × Unexpected very very very very very very very bar-very-very-very-very-very-long
very very very very very very long foo`.trimStart(),
);
});

it('condenses deprecations and invalid option warnings', () => {
Expand Down Expand Up @@ -160,11 +163,12 @@ describe('stringFormatter', () => {

const output = prepareFormatterOutput(results, stringFormatter);

expect(output).toBe(stripIndent`
Invalid Option: Unexpected option for baz
expect(output).toBe(
`
Invalid Option: Unexpected option for baz
Deprecation Warning: Deprecated foo See: bar
`);
Deprecation Warning: Deprecated foo See: bar`.trimStart(),
);
});

it('handles ignored file', () => {
Expand Down Expand Up @@ -204,8 +208,10 @@ describe('stringFormatter', () => {

const output = prepareFormatterOutput(results, stringFormatter);

expect(output).toBe(stripIndent`
path/to/file.css
1:1 × bar`);
expect(output).toBe(
`
path/to/file.css
1:1 × bar`.trimStart(),
);
});
});
30 changes: 16 additions & 14 deletions lib/formatters/__tests__/unixFormatter.test.js
@@ -1,7 +1,6 @@
'use strict';

const prepareFormatterOutput = require('./prepareFormatterOutput');
const stripIndent = require('common-tags').stripIndent;
const unixFormatter = require('../unixFormatter');

describe('unixFormatter', () => {
Expand Down Expand Up @@ -62,12 +61,13 @@ describe('unixFormatter', () => {

const output = prepareFormatterOutput(results, unixFormatter);

expect(output).toBe(stripIndent`
path/to/file.css:1:1: Unexpected foo [error]
path/to/file.css:10:1: Unexpected foo 2 [error]
expect(output).toBe(
`
path/to/file.css:1:1: Unexpected foo [error]
path/to/file.css:10:1: Unexpected foo 2 [error]
2 problems
`);
2 problems`.trimStart(),
);
});

it('outputs warnings without stdout `TTY`', () => {
Expand All @@ -93,11 +93,12 @@ describe('unixFormatter', () => {

const output = prepareFormatterOutput(results, unixFormatter);

expect(output).toBe(stripIndent`
path/to/file.css:1:1: Unexpected foo [error]
expect(output).toBe(
`
path/to/file.css:1:1: Unexpected foo [error]
1 problem
`);
1 problem`.trimStart(),
);
});

it('output warnings with more than 80 characters and `process.stdout.columns` equal 90 characters', () => {
Expand Down Expand Up @@ -126,11 +127,12 @@ describe('unixFormatter', () => {

const output = prepareFormatterOutput(results, unixFormatter);

expect(output).toBe(stripIndent`
path/to/file.css:1:1: Unexpected very very very very very very very very very very very very very long foo [error]
expect(output).toBe(
`
path/to/file.css:1:1: Unexpected very very very very very very very very very very very very very long foo [error]
1 problem
`);
1 problem`.trimStart(),
);
});

it('handles ignored file', () => {
Expand Down

0 comments on commit 6ad94ac

Please sign in to comment.