No console.log to STDOUT when running "npm test" (jest) - javascript

From what I know console.log() should work without problem printing to the STDOUT of my console, when running a script.
But in my case I have NPM configured to run Jest when issuing npm test from the shell, and any console.log() inside the test files doesn't print anything on the screen.
I tried also to use process.stdout.write() but still I get no custom output when running npm test.
How am I supposed to debug stuff in my test scripts?
I can't figure out if it is a problem from Node, from NPM, or from Jest.
There's a Jest issue that looks similar to mine but still I cannot solve and get to output a simple string; while there rest of Jest output is echoed as usual.
Anybody experienced a similar problem?
EDIT 1:
I tried running npm test -- --runInBand but the log doesn't appear.
Note
trying to run the command repeatedly like
console.log('foo...');console.log('bar..');console.log('baz..');
I can sometimes see the log, but this is then overwritten/hidden by the rest of the following Jest output.

From the linked issue since the problem is going on since years...
There is a pull request in the alpha release that is going to fix this problem. Looks like the last refining is being worked in the past few hours (see Github issue link).
However I found a solution that works for my simplest case:
just run enabling verbose mode and the log will magically appear!
npm test -- --verbose=true
This will not fix in every case; maybe async/multi-thread/etc will still have problems (please report your experiences, so I can update the answer); fo r example adding a process.exit(1) after the log will hide it again.
Trying to press ctrl + c before the process.exit() runs (with the right timing...) will show that the log is actually there and being overridden.
I will update the answer with news, but probably this will help others in starting with Node/NPM/Jest setups!
Jest's GitHub issue mention some useful details/suggestions:
https://github.com/facebook/jest/issues/2441#issuecomment-713433790
https://github.com/facebook/jest/issues/2441#issuecomment-724131782
https://github.com/facebook/jest/issues/2441#issuecomment-586359238
https://github.com/facebook/jest/issues/2441#issuecomment-611122871
https://github.com/facebook/jest/issues/2441#issuecomment-623856169
aaaand.... https://github.com/facebook/jest/issues/2441#issuecomment-649391333
Jest replaces the global console module; this apparently is the source of the issue. And also the fact that Jest will set it to true automatically if you're running a single test. upside_down_face

This isn't clean, but I've had luck with it, hopefully it'll help you until a fix is published: log some extra newlines after the log that's getting overwritten:
console.log('I want to be seen!');
console.log('\n\n\n\n');

I had this issue and it turns out that in my case, the problem was calling process.exit(1) immediately after writing to the console. When you do this during Jest tests, the output seems to be lost.
The (not-entirely-satisfying) workaround that I settled on is:
console.warn('stuff I want to write to the console');
if (process.env.NODE_ENV === 'test') {
setTimeout(() => process.exit(1), 1000);
} else {
process.exit(1);
}
For this, the NODE_ENV environment variable would need to be set to test in your Jest setup script or elsewhere.

Related

Cypress - Timed out retrying after 4000ms: Expected to find element - only in Jenkins

