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,
},
globals: {
testRule: true,
testStylelintRule: true,
},
extends: ['airbnb', 'prettier', 'prettier/react'],
plugins: ['react-hooks', 'header'],
@ -39,17 +39,10 @@ module.exports = {
'block',
[
'*',
{
pattern: ' * Copyright \\(c\\) Facebook, Inc. and its affiliates.',
},
' * Copyright (c) Facebook, Inc. and its affiliates.',
' *',
{
pattern:
' * 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',
' * LICENSE file in the root directory of this source tree.',
' ',
],
],

View file

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

View file

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