Knapsack Pro

Check out the new docs for the updated documentation.

FAQ / @knapsack-pro/jest / Knapsack Pro Jest specific questions

How to run tests only from a specific directory in Jest? Define your test files pattern with KNAPSACK_PRO_TEST_FILE_PATTERN

You can modify a default test file pattern KNAPSACK_PRO_TEST_FILE_PATTERN="{**/__tests__/**/*.js?(x),**/?(*.)(spec|test).js?(x)}". Set KNAPSACK_PRO_TEST_FILE_PATTERN environment variable and change pattern to match your directory with test files. The pattern uses node-glob pattern so you can't copy & paste existing pattern form your Jest config (read next section to learn more)!

The default pattern KNAPSACK_PRO_TEST_FILE_PATTERN="{**/__tests__/**/*.js?(x),**/?(*.)(spec|test).js?(x)}" in @knapsack-pro/jest matches a few directories {pattern1,pattern2}. If your tests are in single directory don't use {}. Instead, if you want to use single pattern then you can do KNAPSACK_PRO_TEST_FILE_PATTERN="__tests__/**/*.test.js".

Note @knapsack-pro/jest by default rejects tests inside of node_modules directory. If your pattern set by KNAPSACK_PRO_TEST_FILE_PATTERN matches test file paths within node_modules then those test file paths won't be run.

If you want to use a few patterns you can do it as shown above KNAPSACK_PRO_TEST_FILE_PATTERN="{pattern_1,pattern_2}".

You can also exclude tests for Jest and please note @knapsack-pro/jest rejects patterns from Jest config file.

Why Knapsack Pro needs test file pattern?

It is Knapsack Pro responsibility to decide what test files should be run. It means you need to tell Knapsack Pro where to look for your test files on your disk if you keep them in the non-default directory.

Knapsack Pro does not know and does not care about test file patterns provided to your Jest test runner as CLI argument or in any of your Jest config files. It works like that because Knapsack Pro needs to know about all existing test files on your disk before it can decide what test files should be run. It means Knapsack Pro needs to know about your test files before you even start running Jest - because of that we can't use a pattern from Jest config file or pattern provided as CLI argument to Jest itself.

By default Knapsack Pro looks for test files on the disk matching default directory structure like this KNAPSACK_PRO_TEST_FILE_PATTERN="{**/__tests__/**/*.js?(x),**/?(*.)(spec|test).js?(x)}".

You can override this pattern if you set environment variable KNAPSACK_PRO_TEST_FILE_PATTERN on your own (you must use a valid node-glob pattern - read next section!).

Defining your own KNAPSACK_PRO_TEST_FILE_PATTERN might be helpful when you keep your test files in a different directory structure than most users do.

How KNAPSACK_PRO_TEST_FILE_PATTERN works and why it uses node-glob pattern structure?

KNAPSACK_PRO_TEST_FILE_PATTERN pattern is a different type of pattern than a pattern that you are familiar with when working with Jest. Because of that you cannot copy & paste pattern from Jest config file to KNAPSACK_PRO_TEST_FILE_PATTERN!

KnapsackPro uses node-glob npm package and its pattern structure to detect test files on the disk.

IMPORTANT: You must learn how to define node-glob pattern to set a valid pattern in KNAPSACK_PRO_TEST_FILE_PATTERN! Learn how node-glob pattern works. We suggest using to test what files would match. Often the mistake is that people copy & paste the existing Jest pattern from your Jest config file to KNAPSACK_PRO_TEST_FILE_PATTERN and this won't work!

How to check the KNAPSACK_PRO_TEST_FILE_PATTERN is working?

Run node in development inside of your local project repository.

$ node
var glob = require("glob")
var KNAPSACK_PRO_TEST_FILE_PATTERN="{**/__tests__/**/*.js?(x),**/?(*.)(spec|test).js?(x)}"
glob(KNAPSACK_PRO_TEST_FILE_PATTERN, {}, function (er, files) { console.log(files) })

Example output:

> [

It should print the list of test files that are matching the pattern. This way you can confirm the pattern is valid and can be used to find the test files in your project repository. If the output is an empty array then adjust the pattern to find the test files matching the local structure of your tests directory.

How to set a few directories with test files in KNAPSACK_PRO_TEST_FILE_PATTERN?

If you want to use a few patterns you can provide them comma separated and inside of {}. Do it this way KNAPSACK_PRO_TEST_FILE_PATTERN="{pattern_1,pattern_2}".

Important to understand about KNAPSACK_PRO_TEST_FILE_PATTERN

It is important to understand that KNAPSACK_PRO_TEST_FILE_PATTERN is looking for test files inside of the same directory where you run Knapsack Pro command $(npm bin)/knapsack-pro-jest. For instance:

  • /home/user/project <- this is a directory with the source code of your project
  • /home/user/project/__tests__ <- this directory contains all your Jest test files

When you use above directory structure and you run $(npm bin)/knapsack-pro-jest inside of the directory /home/user/project then pattern KNAPSACK_PRO_TEST_FILE_PATTERN="{**/__tests__/**/*.js?(x),**/?(*.)(spec|test).js?(x)}" should find all your test files inside of /home/user/project/__tests__ directory.

If you keep test files in a different folder please adjust KNAPSACK_PRO_TEST_FILE_PATTERN to your directory structure. Remember to provide a valid node-glob pattern!

Start using Knapsack Pro

Sign up and speed up your tests.