Debugging jest test in VS Code shows "Error: Cannot find module "jest-environment-node" - node.js

I have troubles with debugging my jest tests. Maybe I introduce my setup first:
root-dir
test
sample.spec.ts
jest.config.js
package.json
tsconfig.json
The contents of sample.spec.ts:
describe('Sample', () => {
it.concurrent('App', async () => {
expect(true).toEqual(true); // here the breakpoint is set
});
});
So nothing fancy to be debugged here. I installed the Jest Runner extension in VS Code.
However, when I "click" the "Debug" button, right above the it, the debugger starts, but shows an exception:
Exception has occurred: Error: Cannot find module 'jest-environment-<path-to-my-root-dir>\node_modules\jest-environment-node\build\index.js/package.json' from '<path-to-my-root-dir>'e
What is the problem there? In the node_modules directory, the jest-environment-node is there. The path form the exception is not correct, hence it can't find the package.json. But this one I can't influence!
I appreciate your help!
Kind regards to all of you!

I've found the solution to this problem... which isn't a problem at all. It is also answered here: Cannot find module jest-sequencer-#jest/test-sequencer!
So... never ever tick Caught Exceptions and Uncaught Exceptions in VS Code:
Without those two ticks, jest works out of the box.

Related

vite - module is not defined error in 3rd party npm package

Here's the error I'm getting inside of my application:
Uncaught ReferenceError: module is not defined
at isHotReloading2 (isHotReloading.js:2:20)
at Form3.UNSAFE_componentWillMount (createReduxForm.js:511:16)
and here's what the error looks like in the chrome inspector:
I can't easily change the course code of redux-form (which is no longer being maintained) and neither can I remove it from my application. Is there a way to work around this error?
I've tried the following fixes in the vite.config.js file to no avail. Any ideas would be greatly appreciated. Thanks!
I ran into this same issue with Redux-form and fixed it as follows.
Create a file with the following
const isHotReloading = () => false;
export default isHotReloading;
In your vite.config file add the following to resolve.alias
{
find: './util/isHotReloading',
replacement: path.resolve(__dirname, './PATH_TO_FILE_ABOVE/reduxFormHotReload.js'),
},
This will disable the hot reload update functionality all together, so maybe you could improve the function above, but I didn't worry about it.

custom-environment-variables.js cannot be read error in react application

I have a React + ASP.NET application run from Visual Studio. For some reason I am receiving the following error:
Error: Config file /config/custom-environment-variables.js cannot be
read. Error code is: undefined. Error message is:
FileSystem.readFileSync is not a function
I have had no intentions to use custom environment variable. I have no idea why I am receiving this error. Also, I could not find much resources on the internet about this issue. I have been spending the whole day un/installing the npm and nodejs but no help.
Any suggestions?
After spending 1 day, I found the solution, which was in my case to delete this stupid line of code:
import config from 'config';
I do not understand why this suddenly started to cause problems since it was always there. I hope this helps others.
I could not understand the solution but;
I wrote my config.js. Afterward, export it, add it .gitignore as well
mycongif.js
const config = {
token: "xaxaxax"
}
export default config;
and I use it
import config from './config/myconfig'
console.log(config.token);
just by removing this line in your on of the files => 'import config from 'config';
it's just due to auto import by an editor

Console.log statements output nothing at all in Jest

