Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make transport Jest-aware #1181

Merged
merged 2 commits into from
Oct 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 9 additions & 3 deletions lib/transport.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* global test */
const pino = require('../../pino')

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