I have a grouping of Cypress tests that are failing, only in my Jenkins environment. An entire describe block is failing to find an element. Every test in the block starts with the same commands:
describe("This section of tests", () => {
it("Test for something in this section", () => {
cy.login(); // custom login command, works in all other test blocks
setupPage(); // page setup function, no problems in all other test blocks
cy.wait(10000); // desperation wait
cy.get("#toggle-detail-pane-button").click(); // issue here!
// all tests in block run more assertions and commands after this
});
// more similar tests in block
});
// more describe blocks that also use cy.login and setupPage, with no issue
When I run these tests in the cypress test UI, they all pass. When I run them in the terminal on my machine with npx cypress run, they pass. When I ssh into a remote OpenStack terminal which is set up to run my project and run cypress, and run npx cypress run, they pass.
When I run the tests in Jenkins, which uses a handful of identical OpenStack instances to run cypress in parallel, the tests fail, consistently, with the following message:
AssertionError: Timed out retrying after 4000ms: Expected to find element: `#toggle-detail-pane-button`, but never found it.
at Context.eval (http://localhost:3000/__cypress/tests?p=cypress/integration/stuff/mytests.spec.js:519:8)
I have tried reorganizing my tests, rewriting them, etc, but no luck. I cannot figure out what is going wrong here. My Jenkins environment uses a custom sorry-cypress setup, and for some reason, the dashboard is not registering the tests, so I can't go and view any screenshots or videos (that is a whole separate can-of-worms problem).
Why would only these tests fail only in my CI/CD env? How can I even begin to debug this?
Edit: screenshots
I was able to rerun the CI/CD with screenshots and videos, and then I was able to scp the files off of the instance with the failing tests. When running on my machine, cypress has no problem finding the element:
When using the cypress selector playground on my machine, it finds it as well:
But in the screenshot I pulled off of the openstack instance running exactly the same test, it can't find it:
(Sorry for all the green, this is a proprietary app)
What gives?
Please update the default command timeout in cypress.config.js file using below line:
defaultCommandTimeout: 10000
Instead of 10000 you can use any value of time in miliseconds. Using this your, timeout issue will be resolved.
That means there's an issue with your cy.visit() command. The page is not being loaded before the test that's why
I had a similar problem. I solved it by getting the element by a selector or class instead of its id.
I went from:
cy.get('#mat-options-text').click()
to
cy.get('mat-options').click()

Opensea JS SDK createSellOrder method doesn't seem to work or silent failure

I am trying to create multiple sell orders for my NFTs on the rinkeby testnet. I am using the script provided by OpenSea from this repo called sell.js
The issue is after I run the script, the method createSellOrder seems to run because I get the following output on my terminal but nothing else appears or happens.
The first two lines are my own logging and the third line is what I guess is from createSellOrder.
My question is, what is the expected outcome/output of createSellOrder and how can I know it succeeded? For me it just prints the last line: creating proxy for acc... and stops.
When I check opensea, it doesn't have my nfts listed for sale so it obviously didn't work...
This type of issue tends to be fixed by removing all your node_modules and reinstalling using yarn instead of npm

Error importing dependency in Cypress Cucumber

I have a problem when importing some dependencies in Cucumber. The error is the following:
Running: features\my_feature.feature... (1 of 1)
Browserslist: caniuse-lite is outdated. Please run next command `npm update`
Oops...we found an error preparing this test file:
cypress\integration\features\my_feature.feature
The error was:
Error: Cannot find module 'C:gitdevelopPROJECT
ode_modulescypress-cucumber-preprocessorlib/resolveStepDefinition' from 'C:\git\develop\PROJECT\cypress\integration\features'
This occurred while Cypress was compiling and bundling your test code. This is usually caused by:
- A missing file or dependency
- A syntax error in the file or one of its dependencies
Fix the error in your code and re-run your tests.
I'm not able to run the features I worked on because of this. I suppose the key for the error is in line 3. I'll explain this, the path of the file is the following: C:\git\develop\PROJECT\cypress\node_modules\cypress-cucumber-preprocessor\lib\resolveStepDefinition, but the library is recognizing the \n substring from \node_modules as a break line and is not recognizing the \ in the path. I don't know if that makes any sense.
I don't know what to publish here, because I don't think it's related to a configuration or anything like that. If you have any idea or need anything else, please, tell me. I'll publish all that could be required.
Make sure your file structure is like:
cypress/integration/myfeatures/feature1.feature
cypress/integration/myfeatures/feature1/feature1.js
This structure is required by cypress-cucumber-preprocessor and might be why you get the resolveStepDefinition error message. See the github docs
I believe the error message you get is misleading you in thinking the '\n' is an issue in the code, but in reality, it's the console that can't properly handle the '\n'.

Update Jest Snapshot Tests within IntelliJ IDEA

Recent versions of IntelliJ IDEA support the execution of Jest tests.
I couldn't find an option (or even better a shortcut) to update snapshot tests within IntelliJ IDEA.
Is there an option/shortcut to update snapshots within IntelliJ IDEA?
What I have been doing is to right click on the failing Jest test and select the Create option in the pop-up menu to create a new run configuration for just that failing test.
I then add -u to the Jest options and run that specific test (once) to update the snapshot.
It is far from ideal, but you can keep them around for later, if you like, to re-run them with the -u option when needed.
I was wondering the same thing and I asked Jetbrains. They said this feature was requested and you can track the status of it here: https://youtrack.jetbrains.com/issue/WEB-26008
I'm not sure when it will be complete but looks like it is on their radar.
The -u option can be applied as a Jest run config default if you want it to be active for all Jest tests.

How to run callback with jest --watch

I started to learn ReactJS yesterday (to be used in my next product), I am willing to set up my dev environment but now I'm stuck with Jest...
Having a bluetooth lightbulb on my desk (already op with scripts etc..), I want to get a red light when my tests launched with jest --watch fail (see create-react-app from FB devs here)
The problem is, I don't know how to run a callback after the tests, it seems like no one ran into this issue on the interwebz, no solution found yet for me.
Update:
I am currently using a log file to grep:
lamp.rb
def ci
if File.readlines("path/jest.log").grep(/failed/).any?
File.truncate('path/jest.log', 0)
fail_jest # This method updates my lightbulb :) (red blink)
end
rescue
puts 'No jest log found :('
end
Launching my jest tests like this: unbuffer npm run test |& tee tmp/jest.log
I am still looking for a better solution !
Thanks for your help
Your problem is not specific to React or Jest. When your run jest tests, you are basically running a Node/npm command and when the tests fail the process exists with an unsuccessful exit code. This is the same mechanism used to make automated CI builds fail when tests don't pass. So I'd suggest you start your research from there and depending on your lightbulb's API, it should be straight forward to make it fire events whenever the process fails regardless of the reason.

Categories

Resources