misc: update stylelint rule tester

This commit is contained in:
Yangshun Tay 2020-03-21 15:22:25 +08:00
parent b4f4057d97
commit c50df3003c
4 changed files with 72 additions and 85 deletions

View file

@ -21,7 +21,7 @@ module.exports = {
allowImportExportEverywhere: true, allowImportExportEverywhere: true,
}, },
globals: { globals: {
testRule: true, testStylelintRule: true,
}, },
extends: ['airbnb', 'prettier', 'prettier/react'], extends: ['airbnb', 'prettier', 'prettier/react'],
plugins: ['react-hooks', 'header'], plugins: ['react-hooks', 'header'],
@ -39,17 +39,10 @@ module.exports = {
'block', 'block',
[ [
'*', '*',
{ ' * Copyright (c) Facebook, Inc. and its affiliates.',
pattern: ' * Copyright \\(c\\) Facebook, Inc. and its affiliates.',
},
' *', ' *',
{ ' * This source code is licensed under the MIT license found in the',
pattern: ' * LICENSE file in the root directory of this source tree.',
' * This source code is licensed under the MIT license found in the',
},
{
pattern: ' * LICENSE file in the root directory of this source tree.',
},
' ', ' ',
], ],
], ],

View file

@ -27,5 +27,5 @@ module.exports = {
transform: { transform: {
'^.+\\.[jt]sx?$': 'babel-jest', '^.+\\.[jt]sx?$': 'babel-jest',
}, },
setupFiles: ['./jest-setup.js'], setupFiles: ['./jest/stylelint-rule-test.js'],
}; };

View file

