Skip to content

Commit

Permalink
Fix syntax error in test command
Browse files Browse the repository at this point in the history
  • Loading branch information
atkinchris committed Oct 25, 2021
1 parent 0492f13 commit 0a7d5a1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
8 changes: 5 additions & 3 deletions lib/logger.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const { cosmiconfigSync } = require('cosmiconfig')
const pino = require('pino')

const defaultPinoConfig = require('./defaultPinoConfig')

Expand All @@ -12,12 +11,15 @@ if (results && results.config) {
config = results.config
}

// Set the default logger constructor to Pino.
let logger = pino
let logger

// If logger exists in the config file, and it's a function, use it as the logger constructor.
if ('logger' in config && typeof config.logger === 'function') {
logger = config.logger
} else {
// Otherwise, set the default logger constructor to Pino.
// eslint-disable-next-line global-require
logger = require('pino')
}

// Call the logger constructor with the library's default Pino configuration.
Expand Down
34 changes: 21 additions & 13 deletions tests/config.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,42 @@ const path = require('path')
const { buildNextJsScript } = require('./scenarios')

describe('config', () => {
let builder
let container

const runScriptInDockerWithConfig = async (script, preset, configFilePath) => {
const runScriptInDockerWithConfig = async (script, preset) => {
const entrypoint = preset ? `next-logger/presets/${preset}` : 'next-logger'

const container = await builder
.withBindMount(configFilePath, '/app/next-logger.config.js', 'ro')
.withCmd(['top'])
.start()
const { output, exitCode } = await container.exec(['node', '-r', entrypoint, '-e', script])

await container.stop()

return { stdout: output, exitCode }
}

beforeAll(async () => {
builder = await GenericContainer.fromDockerfile(process.cwd(), 'tests/docker/Dockerfile').build()
const builder = await GenericContainer.fromDockerfile(process.cwd(), 'tests/docker/Dockerfile').build()
const configFilePath = path.resolve(__dirname, 'config/next-logger.config.js')
container = await builder.withBindMount(configFilePath, '/app/next-logger.config.js', 'ro').withCmd(['top']).start()
}, 60000)

afterAll(async () => {
await container.stop()
})

it('loads a config from the working directory and uses that Pino instance', async () => {
const script = buildNextJsScript('info', "'Message for info'")
const configFilePath = path.resolve(__dirname, 'config/next-logger.config.js')
const { stdout, exitCode } = await runScriptInDockerWithConfig(script, undefined, configFilePath)
const { stdout, exitCode } = await runScriptInDockerWithConfig(script, undefined)

expect(exitCode).toBe(0)
expect(JSON.parse(stdout)).toMatchObject({
message: 'Message for info',
})
})

it("includes the default behaviour from the library's Pino config", async () => {
const script = buildNextJsScript('info', "'Hello World', new Error('Boom')")
const { stdout, exitCode } = await runScriptInDockerWithConfig(script, undefined)

expect(exitCode).toBe(0)
expect(JSON.parse(stdout)).toMatchObject({
err: { message: 'Boom', type: 'Error', stack: expect.stringMatching(/Error: Boom\n *?at \[eval\]/) },
message: 'Hello World',
})
})
})

0 comments on commit 0a7d5a1

Please sign in to comment.