console.log statements output nothing at all in Jest. This was working for me yesterday, and all of sudden, it's not working today. I have made zero changes to my config and haven't installed any updates.
I'm not using the --forceExit option. Still seeing this issue.
Jest suppresses the console log message by default. In order to show the console log message, set silent option to false at the command line
set --silent=false in the command line:
npm run test -- --silent=false
You can run both options together like this --watch --verbose false if you want to also be watching the files and see the output.
for one time runs just do --verbose false
As per comment on https://github.com/facebook/jest/issues/2441,
Try setting verbose: false (or removing it) in the jest options in package.json.
This is a pretty old question and still there's no accepted answer. However, none of the suggested solutions worked for me (settings like --silent --verbose etc.). The main problem is that Jest changes the global console object. So, the easiest solution is to not use the global console object.
Instead import dedicated log functions from the console module and work with those:
import { error } from "console";
error("This is an error");
As easy as that.
Try using console.debug() instead.
Run console.debug('Message here', yourValueHere) inside test function and it should show in the console output when running test script. You can verify if it works using Ctrl+F and find Message here in the standard output.
This does the trick of showing output in the console, while it is not an answer quite on how to use console.log I understand.
I am running #testing-library/jest-dom and jest-junit 12.0.0 as devDependencies.
jest-junit has a minimal configuration of
"jest-junit": {
"usePathForSuiteName": "true"
},
in package.json. This is mainly to configure coverage reporting.
jest is configured like this:
"jest": {
"testMatch": [
"**/__tests__/**/*.[jt]s?(x)",
"**/?(*.)+(spec|test).[jt]s?(x)",
"!**/utilities.ts",
],
Check for your command line flags in package.json to see that you don't have --silent in there.
in addition to --verbose option which can cause this as mentioned, be aware that the --watch may also cause this bug.
One of the potential reason that logging is not printing is due to console.log has been mocked. Something as below
// jest-setup.js
global.console = {
// eslint-disable-next-line no-undef
log: jest.fn(), // console.log are ignored in tests
// log: console.log,
// Keep native behaviour for other methods, use those to print out things in your own tests, not `console.log`
error: console.error,
warn: console.warn,
info: console.info,
debug: console.debug,
};
// package.json
"jest": {
"preset": "react-native",
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"json",
"node"
],
"setupFilesAfterEnv": [
"#testing-library/jest-native/extend-expect",
"<rootDir>/src/config/jest-setup.js"
],
"testMatch": [
"<rootDir>/src/**/__tests__/**/*.test.{ts,tsx}"
]
},
This is commonly used if you wish to disable console.log in jest
Also be sure that your jest config does not have silent: true. In my case, I didn't realize that someone else had added that to our config.
I don't see it in the list of config options, but the command line flag is documented here.
If using Webstorm with Jest configuration, click on the file name instead of the test name.
Having tried a few of the config options in the previous replies, using console.debug() instead of console.log() worked.
In my case, the issue was caused by [only] flag in:
it.only() or test.only('some text',()=>{})
According to the v27 docs silent is what you want here. verbose false (the default) prevents Jest from outputting the result of every test in a hierarchy while silent true (the default) will:
Prevent tests from printing messages through the console.
Use npx jest --silent false if you want to run Jest with that option from the CLI. Tested this just now with console.log and it works as expected.
Tried the advice given regarding jest config settings to no avail. Instead, in my case, the issue seemed related to not awaiting asynchronous code:
test("test", async () => {
console.log("Does output")
new Promise(resolve => {
// some expectation depending on async code
setTimeout(() => resolve(console.log("Does not output")) , 1)
})
})
Rather, awaiting the promise does output the async log:
test("test", async () => {
console.log("Does output")
await new Promise(resolve => {
// some expectation depending on async code
setTimeout(() => resolve(console.log("Does output")) , 1)
})
})
Possibly related background:
https://github.com/facebook/jest/issues/2441
Try using console.info() which is an alias for console.log(). I tried almost all the above answers but still console.log() didn't worked for me by any means. So, used console.info() which did the work.
This is what worked for me: jest --verbose true
In my case the problem was that the logs where made when the module is required, so before the start of an actual test case. Change from a top-level import to using require inside the test case fixed the problem.
In my case the problem was importing the functions from the compiled version (present in dist folder) instead of the src folder. And therefore it was using the old version. So rebuilding the project and/or importing from src fixed my issue.
On MacOS with jest version 26.6.3 I had to append --silent="false"
renaming my file to index.test.js from index.spec.js did the trick for me.

Expect assertions type error -> expect(...).toExist is not a function

I'm testing a NodeJS app. I encountered this error when I ran the tests. The test script is below:
.expect((res) => {
expect(res.headers['x-auth']).toExist();
expect(res.body._id).toExist();
expect(res.body.email).toBe(email);
})
The error showed:
TypeError: expect(...).toExist is not a function
How can I resolve this issue?
The expect assertion library has changed ownership. It was handed over to the Jest team, who in their infinite wisdom, created a new API.
You must now use toBeTruthy()instead of toExist().
You can still install expect as before, npm install expect --save-dev, which is currently at version 21.2.1. Most methods names will remain unchanged except for a few, including toExist().
If you are using Jest you can also use 'toBeDefined()'

Project: sahat/hackathon-starter: Error: ENOENT, no such file or directory

my problem is, that I can't debug (node-inspector) my project anymore, because some files for express couldn't be found. The problem comes only up, when I use the debugger.
Because of this problem, I installed the project approximately 10 times from the scratch. :(
Does anybody of you has an idea, what the reason is?
My system: Windows 7,
Project: "sahat/hackathon-starter"
Express: "version": "4.8.3",
node-inspector 0.7.4
fs.js
fs.statSync = function(path) {
nullCheck(path);
return binding.stat(pathModule._makeLong(path));
};
debugger says: "Error: ENOENT, no such file or directory"
path: "c:\bpr\node_modules\express\lib\node_modules\utils-merge
real path:
path: "c:\bpr\node_modules\express\node_modules\utils-merge
I would think the required path must be wrong, but can't believe it.
Express:index.js
module.exports = require('./lib/express');
Thanks for your help!
Here you'll find the answer.
https://github.com/node-inspector/node-inspector/issues/412
Turn the icon "pause on all exceptions" on the buttom of your console off.

Resources