Skip to content

Commit

Permalink
Use jest-preset-stylelint
Browse files Browse the repository at this point in the history
  • Loading branch information
BPScott committed Nov 9, 2021
1 parent db6de41 commit 26b8422
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 203 deletions.
204 changes: 1 addition & 203 deletions jest-setup.js
Original file line number Diff line number Diff line change
@@ -1,205 +1,3 @@
const util = require('util');
const {basicChecks, lint} = require('stylelint');
const getTestRule = require('jest-preset-stylelint/getTestRule');

global.testRule = getTestRule({plugins: ['./']});

/**
* @typedef {Object} TestCase
* @property {string} code
* @property {string} [description]
* @property {boolean} [only]
* @property {boolean} [skip]
*/

/**
* @typedef {Object} TestSchema
* @property {string} ruleName
* @property {any} config
* @property {TestCase[]} accept
* @property {TestCase[]} reject
* @property {string | string[]} plugins
* @property {boolean} [skipBasicChecks]
* @property {boolean} [fix]
* @property {Syntax} [customSyntax] - PostCSS Syntax (https://postcss.org/api/#syntax)
* @property {boolean} [only]
* @property {boolean} [skip]
*/

function getTestRule(options = {}) {
/**
* @param {TestSchema} schema
*/
return function testRule(schema) {
describe(`${schema.ruleName}`, () => {
const stylelintConfig = {
plugins: options.plugins || schema.plugins,
rules: {
[schema.ruleName]: schema.config,
},
};

let passingTestCases = schema.accept || [];

if (!schema.skipBasicChecks) {
passingTestCases = passingTestCases.concat(basicChecks);
}

setupTestCases({
name: 'accept',
cases: passingTestCases,
schema,
comparisons: (testCase) => async () => {
const stylelintOptions = {
code: testCase.code,
config: stylelintConfig,
customSyntax: schema.customSyntax,
codeFilename: schema.codeFilename,
};

const output = await lint(stylelintOptions);

expect(output.results[0].warnings).toEqual([]);
expect(output.results[0].parseErrors).toEqual([]);

if (!schema.fix) return;

// Check that --fix doesn't change code
const outputAfterFix = await lint({...stylelintOptions, fix: true});
const fixedCode = getOutputCss(outputAfterFix);

expect(fixedCode).toBe(testCase.code);
},
});

setupTestCases({
name: 'reject',
cases: schema.reject,
schema,
comparisons: (testCase) => async () => {
const stylelintOptions = {
code: testCase.code,
config: stylelintConfig,
customSyntax: schema.customSyntax,
codeFilename: schema.codeFilename,
};

const outputAfterLint = await lint(stylelintOptions);

const actualWarnings = outputAfterLint.results[0].warnings;

expect(outputAfterLint.results[0].parseErrors).toEqual([]);
expect(actualWarnings).toHaveLength(
testCase.warnings ? testCase.warnings.length : 1
);

(testCase.warnings || [testCase]).forEach((expected, i) => {
const warning = actualWarnings[i];

expect(expected).toHaveMessage();

expect(warning.text).toBe(expected.message);

if (expected.line !== undefined) {
expect(warning.line).toBe(expected.line);
}

if (expected.column !== undefined) {
expect(warning.column).toBe(expected.column);
}
});

if (!schema.fix) return;

// Check that --fix doesn't change code
if (
schema.fix &&
!testCase.fixed &&
testCase.fixed !== '' &&
!testCase.unfixable
) {
throw new Error(
'If using { fix: true } in test schema, all reject cases must have { fixed: .. }'
);
}

const outputAfterFix = await lint({...stylelintOptions, fix: true});

const fixedCode = getOutputCss(outputAfterFix);

if (!testCase.unfixable) {
expect(fixedCode).toBe(testCase.fixed);
expect(fixedCode).not.toBe(testCase.code);
} else {
// can't fix
if (testCase.fixed) {
expect(fixedCode).toBe(testCase.fixed);
}

expect(fixedCode).toBe(testCase.code);
}

// Checks whether only errors other than those fixed are reported
const outputAfterLintOnFixedCode = await lint({
...stylelintOptions,
code: fixedCode,
});

expect(outputAfterLintOnFixedCode.results[0].warnings).toEqual(
outputAfterFix.results[0].warnings
);
expect(outputAfterLintOnFixedCode.results[0].parseErrors).toEqual([]);
},
});
});

expect.extend({
toHaveMessage(testCase) {
if (testCase.message === undefined) {
return {
message: () =>
'Expected "reject" test case to have a "message" property',
pass: false,
};
}

return {
pass: true,
};
},
});
};
}

function setupTestCases({name, cases, schema, comparisons}) {
if (cases && cases.length) {
const testGroup = schema.only
? describe.only
: schema.skip
? describe.skip
: describe;

testGroup(`${name}`, () => {
cases.forEach((testCase) => {
if (testCase) {
const spec = testCase.only ? it.only : testCase.skip ? it.skip : it;

describe(`${util.inspect(schema.config)}`, () => {
describe(`${util.inspect(testCase.code)}`, () => {
spec(
testCase.description || 'no description',
comparisons(testCase)
);
});
});
}
});
});
}
}

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

return css;
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^4.0.0",
"jest": "^27.3.1",
"jest-preset-stylelint": "^4.2.0",
"postcss": "^8.3.11",
"postcss-html": "^1.2.0",
"postcss-markdown": "^1.1.0",
Expand All @@ -58,6 +59,7 @@
"node": "^12.22.0 || ^14.17.0 || >=16.0.0"
},
"jest": {
"preset": "jest-preset-stylelint",
"setupFiles": [
"./jest-setup.js"
]
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2241,6 +2241,11 @@ jest-pnp-resolver@^1.2.2:
resolved "https://registry.yarnpkg.com/jest-pnp-resolver/-/jest-pnp-resolver-1.2.2.tgz#b704ac0ae028a89108a4d040b3f919dfddc8e33c"
integrity sha512-olV41bKSMm8BdnuMsewT4jqlZ8+3TCARAXjZGT9jcoSnrfUnRCqnMoF9XEeoWjbzObpqF9dRhHQj0Xb9QdF6/w==

jest-preset-stylelint@^4.2.0:
version "4.2.0"
resolved "https://registry.yarnpkg.com/jest-preset-stylelint/-/jest-preset-stylelint-4.2.0.tgz#b1efe821bf525e5fd364e4ac3416d381391d13c9"
integrity sha512-BzkoD/qyB5dsTOAMTC1YR0ii4iJvqg2KFXXUl6qjoeyODAAYzYEw1xAHxvCG8fvep2rKemMgfP0FTKVzAruDRQ==

jest-regex-util@^27.0.6:
version "27.0.6"
resolved "https://registry.yarnpkg.com/jest-regex-util/-/jest-regex-util-27.0.6.tgz#02e112082935ae949ce5d13b2675db3d8c87d9c5"
Expand Down

0 comments on commit 26b8422

Please sign in to comment.