Skip to content

Commit

Permalink
Fix circular structure in exceptions (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
romainmenke authored Jan 1, 2025
1 parent ccc961a commit f5011e5
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions getTestRule.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ module.exports = function getTestRule(options = {}) {
codeFilename: testCase.codeFilename || schema.codeFilename,
};

const output = await lint(stylelintOptions);
const output = await lint(stylelintOptions).catch(formatExceptions);

expect(output.results[0].warnings).toEqual([]);
expect(output.results[0].parseErrors).toEqual([]);
Expand All @@ -51,7 +51,9 @@ module.exports = function getTestRule(options = {}) {
if (!schema.fix) return;

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

expect(fixedCode).toBe(testCase.code);
Expand All @@ -70,7 +72,7 @@ module.exports = function getTestRule(options = {}) {
codeFilename: testCase.codeFilename || schema.codeFilename,
};

const outputAfterLint = await lint(stylelintOptions);
const outputAfterLint = await lint(stylelintOptions).catch(formatExceptions);

const actualWarnings = [
...outputAfterLint.results[0].invalidOptionWarnings,
Expand Down Expand Up @@ -111,7 +113,9 @@ module.exports = function getTestRule(options = {}) {
);
}

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

const fixedCode = getOutputCss(outputAfterFix);

Expand All @@ -132,7 +136,7 @@ module.exports = function getTestRule(options = {}) {
...stylelintOptions,
code: fixedCode,
fix: testCase.unfixable,
});
}).catch(formatExceptions);

expect(outputAfterLintOnFixedCode.results[0]).toMatchObject({
warnings: outputAfterFix.results[0].warnings,
Expand Down Expand Up @@ -203,3 +207,19 @@ function getOutputCss(output) {

throw new TypeError('Invalid result');
}

/**
* @param {Error & { postcssNode?: (import('postcss').Node | Record<string, unknown>) }} error
* @throws
* @returns {never}
*/
function formatExceptions(error) {
if (error.postcssNode?.toJSON && typeof error.postcssNode.toJSON === 'function') {
// see: https://github.com/stylelint/jest-preset-stylelint/issues/130
// `postcssNode` becomes a circular plain object after structured cloning.
// Eagerly converting to JSON ensures that jest can always pass this data around between threads and ultimately format the report.
error.postcssNode = error.postcssNode.toJSON();
}

throw error;
}

0 comments on commit f5011e5

Please sign in to comment.