javascript - Test specific file using Jest - Stack Overflow

In my project folder I have lots of subfolders with js code and test.js file in each of them. I want to

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:

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

  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 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 badges
Add a ment  | 

2 Answers 2

Reset to default 4

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.

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/']
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743864122a4520244.html

相关推荐

  • javascript - Test specific file using Jest - Stack Overflow

    In my project folder I have lots of subfolders with js code and test.js file in each of them. I want to

    3天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信