Skip to content

Commit

Permalink
test(jest-setup): add linter
Browse files Browse the repository at this point in the history
  • Loading branch information
ayshiff committed Mar 15, 2020
1 parent 655ec11 commit f1fdc7c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
1 change: 0 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ dist
node_modules
build
jest.config.js
jest-setup.js
jest.transform.js
website-1.x/
website/
Expand Down
35 changes: 20 additions & 15 deletions jest-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,16 @@
* LICENSE file in the root directory of this source tree.
*/

const _ = require('lodash');
// eslint-disable-next-line import/no-extraneous-dependencies
const stylelint = require('stylelint');

function getOutputCss(output) {
const result = output.results[0]._postcssResult;
const css = result.root.toString(result.opts.syntax);

return css;
}

global.testRule = (rule, schema) => {
describe(schema.ruleName, () => {
const stylelintConfig = {
Expand Down Expand Up @@ -40,11 +47,14 @@ global.testRule = (rule, schema) => {
}

// Check the fix
return stylelint.lint({...options, fix: true}).then(output2 => {
const fixedCode = getOutputCss(output2);
// eslint-disable-next-line consistent-return
return stylelint
.lint({...options, fix: true})
.then(fixedOutput => {
const fixedCode = getOutputCss(fixedOutput);

expect(fixedCode).toBe(testCase.code);
});
expect(fixedCode).toBe(testCase.code);
});
});
});
});
Expand All @@ -54,6 +64,7 @@ global.testRule = (rule, schema) => {
if (schema.reject && schema.reject.length) {
describe('reject', () => {
schema.reject.forEach(testCase => {
// eslint-disable-next-line no-nested-ternary
const spec = testCase.only ? it.only : testCase.skip ? it.skip : it;

spec(checkTestCaseContent(testCase), () => {
Expand All @@ -71,15 +82,15 @@ global.testRule = (rule, schema) => {
expect(testCase).toHaveMessage();

if (testCase.message !== undefined) {
expect(_.get(warning, 'text')).toBe(testCase.message);
expect(warning.text).toBe(testCase.message);
}

if (testCase.line !== undefined) {
expect(_.get(warning, 'line')).toBe(testCase.line);
expect(warning.line).toBe(testCase.line);
}

if (testCase.column !== undefined) {
expect(_.get(warning, 'column')).toBe(testCase.column);
expect(warning.column).toBe(testCase.column);
}

if (!schema.fix) {
Expand All @@ -92,6 +103,7 @@ global.testRule = (rule, schema) => {
);
}
// Check the fix
// eslint-disable-next-line consistent-return
return stylelint
.lint({...options, fix: true})
.then(fixedOutput => {
Expand Down Expand Up @@ -121,10 +133,3 @@ global.testRule = (rule, schema) => {
});
});
};

function getOutputCss(output) {
const result = output.results[0]._postcssResult;
const css = result.root.toString(result.opts.syntax);

return css;
}

0 comments on commit f1fdc7c

Please sign in to comment.