Skip to content

Commit

Permalink
chore: add test for uncovered usecases
Browse files Browse the repository at this point in the history
  • Loading branch information
DavideArena committed Oct 20, 2023
1 parent 14afe9a commit 52e37e1
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions test/logMessage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,88 @@ test('should log without msg using a logMessage function returning an undefined
})
})

test('should log without msg using a logMessage function returning an array containing non string value', async (t) => {
t.plan(5)

const customLogMessage = (context) => ['foobar', 3]

const stream = jsonLogger(
line => {
t.is(line.req, undefined)
t.is(line.reqId, 'req-1')
t.deepEqual(line.msg, undefined)
t.deepEqual(line.graphql, {
operationName: 'logMe',
queries: ['add', 'add', 'echo', 'counter']
})
})

const app = buildApp(t, { stream }, { logMessage: customLogMessage })

const query = `query logMe{
four: add(x: 2, y: 2)
six: add(x: 3, y: 3)
echo(msg: "hello")
counter
}`

const response = await app.inject({
method: 'POST',
headers: { 'content-type': 'application/json' },
url: '/graphql',
body: JSON.stringify({ query })
})
t.deepEqual(response.json(), {
data: {
four: 4,
six: 6,
echo: 'hellohello',
counter: 0
}
})
})

test('should log without msg using a logMessage function throwing an error', async (t) => {
t.plan(5)

const customLogMessage = (context) => { throw new Error() }

const stream = jsonLogger(
line => {
t.is(line.req, undefined)
t.is(line.reqId, 'req-1')
t.deepEqual(line.msg, undefined)
t.deepEqual(line.graphql, {
operationName: 'logMe',
queries: ['add', 'add', 'echo', 'counter']
})
})

const app = buildApp(t, { stream }, { logMessage: customLogMessage })

const query = `query logMe{
four: add(x: 2, y: 2)
six: add(x: 3, y: 3)
echo(msg: "hello")
counter
}`

const response = await app.inject({
method: 'POST',
headers: { 'content-type': 'application/json' },
url: '/graphql',
body: JSON.stringify({ query })
})
t.deepEqual(response.json(), {
data: {
four: 4,
six: 6,
echo: 'hellohello',
counter: 0
}
})
})

test('should log with msg using a logMessage function returning a string', async (t) => {
t.plan(5)

Expand Down

0 comments on commit 52e37e1

Please sign in to comment.