From 6cd56cff6c5a81a85e6b4fa182910accabc0682e Mon Sep 17 00:00:00 2001 From: uzlopak Date: Sun, 17 Sep 2023 01:46:10 +0200 Subject: [PATCH 1/2] docs: extend documentation about suppression mechanism --- README.md | 12 ++++++ examples/example.js | 7 ++++ test/no-warnings.test.js | 80 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 examples/example.js create mode 100644 test/no-warnings.test.js diff --git a/README.md b/README.md index a8accb9..6cafc35 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,18 @@ warning.emit('FST_ERROR_CODE', 'world') // will be emitted warning.emit('FST_ERROR_CODE', 'world') // will be emitted again ``` +#### Suppressing warnings + +It is possible to suppress warnings by utilizing node's built-in warning suppression mechanism. + +Warnings can be suppressed: + +- by setting the `NODE_NO_WARNINGS` environment variable to `1` +- by passing the `--no-warnings` flag to the node process +- by setting 'no-warnings' in the `NODE_OPTIONS` environment variable + +For more information see [node's documentation](https://nodejs.org/api/cli.html). + ## License Licensed under [MIT](./LICENSE). diff --git a/examples/example.js b/examples/example.js new file mode 100644 index 0000000..d6733fd --- /dev/null +++ b/examples/example.js @@ -0,0 +1,7 @@ +'use strict' + +const warning = require('..')() + +warning.create('DeprecationWarning', 'CUSTDEP001', 'This is a deprecation warning') + +warning.emit('CUSTDEP001') diff --git a/test/no-warnings.test.js b/test/no-warnings.test.js new file mode 100644 index 0000000..4622cf0 --- /dev/null +++ b/test/no-warnings.test.js @@ -0,0 +1,80 @@ +'use strict' + +const { test } = require('tap') +const { spawnSync } = require('child_process') +const { resolve } = require('path') + +const entry = resolve(__dirname, '../examples', 'example.js') + +test('--no-warnings is set in cli', t => { + t.plan(1) + const child = spawnSync(process.execPath, [ + '--no-warnings', + entry + ]) + + const stderr = child.stderr.toString() + t.equal(stderr, '') +}) + +test('--no-warnings is not set in cli', t => { + t.plan(1) + const child = spawnSync(process.execPath, [ + entry + ]) + + const stderr = child.stderr.toString() + t.match(stderr, /\[CUSTDEP001\] DeprecationWarning: This is a deprecation warning/) +}) + +test('NODE_NO_WARNINGS is set to 1', t => { + t.plan(1) + const child = spawnSync(process.execPath, [ + entry + ], { + env: { + NODE_NO_WARNINGS: '1' + } + }) + + const stderr = child.stderr.toString() + t.equal(stderr, '') +}) + +test('NODE_NO_WARNINGS is set to 0', t => { + t.plan(1) + const child = spawnSync(process.execPath, [ + entry + ], { + env: { + NODE_NO_WARNINGS: '0' + } + }) + + const stderr = child.stderr.toString() + t.match(stderr, /\[CUSTDEP001\] DeprecationWarning: This is a deprecation warning/) +}) + +test('NODE_NO_WARNINGS is not set', t => { + t.plan(1) + const child = spawnSync(process.execPath, [ + entry + ]) + + const stderr = child.stderr.toString() + t.match(stderr, /\[CUSTDEP001\] DeprecationWarning: This is a deprecation warning/) +}) + +test('NODE_Options contains --no-warnings', t => { + t.plan(1) + const child = spawnSync(process.execPath, [ + entry + ], { + env: { + NODE_OPTIONS: '--no-warnings' + } + }) + + const stderr = child.stderr.toString() + t.equal(stderr, '') +}) From e32f8ca88edcdca6cec52ded92d3431c727d095e Mon Sep 17 00:00:00 2001 From: Uzlopak Date: Sun, 17 Sep 2023 10:41:53 +0200 Subject: [PATCH 2/2] Update README.md Co-authored-by: Frazer Smith --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6cafc35..1abf521 100644 --- a/README.md +++ b/README.md @@ -76,7 +76,7 @@ warning.emit('FST_ERROR_CODE', 'world') // will be emitted again #### Suppressing warnings -It is possible to suppress warnings by utilizing node's built-in warning suppression mechanism. +It is possible to suppress warnings by utilizing one of node's built-in warning suppression mechanisms. Warnings can be suppressed: