mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-10 15:47:23 +02:00
misc: update stylelint rule tester
This commit is contained in:
parent
b4f4057d97
commit
c50df3003c
4 changed files with 72 additions and 85 deletions
13
.eslintrc.js
13
.eslintrc.js
|
@ -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.',
|
|
||||||
},
|
|
||||||
' *',
|
' *',
|
||||||
{
|
|
||||||
pattern:
|
|
||||||
' * This source code is licensed under the MIT license found in the',
|
' * This source code is licensed under the MIT license found in the',
|
||||||
},
|
' * LICENSE file in the root directory of this source tree.',
|
||||||
{
|
|
||||||
pattern: ' * LICENSE file in the root directory of this source tree.',
|
|
||||||
},
|
|
||||||
' ',
|
' ',
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
|
|
|
@ -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'],
|
||||||
};
|
};
|
||||||
|
|
|
@ -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',
|
|
@ -9,7 +9,15 @@ const rule = require('..');
|
||||||
|
|
||||||
const {ruleName, messages} = rule;
|
const {ruleName, messages} = rule;
|
||||||
|
|
||||||
testRule(rule, {
|
testStylelintRule(
|
||||||
|
{
|
||||||
|
// Relative to repo root.
|
||||||
|
plugins: ['./packages/stylelint-copyright'],
|
||||||
|
rules: {
|
||||||
|
[ruleName]: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
ruleName,
|
ruleName,
|
||||||
fix: false,
|
fix: false,
|
||||||
accept: [
|
accept: [
|
||||||
|
@ -57,4 +65,5 @@ testRule(rule, {
|
||||||
column: 1,
|
column: 1,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
});
|
},
|
||||||
|
);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue