Skip to content

Clean up typography CSS output #1068

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion libs/@guardian/source-foundations/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@ export default {
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
coverageDirectory: '../../../coverage/libs/@guardian/source-foundations',
setupFilesAfterEnv: ['./lib/jest-matchers/toBeValidCSS.ts'],
setupFilesAfterEnv: [
'./lib/jest-matchers/toBeValidCSS.ts',
'./lib/jest-matchers/toMatchCSS.ts',
],
};
1 change: 1 addition & 0 deletions libs/@guardian/source-foundations/jest.e2e.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ import * as dist from '../../../dist/libs/@guardian/source-foundations';

// Register our custom Jest matcher.
import './lib/jest-matchers/toBeValidCSS';
import './lib/jest-matchers/toMatchCSS';

jest.mock('./src/index', () => dist);
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
declare namespace jest {
interface Matchers<R> {
toBeValidCSS(options?: ToBeValidCSSOptions): R;
toBeValidCSS(options?: CSSMatcherOptions): R;
toMatchCSS(expected: string, options?: CSSMatcherOptions): R;
}
}

type ToBeValidCSSOptions = {
type CSSMatcherOptions = {
/**
* Set this to true if the CSS is a fragment (e.g. not wrapped in a selector and valid on its own)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ expect.extend({
*/
toBeValidCSS(
received: string,
options: ToBeValidCSSOptions = {},
options: CSSMatcherOptions = {},
): jest.CustomMatcherResult {
const { isFragment = false } = options;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import type { Warning } from 'lightningcss';
import { transform } from 'lightningcss';

expect.extend({
/**
* Uses lightningcss library to normalise and compare given CSS
*
* @param received - The CSS to compare
* @param options - Specify whether the CSS being compared is a fragment (not wrapped in a selector)
*/
toMatchCSS(
received: string,
expected: string,
options: CSSMatcherOptions = {},
): jest.CustomMatcherResult {
const { isFragment = false } = options;

// We wrap the CSS in a selector if it is a fragment to ensure it is valid.
const recievedCSS = isFragment ? `* { ${received} }` : received;
const expectedCSS = isFragment ? `* { ${expected} }` : expected;

try {
const normalisedReceivedCSS = transform({
code: Buffer.from(recievedCSS, 'utf8'),
filename: '',
});

const normalisedExpectedCSS = transform({
code: Buffer.from(expectedCSS, 'utf8'),
filename: '',
});

if (
normalisedReceivedCSS.code.toString() !==
normalisedExpectedCSS.code.toString()
) {
throw new Error(
'Received CSS does not match expected CSS\n\n' +
normalisedReceivedCSS.code.toString(),
);
}

return {
pass: true,
message: () => '',
};
} catch (error) {
const message = (error as Warning).message;
if (!message) throw error;
return {
pass: false,
message: () => message,
};
}
},
});
Loading