Skip to content

Commit

Permalink
fix: workaround missing options validation with jest styleint preset
Browse files Browse the repository at this point in the history
  • Loading branch information
AndyOGo committed Feb 15, 2023
1 parent 54e5330 commit c25950b
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 20 deletions.
38 changes: 38 additions & 0 deletions jest.setup.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,42 @@
/* eslint-disable @typescript-eslint/no-var-requires */
const { getTestRule } = require('jest-preset-stylelint');
const { lint } = require('stylelint');

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

global.testOptions = testOptions;

function testOptions({ ruleName, config, reject }) {
// eslint-disable-next-line no-undef
describe(ruleName, () => {
// eslint-disable-next-line no-undef
it('warn for invalid options', async () => {
const rejections = await Promise.all(
reject.map(async ({ code, message }) => {
const {
results: [{ invalidOptionWarnings }],
} = await lint({
code,
config: {
plugins: ['./src/index.ts'],
rules: {
[ruleName]: config,
},
},
});

return { message, invalidOptionWarnings };
})
);

rejections.forEach(({ message, invalidOptionWarnings }) => {
const expectedWarning = {
text: message,
};

// eslint-disable-next-line no-undef
expect(invalidOptionWarnings[0]).toMatchObject(expectedWarning);
});
});
});
}
8 changes: 4 additions & 4 deletions test/auto-fix-func.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ testRule(
}
);

testRule({
testOptions({
skip: true,
ruleName,

Expand All @@ -170,12 +170,12 @@ testRule({
reject: [
{
code: '.foo { color: red; }',
message: `Invalid option "{"autoFixFunc":true}" for rule ${ruleName}`,
message: `Invalid option "{"autoFixFunc":true}" for rule "${ruleName}"`,
},
],
});

testRule({
testOptions({
skip: true,
ruleName,

Expand All @@ -189,7 +189,7 @@ testRule({
reject: [
{
code: '.foo { color: red; }',
message: `Invalid option "{"disableFix":1234}" for rule ${ruleName}`,
message: `Invalid option "{"disableFix":1234}" for rule "${ruleName}"`,
},
],
});
4 changes: 2 additions & 2 deletions test/custom-message.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ testRule({
],
});

testRule({
testOptions({
skip: true,
ruleName,

Expand All @@ -64,7 +64,7 @@ testRule({
reject: [
{
code: '.foo { color: red; }',
message: `Invalid option "{"message":1234}" for rule ${ruleName}`,
message: `Invalid option "{"message":1234}" for rule "${ruleName}"`,
},
],
});
4 changes: 2 additions & 2 deletions test/ignore-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ testRule({
],
});

testRule({
testOptions({
skip: true,
ruleName,

Expand All @@ -241,7 +241,7 @@ testRule({
reject: [
{
code: '.foo { color: red; }',
message: `Invalid option "{"ignoreFunctions":"foo"}" for rule ${ruleName}`,
message: `Invalid option "{"ignoreFunctions":"foo"}" for rule "${ruleName}"`,
},
],
});
4 changes: 2 additions & 2 deletions test/ignore-variables.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ testRule({
],
});

testRule({
testOptions({
skip: true,
ruleName,

Expand All @@ -262,7 +262,7 @@ testRule({
reject: [
{
code: '.foo { color: red; }',
message: `Invalid option "{"ignoreVariables":"foo"}" for rule ${ruleName}`,
message: `Invalid option "{"ignoreVariables":"foo"}" for rule "${ruleName}"`,
},
],
});
4 changes: 2 additions & 2 deletions test/severity.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ testRule({
],
});

testRule({
testOptions({
skip: true,
ruleName,

Expand All @@ -41,7 +41,7 @@ testRule({
reject: [
{
code: '.foo { color: red; }',
message: `Invalid option "{"severity":1234}" for rule ${ruleName}`,
message: `Invalid option "{"severity":1234}" for rule "${ruleName}"`,
},
],
});
8 changes: 4 additions & 4 deletions test/shorthand.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ testRule({
],
});

testRule({
testOptions({
skip: true,
ruleName,

Expand All @@ -318,12 +318,12 @@ testRule({
reject: [
{
code: '.foo { border: red; }',
message: `Invalid option "{"expandShorthand":"foo"}" for rule ${ruleName}`,
message: `Invalid option "{"expandShorthand":"foo"}" for rule "${ruleName}"`,
},
],
});

testRule({
testOptions({
skip: true,
ruleName,

Expand All @@ -337,7 +337,7 @@ testRule({
reject: [
{
code: '.foo { border: red; }',
message: `Invalid option "{"recurseLonghand":"foo"}" for rule ${ruleName}`,
message: `Invalid option "{"recurseLonghand":"foo"}" for rule "${ruleName}"`,
},
],
});
4 changes: 2 additions & 2 deletions test/single-keyword.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ testRule({
],
});

testRule({
testOptions({
skip: true,
ruleName,

Expand All @@ -85,7 +85,7 @@ testRule({
reject: [
{
code: '.foo { color: red; }',
message: `Invalid option "{"ignoreKeywords":true}" for rule ${ruleName}`,
message: `Invalid option "{"ignoreKeywords":true}" for rule "${ruleName}"`,
},
],
});
4 changes: 2 additions & 2 deletions test/single-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ testRule({
],
});

testRule({
testOptions({
skip: true,
ruleName,

Expand All @@ -132,7 +132,7 @@ testRule({
reject: [
{
code: '.foo { color: red; }',
message: `Invalid option "{"ignoreValues":true}" for rule ${ruleName}`,
message: `Invalid option "{"ignoreValues":true}" for rule "${ruleName}"`,
},
],
});

0 comments on commit c25950b

Please sign in to comment.