From 7fb5131f8905b0aa0acff898e3b00865e868755b Mon Sep 17 00:00:00 2001 From: Chandra Date: Mon, 30 Nov 2026 22:57:48 +0530 Subject: [PATCH] fix(jest): accept debug: "verbose" in registerCSS and keep a given logger --- src/__tests__/native/jest-helpers.test.ts | 40 +++++++++++++++++++++++ src/jest/index.ts | 18 +++++----- 2 files changed, 50 insertions(+), 8 deletions(-) create mode 100644 src/__tests__/native/jest-helpers.test.ts diff --git a/src/__tests__/native/jest-helpers.test.ts b/src/__tests__/native/jest-helpers.test.ts new file mode 100644 index 00000000..9aae038a --- /dev/null +++ b/src/__tests__/native/jest-helpers.test.ts @@ -0,0 +1,40 @@ +import { compile } from "react-native-css/compiler"; +import { compileWithAutoDebug, registerCSS } from "react-native-css/jest"; + +const css = ".a { color: red; }"; + +function compileMessages() { + const messages: string[] = []; + compile(css, { logger: (message: string) => messages.push(message) }); + return messages; +} + +test("registerCSS calls the logger it is given", () => { + const messages: string[] = []; + registerCSS(css, { logger: (message: string) => messages.push(message) }); + + expect(messages).toStrictEqual(compileMessages()); +}); + +test("compileWithAutoDebug calls the logger it is given", () => { + const messages: string[] = []; + compileWithAutoDebug(css, { + debug: true, + logger: (message: string) => messages.push(message), + }); + + expect(messages).toStrictEqual(compileMessages()); +}); + +test('registerCSS passes debug: "verbose" on to compileWithAutoDebug', () => { + const log = jest.spyOn(console, "log").mockImplementation(() => undefined); + + try { + registerCSS(css, { debug: "verbose" }); + + expect(log).toHaveBeenCalledWith(expect.stringMatching(/^Rules:\n/)); + expect(log).toHaveBeenCalledWith(expect.stringMatching(/^Compiled:\n/)); + } finally { + log.mockRestore(); + } +}); diff --git a/src/jest/index.ts b/src/jest/index.ts index d16a492a..eb4e78d0 100644 --- a/src/jest/index.ts +++ b/src/jest/index.ts @@ -37,7 +37,7 @@ const debugDefault = Boolean( export function registerCSS( css: string, - options: CompilerOptions & { debug?: boolean | undefined } = {}, + options: CompilerOptions & { debug?: boolean | "verbose" | undefined } = {}, ) { const { debug = debugDefault } = options; const compiled = compileWithAutoDebug(css, options); @@ -66,14 +66,16 @@ export function compileWithAutoDebug( ...options }: CompilerOptions & { debug?: boolean | "verbose" | undefined } = {}, ) { - const logger = debug - ? (text: string) => { - // Just log the rules - if (text.startsWith("[") && debug === "verbose") { - console.log(`Rules:\n---\n${text}`); + const logger = + options.logger ?? + (debug + ? (text: string) => { + // Just log the rules + if (text.startsWith("[") && debug === "verbose") { + console.log(`Rules:\n---\n${text}`); + } } - } - : undefined; + : undefined); return compile(css, { ...options, logger }); }