In my project folder I have lots of subfolders with js code and test.js file in each of them. I want to be able to test specific file. For example, let's say in our project folder we have 'fib' folder:
C:.
└───exercises
└───fib
fib-test.js
index.js
Now, from the exercises folder I execute jest mand:
jest fib\fib-test.js
And I get:
No tests found, exiting with code 1
Run with `--passWithNoTests` to exit with code 0
In C:\exercises
62 files checked.
testMatch: **/__tests__/**/*.[jt]s?(x), **/?(*.)+(spec|test).[tj]s?(x) - 26 matches
testPathIgnorePatterns: \\node_modules\\ - 62 matches
testRegex: - 0 matches
Pattern: fib\fib-test.js - 0 matches
If I do just jest, I'll get all tests ran. If I move fib folder out of the exercises folder it works as expected. Here is the code for all of the files:
index.js:
function fib(n) {}
module.exports = fib;
test.js:
const fib = require('./index');
test('Fib function is defined', () => {
expect(typeof fib).toEqual('function');
});
test('calculates correct fib value for 1', () => {
expect(fib(1)).toEqual(1);
});
test('calculates correct fib value for 2', () => {
expect(fib(2)).toEqual(1);
});
test('calculates correct fib value for 3', () => {
expect(fib(3)).toEqual(2);
});
test('calculates correct fib value for 4', () => {
expect(fib(4)).toEqual(3);
});
test('calculates correct fib value for 15', () => {
expect(fib(39)).toEqual(63245986);
});
package.json:
{
"name": "dev",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"jest": {
"testEnvironment": "node"
},
"author": "",
"license": "ISC"
}
I've tried all of these solutions with no success:
- Run single test of a specific test suite in Jest
- How do I run a single test using Jest?
- How do I test a single file using Jest?
But was able to achieve the desired result running jest mand with --watch flag and then in regex menu entering the relative path to the fib\test.js. The question is how to do it without entering watch menu?
In my project folder I have lots of subfolders with js code and test.js file in each of them. I want to be able to test specific file. For example, let's say in our project folder we have 'fib' folder:
C:.
└───exercises
└───fib
fib-test.js
index.js
Now, from the exercises folder I execute jest mand:
jest fib\fib-test.js
And I get:
No tests found, exiting with code 1
Run with `--passWithNoTests` to exit with code 0
In C:\exercises
62 files checked.
testMatch: **/__tests__/**/*.[jt]s?(x), **/?(*.)+(spec|test).[tj]s?(x) - 26 matches
testPathIgnorePatterns: \\node_modules\\ - 62 matches
testRegex: - 0 matches
Pattern: fib\fib-test.js - 0 matches
If I do just jest, I'll get all tests ran. If I move fib folder out of the exercises folder it works as expected. Here is the code for all of the files:
index.js:
function fib(n) {}
module.exports = fib;
test.js:
const fib = require('./index');
test('Fib function is defined', () => {
expect(typeof fib).toEqual('function');
});
test('calculates correct fib value for 1', () => {
expect(fib(1)).toEqual(1);
});
test('calculates correct fib value for 2', () => {
expect(fib(2)).toEqual(1);
});
test('calculates correct fib value for 3', () => {
expect(fib(3)).toEqual(2);
});
test('calculates correct fib value for 4', () => {
expect(fib(4)).toEqual(3);
});
test('calculates correct fib value for 15', () => {
expect(fib(39)).toEqual(63245986);
});
package.json:
{
"name": "dev",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"jest": {
"testEnvironment": "node"
},
"author": "",
"license": "ISC"
}
I've tried all of these solutions with no success:
- Run single test of a specific test suite in Jest
- How do I run a single test using Jest?
- How do I test a single file using Jest?
But was able to achieve the desired result running jest mand with --watch flag and then in regex menu entering the relative path to the fib\test.js. The question is how to do it without entering watch menu?
Share Improve this question edited Jul 31, 2020 at 11:10 tnsaturday asked Jul 31, 2020 at 10:59 tnsaturdaytnsaturday 6033 gold badges12 silver badges35 bronze badges2 Answers
Reset to default 4TL;DR :
- rename your test file
fib-test.js
tofib.test.js
- run
jest fib.test.js
- volia,
jest
will run the file you specify - you can also test any number of test file you specify only, by
jest XXX.test.js YYY.test.js
Long Story
I see that your question implicitly have a few assumptions. You can make the question more specific if you list them out explicitly:
- you're running on a Windows machine
- your project root or
jest.config.js
is inC:\exercises
For (1), I don't have a Windows machine on hand, please run & validate my solution.
Assumptions aside, the error occurs at your test file name: fib-test.js
. jest
is looking for XXX.test.js
and will not match & test XXX-test.js
. You can find the clue at error message:
testMatch: ... **/?(*.)+(spec|test).[tj]s?(x) ...
Once you rename your file to fib.test.js
, jest fib.test.js
will search any child folders from your project root directory, or where jest.config.js
at; and match & test the specific test file.
My jest
version: "ts-jest": "^27.0.3"
Small trick #1
Look at the full error message again:
testMatch: **/__tests__/**/*.[jt]s?(x), **/?(*.)+(spec|test).[tj]s?(x) - 26 matches
You can actually group all your test cases into <rootDir>/__tests__/
and put __test__
into .gitignore
so that your test cases can be kept secret and not pushed to Git.
Small trick #2
Because jest
looks at every child folders, you can actually put folder name before your test file:
jest fib/fib.test.js
is essentially equivalent to jest fib.test.js
Though, jest don't see any reason to bother yourself so much.
If you use jest.config.js, a simply manner is just to separate in several testMatch and ment/unment as needed.
module.exports = {
// verbose: true,
// silent: true,
// forceExit: true,
testEnvironment: 'node',
// testMatch: ['**/__tests__/Classes/**/Rules.test.js'],
testMatch: ['**/__tests__/Classes/**/Robot.test.js'], // testing this file
// testMatch: ['**/__tests__/api.test.js'],
// testMatch: ['**/__tests__/**/*.test.js'], // testing ALL test files!
collectCoverageFrom: ['src/**/*.js'],
coveragePathIgnorePatterns: ['/node_modules/']
}