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

docs: extend documentation about suppression mechanism #83

Merged
merged 2 commits into from
Sep 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Uzlopak marked this conversation as resolved.
Show resolved Hide resolved

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).
7 changes: 7 additions & 0 deletions examples/example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict'

const warning = require('..')()

warning.create('DeprecationWarning', 'CUSTDEP001', 'This is a deprecation warning')

warning.emit('CUSTDEP001')
80 changes: 80 additions & 0 deletions test/no-warnings.test.js
Original file line number Diff line number Diff line change
@@ -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, '')
})