Skip to content

Commit 05c3fe1

Browse files
author
Guillaume Labat
committed
feat(utils): add assert helper
1 parent 58684a7 commit 05c3fe1

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/core/tests/utils.test.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
parseMutationArgs,
66
matchMutation,
77
scheduleMicrotask,
8+
assert,
89
} from '../utils'
910
import { QueryClient, QueryCache, setLogger, Logger } from '../..'
1011
import { queryKey } from '../../reactjs/tests/utils'
@@ -389,4 +390,24 @@ describe('core/utils', () => {
389390
jest.useRealTimers()
390391
})
391392
})
393+
394+
describe('assert', () => {
395+
it('should assert acccording to condition', () => {
396+
const logger: Logger = {
397+
error: jest.fn(),
398+
log: jest.fn(),
399+
warn: jest.fn(),
400+
}
401+
setLogger(logger)
402+
403+
assert(true, '')
404+
expect(logger.warn).not.toHaveBeenCalled()
405+
assert(false, '')
406+
expect(logger.warn).toHaveBeenCalledTimes(1)
407+
assert(false, '')
408+
expect(logger.warn).toHaveBeenCalledTimes(2)
409+
410+
setLogger(console)
411+
})
412+
})
392413
})

src/core/utils.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { getLogger } from './logger'
12
import type { Mutation } from './mutation'
23
import type { Query } from './query'
34
import type {
@@ -74,6 +75,15 @@ export function noop(): undefined {
7475
return undefined
7576
}
7677

78+
/**
79+
* Raise a dev warning message when the condition is not fulfilled
80+
*/
81+
export function assert(condition: boolean, message: string): void {
82+
if (process.env.NODE_ENV !== 'production' && !condition) {
83+
getLogger().warn(message)
84+
}
85+
}
86+
7787
export function functionalUpdate<TInput, TOutput>(
7888
updater: Updater<TInput, TOutput>,
7989
input: TInput

0 commit comments

Comments
 (0)