Skip to content

Commit

Permalink
createDestination: Use Record type for stdoutMock.calls and `stdo…
Browse files Browse the repository at this point in the history
…utMock.onlyCall()` (#137)
  • Loading branch information
72636c authored Jul 3, 2024
1 parent 7833986 commit 35cd486
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
11 changes: 11 additions & 0 deletions .changeset/neat-radios-ring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
'@seek/logger': patch
---

createDestination: Use `Record` type for `stdoutMock.calls` and `stdoutMock.onlyCall()`

This allows you to destructure a call in your test code without the TypeScript compiler complaining:

```typescript
const { level, ...rest } = stdoutMock.onlyCall();
```
11 changes: 8 additions & 3 deletions src/destination/mock.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import fastRedact from 'fast-redact';

type Call = Readonly<Record<PropertyKey, unknown>>;

const isCall = (value: unknown): value is Call =>
value !== null && typeof value === 'object' && !Array.isArray(value);

export type MockOptions = {
/**
* Properties to replace with a static `-` before recording the logging call.
Expand Down Expand Up @@ -50,7 +55,7 @@ export const createStdoutMock = (opts: MockOptions) => {
strict: true,
});

const calls: object[] = [];
const calls: Call[] = [];

return {
/**
Expand Down Expand Up @@ -88,7 +93,7 @@ export const createStdoutMock = (opts: MockOptions) => {
* expect(stdoutMock.onlyCall()).toMatchSnapshot();
* ```
*/
onlyCall: (): object => {
onlyCall: (): Call => {
const { 0: first, length } = calls;

if (!first || length > 1) {
Expand All @@ -110,7 +115,7 @@ export const createStdoutMock = (opts: MockOptions) => {

call = JSON.parse(String(result));

if (call === null || typeof call !== 'object' || Array.isArray(call)) {
if (!isCall(call)) {
throw new Error(
`@seek/logger mocking failed to process a log message: ${msg}`,
);
Expand Down

0 comments on commit 35cd486

Please sign in to comment.