vendredi 31 juillet 2020

Test specific file using Jest

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?

Aucun commentaire:

Enregistrer un commentaire