How to run tests only from a specific directory in Cypress? Define your test files pattern with KNAPSACK_PRO_TEST_FILE_PATTERN
Knapsack Pro is responsible for deciding what test files should be run. You need to tell Knapsack Pro where to look for test files on your disk if you keep them in a non-default directory.
cypress/e2e
has been a default directory since Cypress 10.cypress/integration
is a default directory if you use the Cypress version older than 10.
Knapsack Pro does not know and does not care about test file patterns provided to your Cypress test runner as a CLI argument or in any of your Cypress config files. Knapsack Pro needs to know about all existing test files on your disk before deciding what test files should be run.
Knapsack Pro needs to know about your test files before you even start running Cypress - because of that, we can't use a pattern from the Cypress config file or pattern provided as a CLI argument to Cypress itself.
Knapsack Pro looks for test files on the disk matching default directory structure: KNAPSACK_PRO_TEST_FILE_PATTERN="cypress/e2e/**/*.{js,jsx,coffee,cjsx}"
.
You can override this pattern if you set the KNAPSACK_PRO_TEST_FILE_PATTERN
environment variable. You must use a valid node-glob pattern (read the next section!).
Defining KNAPSACK_PRO_TEST_FILE_PATTERN
might be helpful when you keep your test files in a different directory structure than most users.
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 than a pattern you are familiar with when working with Cypress. Because of that, you cannot copy & paste the pattern from the Cypress config file to KNAPSACK_PRO_TEST_FILE_PATTERN
!
KnapsackPro uses the node-glob
npm package and its pattern structure to detect test files on the disk.
IMPORTANT: You must learn to define a node-glob
pattern to set a valid pattern in KNAPSACK_PRO_TEST_FILE_PATTERN
! Learn how the node-glob pattern works.
We suggest using https://globster.xyz to test what files would match.
A common mistake is copying & pasting the existing Cypress pattern from your Cypress config file to KNAPSACK_PRO_TEST_FILE_PATTERN
, which 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="cypress/e2e/**/*.{js,jsx,coffee,cjsx}" glob(KNAPSACK_PRO_TEST_FILE_PATTERN, {}, function (er, files) { console.log(files) })
Example output:
> [ 'cypress/e2e/1-getting-started/todo.spec.js', 'cypress/e2e/2-advanced-examples/actions.spec.js', 'cypress/e2e/2-advanced-examples/aliasing.spec.js', 'cypress/e2e/2-advanced-examples/window.spec.js' ]
It should print the list of test files 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 {}
, like this: KNAPSACK_PRO_TEST_FILE_PATTERN="{pattern_1,pattern_2}"
.
How to exclude test files?
You can use KNAPSACK_PRO_TEST_FILE_EXCLUDE_PATTERN
to exclude the test files.
You can combine both KNAPSACK_PRO_TEST_FILE_PATTERN
and KNAPSACK_PRO_TEST_FILE_EXCLUDE_PATTERN
patterns at the same time if you like.
Important to understand KNAPSACK_PRO_TEST_FILE_PATTERN
It is important to understand that KNAPSACK_PRO_TEST_FILE_PATTERN
is looking for test files inside the same directory where you run Knapsack Pro command $(npm bin)/knapsack-pro-cypress
. For instance:
/home/user/project
<- this is a directory with the source code of your project/home/user/project/cypress/e2e
<- this directory contains all your Cypress test files
When you use the above directory structure and you run $(npm bin)/knapsack-pro-cypress
inside of the /home/user/project
directory then KNAPSACK_PRO_TEST_FILE_PATTERN="cypress/e2e/**/*.{js,jsx,coffee,cjsx}"
pattern should find all your test files inside of the /home/user/project/cypress/e2e
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!