@ -10,68 +10,55 @@ const stylelint = require('stylelint');
function getOutputCss(output) { function getOutputCss(output) {
const result = output.results[0]._postcssResult; const result = output.results[0]._postcssResult;
const css = result.root.toString(result.opts.syntax); return result.root.toString(result.opts.syntax);
return css;
} }
global.testRule = (rule, schema) => { global.testStylelintRule = (config, tests) => {
describe(schema.ruleName, () => { describe(tests.ruleName, () => {
const stylelintConfig = {
plugins: ['./packages/stylelint-copyright'],
rules: {
[schema.ruleName]: schema.config,
},
};
const checkTestCaseContent = testCase => const checkTestCaseContent = testCase =>
testCase.description || testCase.code || 'no description'; testCase.description || testCase.code || 'no description';
if (schema.accept && schema.accept.length) { if (tests.accept && tests.accept.length) {
describe('accept', () => { describe('accept cases', () => {
schema.accept.forEach(testCase => { tests.accept.forEach(testCase => {
const spec = testCase.only ? it.only : it; const spec = testCase.only ? it.only : it;
spec(checkTestCaseContent(testCase), () => { spec(checkTestCaseContent(testCase), () => {
const options = { const options = {
code: testCase.code, code: testCase.code,
config: stylelintConfig, config,
syntax: schema.syntax, syntax: tests.syntax,
}; };
return stylelint.lint(options).then(output => { return stylelint.lint(options).then(output => {
expect(output.results[0].warnings).toEqual([]); expect(output.results[0].warnings).toEqual([]);
if (!schema.fix) { if (!tests.fix) {
return; return null;
} }
// Check the fix // Check the fix.
// eslint-disable-next-line consistent-return
return stylelint return stylelint
.lint({...options, fix: true}) .lint({...options, fix: true})
.then(fixedOutput => { .then(fixedOutput => getOutputCss(fixedOutput))
const fixedCode = getOutputCss(fixedOutput); .then(fixedCode => expect(fixedCode).toBe(testCase.fixed));
expect(fixedCode).toBe(testCase.code);
});
}); });
}); });
}); });
}); });
} }
if (schema.reject && schema.reject.length) { if (tests.reject && tests.reject.length) {
describe('reject', () => { describe('reject cases', () => {
schema.reject.forEach(testCase => { tests.reject.forEach(testCase => {
// eslint-disable-next-line no-nested-ternary const skip = testCase.skip ? it.skip : it;
const spec = testCase.only ? it.only : testCase.skip ? it.skip : it; const spec = testCase.only ? it.only : skip;
spec(checkTestCaseContent(testCase), () => { spec(checkTestCaseContent(testCase), () => {
const options = { const options = {
code: testCase.code, code: testCase.code,
config: stylelintConfig, config,
syntax: schema.syntax, syntax: tests.syntax,
}; };
return stylelint.lint(options).then(output => { return stylelint.lint(options).then(output => {
@ -81,44 +68,42 @@ global.testRule = (rule, schema) => {
expect(warnings.length).toBeGreaterThanOrEqual(1); expect(warnings.length).toBeGreaterThanOrEqual(1);
expect(testCase).toHaveMessage(); expect(testCase).toHaveMessage();
if (testCase.message !== undefined) { if (testCase.message != null) {
expect(warning.text).toBe(testCase.message); expect(warning.text).toBe(testCase.message);
} }
if (testCase.line !== undefined) { if (testCase.line != null) {
expect(warning.line).toBe(testCase.line); expect(warning.line).toBe(testCase.line);
} }
if (testCase.column !== undefined) { if (testCase.column != null) {
expect(warning.column).toBe(testCase.column); expect(warning.column).toBe(testCase.column);
} }
if (!schema.fix) { if (!tests.fix) {
return; return null;
} }
if (!testCase.fixed) { if (!testCase.fixed) {
throw new Error( throw new Error(
'If using { fix: true } in test schema, all reject cases must have { fixed: .. }', 'If using { fix: true } in test tests, all reject cases must have { fixed: .. }',
); );
} }
// Check the fix
// eslint-disable-next-line consistent-return // Check the fix.
return stylelint return stylelint
.lint({...options, fix: true}) .lint({...options, fix: true})
.then(fixedOutput => { .then(fixedOutput => getOutputCss(fixedOutput))
const fixedCode = getOutputCss(fixedOutput); .then(fixedCode => expect(fixedCode).toBe(testCase.fixed));
expect(fixedCode).toBe(testCase.fixed);
});
}); });
}); });
}); });
}); });
} }
expect.extend({ expect.extend({
toHaveMessage(testCase) { toHaveMessage(testCase) {
if (testCase.message === undefined) { if (testCase.message == null) {
return { return {
message: () => message: () =>
'Expected "reject" test case to have a "message" property', 'Expected "reject" test case to have a "message" property',

View file

@ -9,41 +9,49 @@ const rule = require('..');
const {ruleName, messages} = rule; const {ruleName, messages} = rule;
testRule(rule, { testStylelintRule(
ruleName, {
fix: false, // Relative to repo root.
accept: [ plugins: ['./packages/stylelint-copyright'],
{ rules: {
code: ` [ruleName]: true,
},
},
{
ruleName,
fix: false,
accept: [
{
code: `
/** /**
* Copyright * Copyright
*/ */
.foo {}`, .foo {}`,
}, },
{ {
code: ` code: `
/** /**
* copyright * copyright
*/ */
.foo {}`, .foo {}`,
}, },
], ],
reject: [ reject: [
{ {
code: ` code: `
/** /**
* Copyleft * Copyleft
*/ */
.foo {}`, .foo {}`,
message: messages.rejected, message: messages.rejected,
line: 2, line: 2,
column: 1, column: 1,
}, },
{ {
code: ` code: `
/** /**
* Copyleft * Copyleft
*/ */
@ -52,9 +60,10 @@ testRule(rule, {
* Copyright * Copyright
*/ */
.foo {}`, .foo {}`,
message: messages.rejected, message: messages.rejected,
line: 2, line: 2,
column: 1, column: 1,
}, },
], ],
}); },
);