Skip to content

Commit

Permalink
Make transport Jest-aware (#1181)
Browse files Browse the repository at this point in the history
* Make transport Jest-aware

* Do not rely on env variables
  • Loading branch information
mcollina committed Oct 25, 2021
1 parent d7f6490 commit 28f1f2c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
12 changes: 9 additions & 3 deletions lib/transport.js
Expand Up @@ -4,14 +4,20 @@ const { createRequire } = require('module')
const getCaller = require('get-caller-file')
const { join, isAbsolute } = require('path')

let onExit

if (global.WeakRef && global.WeakMap && global.FinalizationRegistry) {
// This require MUST be top level otherwise the transport would
// not work from within Jest as it hijacks require.
onExit = require('on-exit-leak-free')
}

const ThreadStream = require('thread-stream')

function setupOnExit (stream) {
/* istanbul ignore next */
if (global.WeakRef && global.WeakMap && global.FinalizationRegistry) {
if (onExit) {
// This is leak free, it does not leave event handlers
const onExit = require('on-exit-leak-free')

onExit.register(stream, autoEnd)

stream.on('close', function () {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -23,7 +23,7 @@
"docs": "docsify serve",
"browser-test": "airtap --local 8080 test/browser*test.js",
"lint": "eslint .",
"test": "npm run lint && tap test/*test.js test/*/*test.js && npm run test-types",
"test": "npm run lint && tap test/*test.js test/*/*test.js && jest test/jest && npm run test-types",
"test-ci": "npm run lint && tap --no-check-coverage test/*test.js test/*/*test.js --coverage-report=lcovonly && npm run test-types",
"test-ci-pnpm": "pnpm run lint && tap --no-coverage --no-check-coverage test/*test.js test/*/*test.js && pnpm run test-types",
"test-ci-yarn-pnp": "yarn run lint && tap --no-check-coverage test/*test.js test/*/*test.js --coverage-report=lcovonly",
Expand Down Expand Up @@ -82,6 +82,7 @@
"fastbench": "^1.0.1",
"flush-write-stream": "^2.0.0",
"import-fresh": "^3.2.1",
"jest": "^27.3.1",
"log": "^6.0.0",
"loglevel": "^1.6.7",
"pino-pretty": "^v7.1.0",
Expand Down
10 changes: 10 additions & 0 deletions test/jest/basic.spec.js
@@ -0,0 +1,10 @@
/* global test */
const pino = require('../../pino')

test('transport should work in jest', function () {
pino({
transport: {
target: 'pino-pretty'
}
})
})

0 comments on commit 28f1f2c

Please sign in to comment.