Skip to content

Commit

Permalink
Improve the error message for dynamic css functions when returning in…
Browse files Browse the repository at this point in the history
…valid values (#230)
  • Loading branch information
jantimon authored Dec 6, 2024
1 parent 4068803 commit 0b95fc1
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/blue-donuts-behave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"next-yak": patch
---

Improve the error message for dynamic css functions when returning invalid values
38 changes: 38 additions & 0 deletions packages/next-yak/runtime/__tests__/styled.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -321,3 +321,41 @@ it("should not remove theme if theme is passed to wrapped element", () => {
</div>
`);
});

describe("dev mode - error tests", () => {
const originalNodeEnv = process.env.NODE_ENV;
const originalError = console.error;

beforeEach(() => {
// errors are only thrown in development mode
process.env.NODE_ENV = "development";
// prevent console.error from printing the error a second time
console.error = () => {};
});

afterEach(() => {
process.env.NODE_ENV = originalNodeEnv;
console.error = originalError;
});

it("should show the function body in error message when dynamic css function returns invalid value", () => {
const Component = styled.div("cssClass", {
style: {
"--bar": ({ $groupColor }) => $groupColor,
},
});

let error = null;
try {
render(<Component />);
} catch (e) {
error = e;
}
expect(error).toMatchInlineSnapshot(`
[Error: Dynamic CSS functions must return a string or number but returned undefined
Dynamic CSS function: ({ $groupColor }) => $groupColor
]
`);
});
});
2 changes: 1 addition & 1 deletion packages/next-yak/runtime/cssLiteral.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ const recursivePropExecution = (
throw new Error(
`Dynamic CSS functions must return a string or number but returned ${JSON.stringify(
result,
)}`,
)}\n\nDynamic CSS function: ${fn.toString()}\n`,
);
}
}
Expand Down

0 comments on commit 0b95fc1

Please sign in to comment.