From 8e647f10aee097258b59fe66eb0a6af5b07c0161 Mon Sep 17 00:00:00 2001 From: Ryan Ling Date: Mon, 9 Oct 2023 14:24:56 +1100 Subject: [PATCH] Use `toStrictEqual` in root tests (#93) `toMatchObject` accepts an object subset, which makes it hard to reliably test property removal. For example, #92 adds a happy-path test case "should omit defaultOmitHeaderNames by default" which unexpectedly passes even if the omission logic is removed. These tests aren't great overall and I'd prefer them to use a more typical Jest structure so we can use `.only`s and the like, but this should be enough to get #92 through with confidence around regression testing. --------- Co-authored-by: Conrad Lang --- .changeset/moody-bobcats-travel.md | 2 +- src/index.test.ts | 38 ++++++++++++++++++++++++++---- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/.changeset/moody-bobcats-travel.md b/.changeset/moody-bobcats-travel.md index fc8b137..91a6a16 100644 --- a/.changeset/moody-bobcats-travel.md +++ b/.changeset/moody-bobcats-travel.md @@ -15,7 +15,7 @@ Omit request headers - `x-envoy-peer-metadata-id` - `x-envoy-upstream-service-time` -If you would like to opt out of this behaviours, you can provide an empty list or your own list of request headers to `omitHeaderNames`: +To opt out of this behaviour, provide an empty list or your own list of omissible request headers to `omitHeaderNames`: ```diff const logger = createLogger({ diff --git a/src/index.test.ts b/src/index.test.ts index 6350ee5..521bbbf 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -49,9 +49,13 @@ function testLog( logger[method ?? 'info'](input); const log: any = await once(stream, 'data'); - expect(log).toMatchObject(output); + expect(log).toStrictEqual({ + level: 30, + name: 'my-app', + timestamp: expect.any(String), + ...output, + }); expect(inputString).toEqual(JSON.stringify(input)); - expect(log).toHaveProperty('timestamp'); }); } @@ -194,6 +198,7 @@ testLog( err: { message: 'a'.repeat(64).split(''), }, + msg: new Array(64).fill('a'), }, ); @@ -228,9 +233,22 @@ testLog( foo: new Error('Oh my, this is an error too!'), }, { - error: { message: 'Ooh oh! Something went wrong' }, - err: { message: 'Woot! Another one!' }, - foo: { message: 'Oh my, this is an error too!' }, + error: { + message: 'Ooh oh! Something went wrong', + name: 'Error', + stack: expect.stringMatching(/^Error: Ooh oh! Something went wrong\n/), + }, + err: { + message: 'Woot! Another one!', + stack: expect.stringMatching(/^Error: Woot! Another one!\n/), + type: 'Error', + }, + foo: { + message: 'Oh my, this is an error too!', + name: 'Error', + stack: expect.stringMatching(/^Error: Oh my, this is an error too!\n/), + }, + msg: 'Woot! Another one!', }, ); @@ -315,12 +333,15 @@ testLog( }, }, err: { + code: 'ECONNABORTED', config: { headers: { Accept: 'application/json, text/plain, */*', authorization: '[Redacted]', }, + timeout: 400, }, + message: 'timeout of 400ms exceeded', request: { _options: { method: 'get', @@ -329,8 +350,15 @@ testLog( authorization: '[Redacted]', }, }, + domain: null, }, + stack: expect.stringMatching(/^Error: timeout of 400ms exceeded\n/), + type: 'Object', }, + level: 40, + meta: { message: 'timeout of 400ms exceeded', attempt: 0 }, + msg: 'Retrying Request', + v: 1, }, );