Jest Runs Files in Parent Directories? - jestjs

I have a project folder in a projects folder, and I just added Jest to that project. Now when I run npx jest, it runs a ton of tests that it finds in the parent projects folder. It does this whether or not I specify:
testMatch: '<rootDir>/**/*.spec.js',
in my jest.config.js file.
I can make things work by running Jest against a specific sub-folder in my project, ie.
npx jest foo
will run (just) the foo folder tests in my project ... but I want it to run all of the files with *.spec.js names that are in my project folder.
How can I make Jest run all files in my folder (something it claims in the documentation to by default), and not run files in my parent folder?
P.S. The only other line in my jest.config.js file is:
presets: [['#babel/preset-env', { targets: { node: 'current' } }]],

Related

unable to run my script that is not on the root folder

I'm unable to move my index.ts file inside a src folder to organize my project.
I'm using prisma and following the 'from the scratch tutorial' (https://www.prisma.io/docs/getting-started/setup-prisma/start-from-scratch) which require just a index.ts to query the database. When I run the script just from the root folder using npx ts-node index.ts it runs fine.
But when I put it inside a folder (in my case, src/controller, two folders), it logs a
error: Cannot find module './index.ts
If I run npx ts-node src/controller/index.ts with the path to index.ts, it does run fine, but there is a way to configure the path, so I can just type index.ts?

run eslint in multi repository project

let's say, I have following project structure:
back/package.json
back/lib/Content/*.js
front/package.json
slices/budget/back/package.json
slices/budget/back/lib/Content/*.js
slices/budget/front/package.json
slices/accounting/back/package.json
slices/accounting/back/lib/Content/*.js
slices/accounting/front/package.json
how do I?
cd back && eslint ./lib/**/*.js ../slices/**/lib/Content/*.js
specifically, I want to
install eslint one time as devDependencies
somewhere in /back of root module
config eslint one time somewhere in /back/package.json:eslint key of root module
add eslint config in /back/package.json of root module just one time
eslint entire tree of modules
not in each slice seperatly
run from ci cd
so I need a way to run from /back
and later - maybe someway to respect eslint config hierarchy
not change project directory structure at all
what I receive
cd back && npm run lint
> back#1.0.0 lint
> eslint ../
Oops! Something went wrong! :(
ESLint: 8.23.1
ESLint couldn't find a configuration file
reason: https://eslint.org/docs/latest/user-guide/configuring/configuration-files#using-configuration-files
You can use the --ignore-path option to specify a file with patterns that should be ignored. The file should contain one pattern per line. For example, to ignore all files in the node_modules directory, you could create a .eslintignore file with the following contents:
node_modules
You can also use the --ignore-pattern option to specify a pattern that should be ignored. For example, to ignore all files in the node_modules directory, you could run:
eslint . --ignore-pattern node_modules
The error is probably because you haven't specified the eslint config file explicitly. To run eslint on all the modules, starting from the parent folder, run: eslint ../ -c .eslintrc.js (or whatever .eslintrc file you use in back). It seems like eslint is confused if it does not have the config file in the same directory it is running from hence you need to manually specify the path to it.
The correct way of solving this issue would be creating sharable config file with configuration you have in back right now:
module.exports = {
rules: {
semi: [2, "always"]
}
};
Then you publish it to public or private npm with a name #your-project/eslint-config and use it in .eslintrc.json that is the same in all your projects:
{
"extends": [
"#your-project/eslint-config"
]
}
This way gives you ability to configure CI in a simple and independent way if you have lots of repositories: just run eslint lib/*.js.
If you have all the repositories in one computer and want to lint all of them using one command, you can use one of my tools:
redfork, install eslint and redfork globally and run:
redfork 'eslint lib/*.js'
But maybe you need to have some changes in project structure.
runny, if you don't want to make changes in project structure, just add configuration file .runny.json:
{
"command": "eslint lib/*.js",
"directories": [
"~/one",
"~/two",
"~/three"
]
}
It will run the same command for any directory you need.
I had a similar issue and the following has solved my problem.
I guess you haven't specified the eslint config file explicitly.
To run eslint on all the modules
run: eslint ../ -c .eslintrc.js
It seems like eslint is confused if it does not have the config file in the same directory it is running from, so you need to manually specify the path to it.
no real answer, except to create .eslintignore, .eslintrc, package.json at project root

Jest - run coverage report for all files in the project?

I want to run a coverage report for all files in my code repo, including those that currently don't have any tests.
I'm using this command:
jest --coverage --collectCoverageFrom='src/features/**/*.{ts,tsx}
But there are other folders i want to cover.
Is it not possible to "tell" Jest to look at all .ts and .tsx files across all folders, including nested folders?
The command you have should generate coverage report for all .ts and .jxs (was that meant to be jsx?) inside the the folders in src/features folder.
If you want to include other folders, for example everything inside /src then use
jest --coverage --collectCoverageFrom='src/**/*.{ts,jxs}'

How to run jest on files not ending in .test.js or .spec.js?

I have files tests/foo.js tests/bar.js. When I run jest tests/foo.js or jest tests/*.js, no test is run. How do I run test on files not ending in .test.js?
You can define the patterns jest uses to detect test files through the testRegex or testMatch options.
Jest uses testMatch to find tests:
By default it looks for .js, .jsx, .ts and .tsx files inside of __tests__ folders, as well as any files with a suffix of .test or .spec (e.g. Component.test.js or Component.spec.js). It will also find files called test.js or spec.js.
In your case you can set testMatch to the following:
testMatch: [ "**/tests/**/*.[jt]s?(x)" ]
...and it should find the tests you have created in your tests folder.

create-react-app eslint issue due to higher level folder containing another app with node_modules in it

I have a git repository which has 2 projects in it, a loopback app (named app), and a react app created with create-react-app (named client).
And the directory structure is as follows;
├─┬app
│ ├──node_modules
│ ├─┬client
├─node_modules
the loopback project (app) uses eslint, has eslint in devDependencies, client does not have eslint in package.json.
Client app is created with create-react-app, so when I run react-scripts, it finds eslint in the upper directory, and complains about its version, if I delete the app\node_modules everything works fine.
So how does react-scripts find the eslint in upper directory, is there a way of telling it not to check any other node_modules folder, it should only check in the current folder.
react-scripts tells me that I can put SKIP_PREFLIGHT_CHECK=true in my .env file, is it safe to do this, does it run the eslint 3.x on the upper level folder, or does it run the required 5.6.0 version installed in client\node_modules folder?
I will setup a deployment toolchain for this project so I need make sure that it works fine all the time.
EDIT:
The config entry in my client\package.json
"eslintConfig": {
"extends": "react-app",
"root": true
},
EDIT2:
Steps to reproduce problems:
my node version is: 8.11.3
npx loopback-cli app (accept default options when prompted)
cd app
npm i
npx create-react-app client
cd client
npm i
npm run start (you should see the errors after this)
EDIT3:
I ended up ejecting react-scripts.
Try creating a .eslintrc file in your client folder, include the following content:
.eslintrc
{
"extends": "react-app"
}
Alternatively, this should work according to the docs:
{
"root": true
}
By default, ESLint will look for configuration files in all parent folders up to the root directory. This can be useful if you want all of your projects to follow a certain convention, but can sometimes lead to unexpected results. To limit ESLint to a specific project, place "root": true inside the eslintConfig field of the package.json file or in the .eslintrc.* file at your project’s root level. ESLint will stop looking in parent folders once it finds a configuration with "root": true.

Resources