Comments (11)
Hard to say I'm afraid! What happens if you put a console.log(...)
and logger.log(...)
calls outside the test, e.g. just after the enableAll()
call?
I suspect what's happening is that something else in your test stack is patching the console methods, but loglevel is capturing them early on (when setLevel
or enableAll
is called) and that's causing issues. We've seen similar issues from React itself in the past (#171).
What happens if you put logger.enableAll()
inside the test, just before you call logger.debug
? If that fix this then this is definitely caused by something patching console.log
itself during the tests, and not expecting anybody to still have a reference to the previous value.
from loglevel.
from loglevel.
Thanks @pimterry. Tried what you suggested:
(1) adding console/logger calls after enableAll() outside the test - no luck (e.g., console.log => display; logger.log => no display.
(2) moving enableAll() and the console/logger calls inside the test - no luck
- I went through the setupTests.js file (testingLibrary) and tried some Jest config options (e.g.,
--silent=false
) - no luck - I also modified setupTests.js to see if I could trap any of the "CDE" messages that I was outputting from logLevel with the following:
const originalConsoleLog = console.log;
...
global.console.log = (...args) => {
if (args[0].match(/CDE/)) {
alert('console.log: ' + args[0] + 'FIXUP');
} else {
originalConsoleLog(args);
}
And did this for all the console options (log, warn, error, info, debug, trace)
Seems as if by the time it got to the test code, the logLevel stuff was all MIA.
So... unless you have any more ideas, I guess I'll just have to add console.logs as needed during tests rather than reusing the existing log calls. <sigh>
from loglevel.
Hard to say! I would suggest debugging inside loglevel's code in your environment (it's very very short, this is a tiny library) and trying to work out why console.log
in your code isn't the same value as console[methodName]
when methodName is 'log' here.
Effectively, all loglevel does is run:
logger[methodName] = console[methodName].bind(console);
for each log level that's enabled. It's nothing especially complicated, and it should work in any standard JS environment anywhere.
My best guess is that Jest is doing something very weird to globals, because it does all sorts of non-standard things like that which break libraries like this in different ways all the time (this is a widespread problem: https://twitter.com/matteocollina/status/1453029660925861901). If you can trace it down though, you might be able to work around it, or at least produce a clear bug the Jest team can fix.
from loglevel.
Thanks, Tim... I'll work on that.
from loglevel.
Well... that's interesting, and looks to be a work-around.
I tried all the console.xxx options and found that Jest does display console.warn
and console.error
messages and doesn't fail the test in either case. I get a lot of unneeded stack trace info, but at least I get my console display from logger.warn.
console.log
DefaultLayoutNoPeriods.test.js/enableAll/console
at src/layouts/DefaultLayoutNoPeriods.test.js:21:11
console.warn
DefaultLayoutNoPeriods/warn [
{
entity: 'XXX-entity',
displayName: 'XXX-Display',
ribbonName: 'XXX-Ribbon'
}
]
20 | logger.debug('DefaultLayoutNoPeriods/debug', state.entitiesList);
21 | logger.info('DefaultLayoutNoPeriods/info', state.entitiesList);
> 22 | logger.warn('DefaultLayoutNoPeriods/warn', state.entitiesList);
| ^
23 | logger.error('DefaultLayoutNoPeriods/error', state.entitiesList);
24 | return (
25 | <>
at DefaultLayoutNoPeriods (src/layouts/DefaultLayoutNoPeriods.js:22:10)
at renderWithHooks (node_modules/react-dom/cjs/react-dom.development.js:14985:18)
at mountIndeterminateComponent (node_modules/react-dom/cjs/react-dom.development.js:17811:13)
at beginWork (node_modules/react-dom/cjs/react-dom.development.js:19049:16)
at beginWork$1 (node_modules/react-dom/cjs/react-dom.development.js:23940:14)
at performUnitOfWork (node_modules/react-dom/cjs/react-dom.development.js:22779:12)
at workLoopSync (node_modules/react-dom/cjs/react-dom.development.js:22707:5)
console.error
DefaultLayoutNoPeriods/error [
{
entity: 'XXX-entity',
displayName: 'XXX-Display',
ribbonName: 'XXX-Ribbon'
}
]
21 | logger.info('DefaultLayoutNoPeriods/info', state.entitiesList);
22 | logger.warn('DefaultLayoutNoPeriods/warn', state.entitiesList);
> 23 | logger.error('DefaultLayoutNoPeriods/error', state.entitiesList);
| ^
24 | return (
25 | <>
26 | <StyledHeader>
at DefaultLayoutNoPeriods (src/layouts/DefaultLayoutNoPeriods.js:23:10)
at renderWithHooks (node_modules/react-dom/cjs/react-dom.development.js:14985:18)
at mountIndeterminateComponent (node_modules/react-dom/cjs/react-dom.development.js:17811:13)
at beginWork (node_modules/react-dom/cjs/react-dom.development.js:19049:16)
at beginWork$1 (node_modules/react-dom/cjs/react-dom.development.js:23940:14)
at performUnitOfWork (node_modules/react-dom/cjs/react-dom.development.js:22779:12)
at workLoopSync (node_modules/react-dom/cjs/react-dom.development.js:22707:5)
console.log
<body>
<div>
<div>
Hello World
</div>
</div>
</body>
at debug (node_modules/@testing-library/react/dist/pure.js:107:13)
from loglevel.
And the unexpected nicety is that these show up in-line with other output from the test, as opposed to being separated out at the very start of the test.
from loglevel.
Have you reported this to the Jest team? It seems like Jest is clearly doing its own custom handling of log output, and so I feel like they'll be more help here.
from loglevel.
Good idea; thanks for all your help on this!
from loglevel.
Any progress on this? I'm experiencing this with Jest v29.7.0
and LogLevel v1.8.1
.
from loglevel.
I looked in Jest's issue list and found one solution that seems to work in jestjs/jest#687
It involves mapping the console.xxx
methods to the LogLevel counterparts. This works fine for my team's use case.
// Jest setupTests.js
import log from 'loglevel';
log.info = console.info;
log.debug = console.debug;
log.trace = console.trace;
log.error = console.error;
log.warn = console.warn;
Also, I tried invoking log.noConflict()
in the setupTest.js
and my logger module, but that didn't seem to have any effect. The solution above is probably the way to go.
from loglevel.
Related Issues (20)
- Programmatic log levels HOT 1
- Configurable persistence HOT 4
- methodFactory logLevel parametr has always same value HOT 2
- Show filename and line of logging code HOT 2
- Would you consider deprecating in favor of Roarr? HOT 2
- loglevel print duplicate log in React component HOT 5
- Is it possible to import Loglevel as an ES module in client-side javascript? HOT 5
- Possibility to save log output to file HOT 4
- current npm web page for loglevel shows no documentation at all HOT 3
- LogLevel `trace`, `debug` and `info` not shown anymore on Google Chrome HOT 2
- Log to a file HOT 1
- Feature/Enhancement: add a level between 'debug' and 'trace' HOT 1
- Is it possible to enable multiple plugins? HOT 2
- how to send to logs server? HOT 1
- Couldnt locate documention or plugins to format logged payload as JSON (compatible with Datadog etc) HOT 2
- Why do I have to configure the log in every typescript file? HOT 12
- The name of the file the log has been generated and the line number is broken when using a plugin HOT 2
- Cleanup/modernization of developer test/build tooling HOT 8
- Cannot set property trace of #<Object> which has only a getter HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from loglevel.