-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Use jest-preset-stylelint * Update tests * Update to use custom syntax * Use PostCSS 8 and latest postcss-scss * Use Stylelint 14 from v14 branch * Use default config for rules * Skip HTML tests * Use `testRule` for all tests * Update GitHub actions * Upgrade dependencies * Revert two suspicious tests * Add testing for nested declarations Co-authored-by: Aleks Hudochenkov <[email protected]>
- Loading branch information
1 parent
9b1b978
commit c167e8c
Showing
62 changed files
with
6,465 additions
and
5,809 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,163 +1,3 @@ | ||
"use strict"; // eslint-disable-line strict | ||
const getTestRule = require("jest-preset-stylelint/getTestRule"); | ||
|
||
const _ = require("lodash"); | ||
const stylelint = require("stylelint"); | ||
|
||
global.testRule = (rule, schema) => { | ||
expect.extend({ | ||
toHaveMessage(testCase) { | ||
if (testCase.message === undefined) { | ||
return { | ||
message: () => | ||
'Expected "reject" test case to have a "message" property', | ||
pass: false | ||
}; | ||
} | ||
|
||
return { | ||
pass: true | ||
}; | ||
} | ||
}); | ||
|
||
// eslint-disable-next-line jest/valid-describe | ||
describe(`${schema.ruleName}`, () => { | ||
const stylelintConfig = { | ||
plugins: ["./src"], | ||
rules: { | ||
[schema.ruleName]: schema.config | ||
} | ||
}; | ||
|
||
if (schema.accept && schema.accept.length) { | ||
describe("accept", () => { | ||
schema.accept.forEach(testCase => { | ||
const spec = testCase.only ? it.only : it; | ||
|
||
spec(testCase.description || "no description", () => { | ||
const options = { | ||
code: testCase.code, | ||
config: stylelintConfig, | ||
syntax: schema.syntax | ||
}; | ||
|
||
return stylelint.lint(options).then(output => { | ||
expect(output.results[0].warnings).toEqual([]); | ||
|
||
if (!schema.fix) { | ||
return; | ||
} | ||
|
||
// Check the fix | ||
return stylelint | ||
.lint(Object.assign({ fix: true }, options)) | ||
.then(output2 => { | ||
const fixedCode = getOutputCss(output2); | ||
|
||
expect(fixedCode).toBe(testCase.code); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
} | ||
|
||
if (schema.reject && schema.reject.length) { | ||
describe("reject", () => { | ||
schema.reject.forEach(testCase => { | ||
const spec = testCase.only ? it.only : it; | ||
|
||
spec(testCase.description || "no description", () => { | ||
const options = { | ||
code: testCase.code, | ||
config: stylelintConfig, | ||
syntax: schema.syntax | ||
}; | ||
|
||
return stylelint.lint(options).then(output => { | ||
const warnings = output.results[0].warnings; | ||
const warning = warnings[0]; | ||
|
||
expect(warnings.length).toBeGreaterThanOrEqual(1); | ||
// expect(testCase).toHaveMessage(); | ||
|
||
if (testCase.message !== undefined) { | ||
expect(_.get(warning, "text")).toBe(testCase.message); | ||
} | ||
|
||
if (testCase.line !== undefined) { | ||
expect(_.get(warning, "line")).toBe(testCase.line); | ||
} | ||
|
||
if (testCase.column !== undefined) { | ||
expect(_.get(warning, "column")).toBe(testCase.column); | ||
} | ||
|
||
if (!schema.fix) { | ||
return; | ||
} | ||
|
||
if (!testCase.fixed) { | ||
throw new Error( | ||
"If using { fix: true } in test schema, all reject cases must have { fixed: .. }" | ||
); | ||
} | ||
|
||
// Check the fix | ||
return stylelint | ||
.lint(Object.assign({ fix: true }, options)) | ||
.then(output2 => { | ||
const fixedCode = getOutputCss(output2); | ||
|
||
expect(fixedCode).toBe(testCase.fixed); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
} | ||
}); | ||
}; | ||
|
||
function getOutputCss(output) { | ||
const result = output.results[0]._postcssResult; | ||
const css = result.root.toString(result.opts.syntax); | ||
|
||
return css; | ||
} | ||
|
||
global.testConfig = input => { | ||
let testFn; | ||
|
||
if (input.only) { | ||
testFn = test.only; | ||
} else if (input.skip) { | ||
testFn = test.skip; | ||
} else { | ||
testFn = test; | ||
} | ||
|
||
testFn(input.description, () => { | ||
const config = { | ||
plugins: ["./"], | ||
rules: { | ||
[input.ruleName]: input.config | ||
} | ||
}; | ||
|
||
return stylelint | ||
.lint({ | ||
code: "", | ||
config | ||
}) | ||
.then(data => { | ||
const invalidOptionWarnings = data.results[0].invalidOptionWarnings; | ||
|
||
if (input.valid) { | ||
expect(invalidOptionWarnings).toHaveLength(0); | ||
} else { | ||
expect(invalidOptionWarnings[0].text).toBe(input.message); | ||
} | ||
}); | ||
}); | ||
}; | ||
global.testRule = getTestRule({ plugins: ["./src"] }); |
Oops, something went wrong.