Skip to content

Commit bb51212

Browse files
committed
tweaks
1 parent feeae76 commit bb51212

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

src/helpers/__tests__/validate-options.test.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ test('does not warn when rest object is empty', () => {
1616
});
1717

1818
test('warns when unknown option is passed', () => {
19-
validateOptions('testFunction', { unknownOption: 'value' }, testFunction);
19+
function testFunctionWithCall() {
20+
validateOptions('testFunction', { unknownOption: 'value' }, testFunctionWithCall);
21+
}
22+
testFunctionWithCall();
2023

2124
expect(_console.warn).toHaveBeenCalledTimes(1);
2225
const warningMessage = jest.mocked(_console.warn).mock.calls[0][0];
@@ -25,11 +28,14 @@ test('warns when unknown option is passed', () => {
2528
});
2629

2730
test('warns when multiple unknown options are passed', () => {
28-
validateOptions(
29-
'testFunction',
30-
{ option1: 'value1', option2: 'value2', option3: 'value3' },
31-
testFunction,
32-
);
31+
function testFunctionWithCall() {
32+
validateOptions(
33+
'testFunction',
34+
{ option1: 'value1', option2: 'value2', option3: 'value3' },
35+
testFunctionWithCall,
36+
);
37+
}
38+
testFunctionWithCall();
3339

3440
expect(_console.warn).toHaveBeenCalledTimes(1);
3541
const warningMessage = jest.mocked(_console.warn).mock.calls[0][0];
@@ -41,9 +47,9 @@ test('warns when multiple unknown options are passed', () => {
4147

4248
test('warns with correct function name and includes stack trace', () => {
4349
function render() {
44-
// Test function
50+
validateOptions('render', { invalid: true }, render);
4551
}
46-
validateOptions('render', { invalid: true }, render);
52+
render();
4753

4854
expect(_console.warn).toHaveBeenCalledTimes(1);
4955
const warningMessage = jest.mocked(_console.warn).mock.calls[0][0];

src/helpers/validate-options.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,23 @@ import { logger } from './logger';
77
*
88
* @param functionName - The name of the function being called (for error messages)
99
* @param restOptions - The rest object from destructuring that contains unknown options
10-
* @param _callsite - The function where the validation is called from (unused, kept for API compatibility)
10+
* @param callsite - The function where the validation is called from (e.g., render, renderHook)
1111
*/
1212
export function validateOptions(
1313
functionName: string,
1414
restOptions: Record<string, unknown>,
1515
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
16-
_callsite: Function,
16+
callsite: Function,
1717
): void {
1818
const unknownKeys = Object.keys(restOptions);
1919
if (unknownKeys.length > 0) {
20-
// Pass validateOptions as callsite so the stack trace shows where the function (e.g., render) was called from
21-
const stackTraceError = new ErrorWithStack('STACK_TRACE_ERROR', validateOptions);
22-
const stackTrace = stackTraceError.stack
23-
? `\n\n${stackTraceError.stack.split('\n').slice(1).join('\n')}`
24-
: '';
20+
// Pass the callsite function (e.g., render) to remove it from stack
21+
// This leaves only where the user called the function from (e.g., test file)
22+
const stackTraceError = new ErrorWithStack('STACK_TRACE_ERROR', callsite);
23+
const stackLines = stackTraceError.stack ? stackTraceError.stack.split('\n') : [];
24+
// Skip the first line (Error: STACK_TRACE_ERROR) to show the actual call sites
25+
// The remaining lines show where the user called the function from
26+
const stackTrace = stackLines.length > 1 ? `\n\n${stackLines.slice(1).join('\n')}` : '';
2527
logger.warn(
2628
`Unknown option(s) passed to ${functionName}: ${unknownKeys.join(', ')}${stackTrace}`,
2729
);

0 commit comments

Comments
 (0)