0
votes

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 command:

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:

  1. Run single test of a specific test suite in Jest
  2. How do I run a single test using Jest?
  3. How do I test a single file using Jest?

But was able to achieve the desired result running jest command 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?

1

1 Answers

0
votes

TL;DR :

  1. rename your test file fib-test.js to fib.test.js
  2. run jest fib.test.js
  3. volia, jest will run the file you specify
  4. 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:

  1. you're running on a Windows machine
  2. your project root or jest.config.js is in C:\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.