@knapsack-pro/jest 5.3.0 → 5.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -617
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -12,27 +12,6 @@ Learn about Knapsack Pro Queue Mode in the video [how to run tests with dynamic
|
|
|
12
12
|
## Table of Contents
|
|
13
13
|
|
|
14
14
|
- [Installation](#installation)
|
|
15
|
-
- [How to use](#how-to-use)
|
|
16
|
-
- [Configuration steps](#configuration-steps)
|
|
17
|
-
- [CI steps](#ci-steps)
|
|
18
|
-
- [CircleCI](#circleci)
|
|
19
|
-
- [Travis CI](#travis-ci)
|
|
20
|
-
- [Buildkite.com](#buildkitecom)
|
|
21
|
-
- [Codeship.com](#codeshipcom)
|
|
22
|
-
- [Heroku CI](#heroku-ci)
|
|
23
|
-
- [Solano CI](#solano-ci)
|
|
24
|
-
- [AppVeyor](#appveyor)
|
|
25
|
-
- [GitLab CI](#gitlab-ci)
|
|
26
|
-
- [GitLab CI `>= 11.5`](#gitlab-ci--115)
|
|
27
|
-
- [GitLab CI `< 11.5` (old GitLab CI)](#gitlab-ci--115-old-gitlab-ci)
|
|
28
|
-
- [SemaphoreCI.com](#semaphorecicom)
|
|
29
|
-
- [Semaphore 2.0](#semaphore-20)
|
|
30
|
-
- [Semaphore 1.0](#semaphore-10)
|
|
31
|
-
- [Cirrus-CI.org](#cirrus-ciorg)
|
|
32
|
-
- [Jenkins](#jenkins)
|
|
33
|
-
- [GitHub Actions](#github-actions)
|
|
34
|
-
- [Codefresh.io](#codefreshio)
|
|
35
|
-
- [Other CI provider](#other-ci-provider)
|
|
36
15
|
- [FAQ](#faq)
|
|
37
16
|
- [Development](#development)
|
|
38
17
|
- [Requirements](#requirements)
|
|
@@ -47,600 +26,7 @@ Learn about Knapsack Pro Queue Mode in the video [how to run tests with dynamic
|
|
|
47
26
|
|
|
48
27
|
## Installation
|
|
49
28
|
|
|
50
|
-
|
|
51
|
-
`@knapsack-pro/jest` uses `jest` version installed in your project.
|
|
52
|
-
|
|
53
|
-
For `npm` users:
|
|
54
|
-
|
|
55
|
-
```
|
|
56
|
-
$ npm install --save-dev @knapsack-pro/jest
|
|
57
|
-
```
|
|
58
|
-
|
|
59
|
-
For `yarn` users:
|
|
60
|
-
|
|
61
|
-
```
|
|
62
|
-
$ yarn add --dev @knapsack-pro/jest
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
Whenever you see `npm` in below steps you can use `yarn` there as well.
|
|
66
|
-
|
|
67
|
-
## How to use
|
|
68
|
-
|
|
69
|
-
### Configuration steps
|
|
70
|
-
|
|
71
|
-
1. To get API token just sign up at [KnapsackPro.com](https://knapsackpro.com?utm_source=github&utm_medium=readme&utm_campaign=%40knapsack-pro%2Fjest&utm_content=sign_up). Please add to your CI environment variables `KNAPSACK_PRO_TEST_SUITE_TOKEN_JEST`. You can generate API token in [user dashboard](https://knapsackpro.com/dashboard).
|
|
72
|
-
|
|
73
|
-
Next to your API token, you can find a link "Build metrics" where you can preview recorded CI builds. You will see yellow tips if something is not configured as expected. Refresh the page once you finish running tests to see new tips. You can also click "Show" on CI build to see details about particular CI build. Look for yellow tips suggesting what to change to ensure all works fine for your project.
|
|
74
|
-
|
|
75
|
-
2. (optional) Do you want to use "retry single failed parallel CI node" feature for your CI? For instance some of CI providers like Travis CI, Buildkite or Codeship allows you to retry only one of failed parallel CI node instead of retrying the whole CI build with all parallel CI nodes. If you want to be able to retry only single failed parallel CI node then you need to tell Knapsack Pro API to remember the way how test files where allocated across parallel CI nodes by adding to your CI environment variables `KNAPSACK_PRO_FIXED_QUEUE_SPLIT=true`.
|
|
76
|
-
|
|
77
|
-
The default is `KNAPSACK_PRO_FIXED_QUEUE_SPLIT=false` which means when you want to retry the whole failed CI build then a new dynamic test suite split will happen across all retried parallel CI nodes thanks to Knapsack Pro Queue Mode. Some people may prefer to retry the whole failed CI build with test files allocated across parallel CI nodes in the same order as it happend for the failed CI build - in such case you should set `KNAPSACK_PRO_FIXED_QUEUE_SPLIT=true`.
|
|
78
|
-
|
|
79
|
-
3. (optional) If one of the parallel CI nodes starts work very late after other parallel CI nodes already finished work.
|
|
80
|
-
|
|
81
|
-
Some of CI providers have a problem with starting parallel CI nodes as soon as possible. For instance, you have a fixed pool of parallel CI nodes shared with many CI builds and sometimes CI build has started work even the pool has not enough available parallel CI nodes at the moment. Another case is when the CI provider infrastructure is overloaded which can lead to some parallel CI nodes starting work later than others.
|
|
82
|
-
|
|
83
|
-
Do you have the CI server that does not start all parallel CI nodes at the same time and one of your parallel CI nodes will start work very late after all other parallel CI nodes already finished consuming tests from the Knapsack Pro Queue? In such a case, if you would use default `KNAPSACK_PRO_FIXED_QUEUE_SPLIT=false` then the very late CI node would start running all tests again based on a new Queue which means you would run test suite twice. This problem can happen if your test suite is very small and differences in the start time of parallel CI nodes are very big.
|
|
84
|
-
|
|
85
|
-
You should set `KNAPSACK_PRO_FIXED_QUEUE_SPLIT=true` to ensure the very late parallel CI node won't run tests again if the Queue was already consumed. The downside of this is that you won't be able to run 2nd CI build for the same set of values git commit/branch/ci node total number with a dynamic test suite split way if your CI provider does not expose unique CI build ID. Instead, the tests will be run assigned to the same parallel CI node indexes with the same order as it was recorded for the first time.
|
|
86
|
-
|
|
87
|
-
Knapsack Pro tries to detect CI build ID from the environment variables of your CI provider. Here you can check if your CI provider exposes CI build ID, see function [`ciNodeBuildId`](https://github.com/KnapsackPro/knapsack-pro-core-js/blob/master/src/ci-providers/github-actions.ts#L14) (example for Github Actions). If you CI provider won't provide CI build ID you can set `KNAPSACK_PRO_CI_NODE_BUILD_ID` (see next point).
|
|
88
|
-
|
|
89
|
-
4. (optional) `@knapsack-pro/jest` detects information about CI build from supported CI environment variables. When information like git branch name and git commit hash cannot be detect from CI environment variables then `@knapsack-pro/jest` will try to use git installed on CI machine to detect the infomation. If you don't have git installed then you should set the information using environment variables:
|
|
90
|
-
|
|
91
|
-
- `KNAPSACK_PRO_COMMIT_HASH` - git commit hash (SHA1)
|
|
92
|
-
- `KNAPSACK_PRO_BRANCH` - git branch name
|
|
93
|
-
- `KNAPSACK_PRO_CI_NODE_BUILD_ID` - a unique ID for your CI build. All parallel CI nodes being part of single CI build must have the same node build ID. Example how to generate node build ID: `KNAPSACK_PRO_CI_NODE_BUILD_ID=$(openssl rand - base64 32)`.
|
|
94
|
-
|
|
95
|
-
5. If you have test files in a non-default directory or you specify test files to run in Jest config file then you won't be able to run tests and you may see below error.
|
|
96
|
-
|
|
97
|
-
```
|
|
98
|
-
Response body:
|
|
99
|
-
{ errors: [ { test_files: [ 'parameter is required' ] } ] }
|
|
100
|
-
```
|
|
101
|
-
|
|
102
|
-
Please [adjust `KNAPSACK_PRO_TEST_FILE_PATTERN`](https://knapsackpro.com/faq/question/how-to-run-tests-only-from-specific-directory-in-jest) variable to match your test files directory structure to let Knapsack Pro detect all the test files you want to run in parallel. You can also exclude test files with [`KNAPSACK_PRO_TEST_FILE_EXCLUDE_PATTERN`](https://knapsackpro.com/faq/question/how-to-exclude-tests-to-ignore-them-from-running-in-jest).
|
|
103
|
-
|
|
104
|
-
6. If you want to use [Jest config file with `@knapsack-pro/jest`](https://knapsackpro.com/faq/question/how-to-use-jest-config-file-with-knapsack-pro-jest) check this tip.
|
|
105
|
-
|
|
106
|
-
7. If you use `--coverage` flag for Jest to generate code coverage then to make it work please check [how to generate code coverage for Jest with `@knapsack-pro/jest` in Queue Mode](https://knapsackpro.com/faq/question/how-to-generate-code-coverage-for-jest-with-knapsack-pro-jest-in-queue-mode)?
|
|
107
|
-
|
|
108
|
-
8. If you use `jest-junit` please check [how to generate XML report using `jest-junit` with `@knapsack-pro/jest` in Queue Mode](https://knapsackpro.com/faq/question/how-to-generate-xml-report-using-jest-junit-with-knapsack-pro-jest-in-queue-mode)?
|
|
109
|
-
|
|
110
|
-
9. To run tests faster you should pass option [`--runInBand`](https://jestjs.io/docs/en/cli#--runinband) to Knapsack Pro that will pass it down to Jest:
|
|
111
|
-
|
|
112
|
-
```
|
|
113
|
-
$(npm bin)/knapsack-pro-jest --runInBand
|
|
114
|
-
```
|
|
115
|
-
|
|
116
|
-
You will see where to add the above command in your CI provider config in further steps.
|
|
117
|
-
|
|
118
|
-
`--runInBand` does run all tests serially in the current process, rather than creating a worker pool of child processes. Knapsack Pro starts Jest process for each set of tests fetched from Knapsack Pro Queue API.
|
|
119
|
-
By using `--runInBand` you can save time by not starting the worker pool of child processes for each set of tests fetched from Knapsack Pro Queue API.
|
|
120
|
-
|
|
121
|
-
See [example how test suite can run 2 times faster with `--runInBand`](https://github.com/KnapsackPro/knapsack-pro-jest/issues/24). You can test this option yourself to verify if it helps increase the speed of tests in case of your test suite.
|
|
122
|
-
|
|
123
|
-
10. If your code is written in TypeScript and you run specs with `ts-jest`, consider moving typechecking and diagnostics to a separate CI step and disable typechecking on Jest runs.
|
|
124
|
-
This setting prevents `ts-jest` from running `tsc` on your code for every Knapsack grouping, repeating effort. On a larger codebase, it could prevent increasing your execution time by more than 50%.
|
|
125
|
-
|
|
126
|
-
```json
|
|
127
|
-
/* jest.config.js */
|
|
128
|
-
{
|
|
129
|
-
"globals": {
|
|
130
|
-
"ts-jest": {
|
|
131
|
-
"diagnostics": false,
|
|
132
|
-
"isolatedModules": true
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
```
|
|
137
|
-
|
|
138
|
-
11. Please select your CI provider and follow instructions to run tests with `@knapsack-pro/jest`.
|
|
139
|
-
|
|
140
|
-
- [CircleCI](#circleci)
|
|
141
|
-
- [Travis CI](#travis-ci)
|
|
142
|
-
- [Buildkite.com](#buildkitecom)
|
|
143
|
-
- [Codeship.com](#codeshipcom)
|
|
144
|
-
- [Heroku CI](#heroku-ci)
|
|
145
|
-
- [Solano CI](#solano-ci)
|
|
146
|
-
- [AppVeyor](#appveyor)
|
|
147
|
-
- [GitLab CI](#gitlab-ci)
|
|
148
|
-
- [SemaphoreCI.com](#semaphorecicom)
|
|
149
|
-
- [Cirrus-CI.org](#cirrus-ciorg)
|
|
150
|
-
- [Jenkins](#jenkins)
|
|
151
|
-
- [GitHub Actions](#github-actions)
|
|
152
|
-
- [Codefresh.io](#codefreshio)
|
|
153
|
-
- [Other CI provider](#other-ci-provider)
|
|
154
|
-
|
|
155
|
-
### CI steps
|
|
156
|
-
|
|
157
|
-
#### CircleCI
|
|
158
|
-
|
|
159
|
-
Example configuration for CircleCI 2.0 platform.
|
|
160
|
-
|
|
161
|
-
```YAML
|
|
162
|
-
# ~/.circleci/config.yml
|
|
163
|
-
version: 2
|
|
164
|
-
jobs:
|
|
165
|
-
test:
|
|
166
|
-
docker:
|
|
167
|
-
- image: circleci/<language>:<version TAG>
|
|
168
|
-
parallelism: 2 # run 2 parallel CI nodes
|
|
169
|
-
|
|
170
|
-
steps:
|
|
171
|
-
- checkout
|
|
172
|
-
|
|
173
|
-
- run:
|
|
174
|
-
name: Run Jest tests with @knapsack-pro/jest using Knapsack Pro Queue Mode
|
|
175
|
-
command: $(npm bin)/knapsack-pro-jest
|
|
176
|
-
```
|
|
177
|
-
|
|
178
|
-
Please remember to add additional parallel containers for your project in CircleCI settings.
|
|
179
|
-
|
|
180
|
-
#### Travis CI
|
|
181
|
-
|
|
182
|
-
You can parallelize your CI build across virtual machines with [travis matrix feature](https://docs.travis-ci.com/user/speeding-up-the-build/#parallelizing-your-builds-across-virtual-machines).
|
|
183
|
-
|
|
184
|
-
```yaml
|
|
185
|
-
# .travis.yml
|
|
186
|
-
script:
|
|
187
|
-
- '$(npm bin)/knapsack-pro-jest'
|
|
188
|
-
|
|
189
|
-
env:
|
|
190
|
-
global:
|
|
191
|
-
- KNAPSACK_PRO_CI_NODE_TOTAL=2
|
|
192
|
-
# allows to be able to retry failed tests on one of parallel job (CI node)
|
|
193
|
-
- KNAPSACK_PRO_FIXED_QUEUE_SPLIT=true
|
|
194
|
-
|
|
195
|
-
jobs:
|
|
196
|
-
- KNAPSACK_PRO_CI_NODE_INDEX=0
|
|
197
|
-
- KNAPSACK_PRO_CI_NODE_INDEX=1
|
|
198
|
-
```
|
|
199
|
-
|
|
200
|
-
The configuration will generate matrix with 2 parallel jobs (2 parallel CI nodes):
|
|
201
|
-
|
|
202
|
-
```
|
|
203
|
-
# first CI node (first parallel job)
|
|
204
|
-
KNAPSACK_PRO_CI_NODE_TOTAL=2 KNAPSACK_PRO_CI_NODE_INDEX=0
|
|
205
|
-
|
|
206
|
-
# second CI node (second parallel job)
|
|
207
|
-
KNAPSACK_PRO_CI_NODE_TOTAL=2 KNAPSACK_PRO_CI_NODE_INDEX=1
|
|
208
|
-
```
|
|
209
|
-
|
|
210
|
-
More info about global and matrix ENV configuration in [travis docs](https://docs.travis-ci.com/user/customizing-the-build/#build-matrix).
|
|
211
|
-
|
|
212
|
-
#### Buildkite.com
|
|
213
|
-
|
|
214
|
-
The only thing you need to do is to configure the parallelism parameter (number of parallel agents) in your build step and run the below command in your build:
|
|
215
|
-
|
|
216
|
-
```
|
|
217
|
-
$(npm bin)/knapsack-pro-jest
|
|
218
|
-
```
|
|
219
|
-
|
|
220
|
-
If you want to use Buildkite retry single agent feature to retry just failed tests on particular agent (CI node) then you should set `KNAPSACK_PRO_FIXED_QUEUE_SPLIT=true`.
|
|
221
|
-
|
|
222
|
-
**Other useful resources:**
|
|
223
|
-
|
|
224
|
-
Here you can find article [how to set up a new pipeline for your project in Buildkite and configure Knapsack Pro](http://docs.knapsackpro.com/2017/auto-balancing-7-hours-tests-between-100-parallel-jobs-on-ci-buildkite-example) and 2 example repositories for Ruby/Rails projects:
|
|
225
|
-
|
|
226
|
-
- [Buildkite Rails Parallel Example with Knapsack Pro](https://github.com/KnapsackPro/buildkite-rails-parallel-example-with-knapsack_pro)
|
|
227
|
-
- [Buildkite Rails Docker Parallel Example with Knapsack Pro](https://github.com/KnapsackPro/buildkite-rails-docker-parallel-example-with-knapsack_pro)
|
|
228
|
-
|
|
229
|
-
When using the `docker-compose` plugin on Buildkite, you have to tell it which environment variables to pass to the docker container. Thanks to it Knapsack Pro can detect info about CI build like commit, branch name, amount of parallel nodes.
|
|
230
|
-
|
|
231
|
-
```yaml
|
|
232
|
-
steps:
|
|
233
|
-
- label: 'Test'
|
|
234
|
-
parallelism: 2
|
|
235
|
-
plugins:
|
|
236
|
-
- docker-compose#3.0.3:
|
|
237
|
-
run: app
|
|
238
|
-
command: $(npm bin)/knapsack-pro-jest
|
|
239
|
-
config: docker-compose.test.yml
|
|
240
|
-
env:
|
|
241
|
-
- BUILDKITE_PARALLEL_JOB_COUNT
|
|
242
|
-
- BUILDKITE_PARALLEL_JOB
|
|
243
|
-
- BUILDKITE_BUILD_NUMBER
|
|
244
|
-
- BUILDKITE_COMMIT
|
|
245
|
-
- BUILDKITE_BRANCH
|
|
246
|
-
```
|
|
247
|
-
|
|
248
|
-
#### Codeship.com
|
|
249
|
-
|
|
250
|
-
Codeship does not provide parallel jobs environment variables so you will have to define `KNAPSACK_PRO_CI_NODE_TOTAL` and `KNAPSACK_PRO_CI_NODE_INDEX` for each [parallel test pipeline](https://documentation.codeship.com/basic/builds-and-configuration/parallel-tests/#using-parallel-test-pipelines). Below is an example for 2 parallel test pipelines.
|
|
251
|
-
|
|
252
|
-
Configure test pipelines (1/2 used)
|
|
253
|
-
|
|
254
|
-
```
|
|
255
|
-
# first CI node running in parallel
|
|
256
|
-
KNAPSACK_PRO_CI_NODE_TOTAL=2 KNAPSACK_PRO_CI_NODE_INDEX=0 $(npm bin)/knapsack-pro-jest
|
|
257
|
-
```
|
|
258
|
-
|
|
259
|
-
Configure test pipelines (2/2 used)
|
|
260
|
-
|
|
261
|
-
```
|
|
262
|
-
# second CI node running in parallel
|
|
263
|
-
KNAPSACK_PRO_CI_NODE_TOTAL=2 KNAPSACK_PRO_CI_NODE_INDEX=1 $(npm bin)/knapsack-pro-jest
|
|
264
|
-
```
|
|
265
|
-
|
|
266
|
-
Remember to add API token `KNAPSACK_PRO_TEST_SUITE_TOKEN_JEST` to `Environment` page of your project settings in Codeship.
|
|
267
|
-
|
|
268
|
-
CodeShip uses the same build number if you restart a build. Because of that you need to set `KNAPSACK_PRO_FIXED_QUEUE_SPLIT=true` in order to be able to restart CI build.
|
|
269
|
-
|
|
270
|
-
#### Heroku CI
|
|
271
|
-
|
|
272
|
-
You can parallelize your tests on [Heroku CI](https://devcenter.heroku.com/articles/heroku-ci) by configuring `app.json` for your project.
|
|
273
|
-
|
|
274
|
-
You can set how many parallel dynos with tests you want to run with `quantity` value.
|
|
275
|
-
Use `test` key to run tests with `@knapsack-pro/jest` as shown in below example.
|
|
276
|
-
|
|
277
|
-
You need to specify also the environment variable `KNAPSACK_PRO_TEST_SUITE_TOKEN_JEST` with API token for Knapsack Pro.
|
|
278
|
-
For any sensitive environment variables (like Knapsack Pro API token) that you do not want commited in your `app.json` manifest, you can add them to your pipeline’s Heroku CI settings.
|
|
279
|
-
|
|
280
|
-
```json
|
|
281
|
-
# app.json
|
|
282
|
-
{
|
|
283
|
-
"environments": {
|
|
284
|
-
"test": {
|
|
285
|
-
"formation": {
|
|
286
|
-
"test": {
|
|
287
|
-
"quantity": 2
|
|
288
|
-
}
|
|
289
|
-
},
|
|
290
|
-
"addons": [
|
|
291
|
-
"heroku-postgresql"
|
|
292
|
-
],
|
|
293
|
-
"scripts": {
|
|
294
|
-
"test": "$(npm bin)/knapsack-pro-jest"
|
|
295
|
-
},
|
|
296
|
-
"env": {
|
|
297
|
-
"KNAPSACK_PRO_TEST_SUITE_TOKEN_JEST": "example"
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
```
|
|
303
|
-
|
|
304
|
-
If you would like to [run multiple Knapsack Pro commands for different test runners on Heroku CI](https://knapsackpro.com/faq/question/how-to-run-multiple-test-suite-commands-in-heroku-ci) please follow tips.
|
|
305
|
-
|
|
306
|
-
Note the [Heroku CI Parallel Test Runs](https://devcenter.heroku.com/articles/heroku-ci-parallel-test-runs) are in Beta and you may need to ask Heroku support to enable it for your project.
|
|
307
|
-
|
|
308
|
-
You can learn more about [Heroku CI](https://devcenter.heroku.com/articles/heroku-ci).
|
|
309
|
-
|
|
310
|
-
#### Solano CI
|
|
311
|
-
|
|
312
|
-
[Solano CI](https://www.solanolabs.com) does not provide parallel jobs environment variables so you will have to define `KNAPSACK_PRO_CI_NODE_TOTAL` and `KNAPSACK_PRO_CI_NODE_INDEX` for each parallel job running as part of the same CI build.
|
|
313
|
-
|
|
314
|
-
```
|
|
315
|
-
# Step for first CI node
|
|
316
|
-
KNAPSACK_PRO_CI_NODE_TOTAL=2 KNAPSACK_PRO_CI_NODE_INDEX=0 $(npm bin)/knapsack-pro-jest
|
|
317
|
-
|
|
318
|
-
# Step for second CI node
|
|
319
|
-
KNAPSACK_PRO_CI_NODE_TOTAL=2 KNAPSACK_PRO_CI_NODE_INDEX=1 $(npm bin)/knapsack-pro-jest
|
|
320
|
-
```
|
|
321
|
-
|
|
322
|
-
Please remember to set up API token `KNAPSACK_PRO_TEST_SUITE_TOKEN_JEST` as global environment.
|
|
323
|
-
|
|
324
|
-
#### AppVeyor
|
|
325
|
-
|
|
326
|
-
[AppVeyor](https://www.appveyor.com) does not provide parallel jobs environment variables so you will have to define `KNAPSACK_PRO_CI_NODE_TOTAL` and `KNAPSACK_PRO_CI_NODE_INDEX` for each parallel job running as part of the same CI build.
|
|
327
|
-
|
|
328
|
-
```
|
|
329
|
-
# Step for first CI node
|
|
330
|
-
KNAPSACK_PRO_CI_NODE_TOTAL=2 KNAPSACK_PRO_CI_NODE_INDEX=0 $(npm bin)/knapsack-pro-jest
|
|
331
|
-
|
|
332
|
-
# Step for second CI node
|
|
333
|
-
KNAPSACK_PRO_CI_NODE_TOTAL=2 KNAPSACK_PRO_CI_NODE_INDEX=1 $(npm bin)/knapsack-pro-jest
|
|
334
|
-
```
|
|
335
|
-
|
|
336
|
-
Please remember to set up API token `KNAPSACK_PRO_TEST_SUITE_TOKEN_JEST` as global environment.
|
|
337
|
-
|
|
338
|
-
#### GitLab CI
|
|
339
|
-
|
|
340
|
-
Remember to add API token `KNAPSACK_PRO_TEST_SUITE_TOKEN_JEST` to [Secret Variables](https://gitlab.com/help/ci/variables/README.md#secret-variables) in `Gitlab CI Settings -> CI/CD Pipelines -> Secret Variables`.
|
|
341
|
-
|
|
342
|
-
##### GitLab CI `>= 11.5`
|
|
343
|
-
|
|
344
|
-
```yaml
|
|
345
|
-
test:
|
|
346
|
-
parallel: 2
|
|
347
|
-
script: $(npm bin)/knapsack-pro-jest
|
|
348
|
-
```
|
|
349
|
-
|
|
350
|
-
Here you can find info [how to configure the GitLab parallel CI nodes](https://docs.gitlab.com/ee/ci/yaml/#parallel).
|
|
351
|
-
|
|
352
|
-
##### GitLab CI `< 11.5` (old GitLab CI)
|
|
353
|
-
|
|
354
|
-
GitLab CI does not provide parallel jobs environment variables so you will have to define `KNAPSACK_PRO_CI_NODE_TOTAL` and `KNAPSACK_PRO_CI_NODE_INDEX` for each parallel job running as part of the same `test` stage. Below is relevant part of `.gitlab-ci.yml` configuration for 2 parallel jobs.
|
|
355
|
-
|
|
356
|
-
```yaml
|
|
357
|
-
# .gitlab-ci.yml
|
|
358
|
-
stages:
|
|
359
|
-
- test
|
|
360
|
-
|
|
361
|
-
variables:
|
|
362
|
-
KNAPSACK_PRO_CI_NODE_TOTAL: 2
|
|
363
|
-
|
|
364
|
-
# first CI node running in parallel
|
|
365
|
-
test_ci_node_0:
|
|
366
|
-
stage: test
|
|
367
|
-
script:
|
|
368
|
-
- export KNAPSACK_PRO_CI_NODE_INDEX=0
|
|
369
|
-
- $(npm bin)/knapsack-pro-jest
|
|
370
|
-
|
|
371
|
-
# second CI node running in parallel
|
|
372
|
-
test_ci_node_1:
|
|
373
|
-
stage: test
|
|
374
|
-
script:
|
|
375
|
-
- export KNAPSACK_PRO_CI_NODE_INDEX=1
|
|
376
|
-
- $(npm bin)/knapsack-pro-jest
|
|
377
|
-
```
|
|
378
|
-
|
|
379
|
-
#### SemaphoreCI.com
|
|
380
|
-
|
|
381
|
-
##### Semaphore 2.0
|
|
382
|
-
|
|
383
|
-
`@knapsack-pro/jest` supports environment variables provided by Semaphore CI 2.0 to run your tests. You will have to define a few things in `.semaphore/semaphore.yml` config file.
|
|
384
|
-
|
|
385
|
-
- You need to set `KNAPSACK_PRO_TEST_SUITE_TOKEN_JEST`. If you don't want to commit secrets in yml file then you can [follow this guide](https://docs.semaphoreci.com/article/66-environment-variables-and-secrets).
|
|
386
|
-
- You should create as many parallel jobs as you need with `parallelism` property. If your test suite is slow you should use more parallel jobs.
|
|
387
|
-
|
|
388
|
-
Below you can find example part of Semaphore CI 2.0 config.
|
|
389
|
-
|
|
390
|
-
```yaml
|
|
391
|
-
blocks:
|
|
392
|
-
- name: Jest tests
|
|
393
|
-
task:
|
|
394
|
-
env_vars:
|
|
395
|
-
- name: KNAPSACK_PRO_TEST_SUITE_TOKEN_JEST
|
|
396
|
-
value: your_api_token_here
|
|
397
|
-
prologue:
|
|
398
|
-
commands:
|
|
399
|
-
- checkout
|
|
400
|
-
- nvm install --lts carbon
|
|
401
|
-
- sem-version node --lts carbon
|
|
402
|
-
|
|
403
|
-
jobs:
|
|
404
|
-
- name: Run tests with Knapsack Pro
|
|
405
|
-
parallelism: 2
|
|
406
|
-
commands:
|
|
407
|
-
- $(npm bin)/knapsack-pro-jest
|
|
408
|
-
```
|
|
409
|
-
|
|
410
|
-
##### Semaphore 1.0
|
|
411
|
-
|
|
412
|
-
The only thing you need to do is set up `@knapsack-pro/jest` for as many parallel threads as you need. Here is an example:
|
|
413
|
-
|
|
414
|
-
```
|
|
415
|
-
# Thread 1
|
|
416
|
-
$(npm bin)/knapsack-pro-jest
|
|
417
|
-
|
|
418
|
-
# Thread 2
|
|
419
|
-
$(npm bin)/knapsack-pro-jest
|
|
420
|
-
```
|
|
421
|
-
|
|
422
|
-
Tests will be split across 2 parallel threads.
|
|
423
|
-
|
|
424
|
-
Please remember to set up API token `KNAPSACK_PRO_TEST_SUITE_TOKEN_JEST` as global environment.
|
|
425
|
-
|
|
426
|
-
#### Cirrus-CI.org
|
|
427
|
-
|
|
428
|
-
The only thing you need to do is to configure number of parallel CI nodes for your project by using [matrix modification](https://cirrus-ci.org/guide/writing-tasks/#matrix-modification). See example for 2 parallel CI nodes.
|
|
429
|
-
|
|
430
|
-
```yaml
|
|
431
|
-
# .cirrus.yml
|
|
432
|
-
task:
|
|
433
|
-
matrix:
|
|
434
|
-
name: CI node 0
|
|
435
|
-
name: CI node 1
|
|
436
|
-
test_script: $(npm bin)/knapsack-pro-jest
|
|
437
|
-
```
|
|
438
|
-
|
|
439
|
-
Please remember to set up API token `KNAPSACK_PRO_TEST_SUITE_TOKEN_JEST` as global environment.
|
|
440
|
-
|
|
441
|
-
Here is Ruby example for [`.cirrus.yml` configuration file](https://cirrus-ci.org/examples/#ruby) that you may find useful.
|
|
442
|
-
|
|
443
|
-
#### Jenkins
|
|
444
|
-
|
|
445
|
-
In order to run parallel jobs with Jenkins you should use Jenkins Pipeline.
|
|
446
|
-
You can learn basics about it in the article [Parallelism and Distributed Builds with Jenkins](https://www.cloudbees.com/blog/parallelism-and-distributed-builds-jenkins).
|
|
447
|
-
|
|
448
|
-
Here is example `Jenkinsfile` working with Jenkins Pipeline.
|
|
449
|
-
|
|
450
|
-
```groovy
|
|
451
|
-
timeout(time: 60, unit: 'MINUTES') {
|
|
452
|
-
node() {
|
|
453
|
-
stage('Checkout') {
|
|
454
|
-
checkout([/* checkout code from git */])
|
|
455
|
-
|
|
456
|
-
// determine git commit hash because we need to pass it to Knapsack Pro
|
|
457
|
-
COMMIT_HASH = sh(returnStdout: true, script: 'git rev-parse HEAD').trim()
|
|
458
|
-
|
|
459
|
-
stash 'source'
|
|
460
|
-
}
|
|
461
|
-
}
|
|
462
|
-
|
|
463
|
-
def num_nodes = 4; // define your total number of CI nodes (how many parallel jobs will be executed)
|
|
464
|
-
def nodes = [:]
|
|
465
|
-
|
|
466
|
-
for (int i = 0; i < num_nodes; i++) {
|
|
467
|
-
def index = i;
|
|
468
|
-
nodes["ci_node_${i}"] = {
|
|
469
|
-
node() {
|
|
470
|
-
stage('Setup') {
|
|
471
|
-
unstash 'source'
|
|
472
|
-
// other setup steps
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
def knapsack_options = """\
|
|
476
|
-
KNAPSACK_PRO_CI_NODE_TOTAL=${num_nodes}\
|
|
477
|
-
KNAPSACK_PRO_CI_NODE_INDEX=${index}\
|
|
478
|
-
KNAPSACK_PRO_COMMIT_HASH=${COMMIT_HASH}\
|
|
479
|
-
KNAPSACK_PRO_BRANCH=${env.BRANCH_NAME}\
|
|
480
|
-
KNAPSACK_PRO_CI_NODE_BUILD_ID=${env.BUILD_TAG}\
|
|
481
|
-
"""
|
|
482
|
-
|
|
483
|
-
// example how to run tests with Knapsack Pro
|
|
484
|
-
stage('Run tests') {
|
|
485
|
-
sh """${knapsack_options} $(npm bin)/knapsack-pro-jest"""
|
|
486
|
-
}
|
|
487
|
-
}
|
|
488
|
-
}
|
|
489
|
-
}
|
|
490
|
-
|
|
491
|
-
parallel nodes // run CI nodes in parallel
|
|
492
|
-
}
|
|
493
|
-
```
|
|
494
|
-
|
|
495
|
-
Remember to set environment variable `KNAPSACK_PRO_TEST_SUITE_TOKEN_JEST` in Jenkins configuration with your API token.
|
|
496
|
-
|
|
497
|
-
#### GitHub Actions
|
|
498
|
-
|
|
499
|
-
`@knapsack-pro/jest` supports environment variables provided by GitHub Actions to run your tests. You have to define a few things in `.github/workflows/main.yaml` config file.
|
|
500
|
-
|
|
501
|
-
- You need to set `KNAPSACK_PRO_TEST_SUITE_TOKEN_JEST` environment variable in GitHub repository Settings -> Secrets. See [creating and using secrets in GitHub Actions](https://help.github.com/en/articles/virtual-environments-for-github-actions#creating-and-using-secrets-encrypted-variables).
|
|
502
|
-
- You should create as many parallel jobs as you need with `matrix.ci_node_total` and `matrix.ci_node_index` properties. If your test suite is slow you should use more parallel jobs.
|
|
503
|
-
|
|
504
|
-
Below you can find config for GitHub Actions.
|
|
505
|
-
|
|
506
|
-
```yaml
|
|
507
|
-
# .github/workflows/main.yaml
|
|
508
|
-
name: Main
|
|
509
|
-
|
|
510
|
-
on: [push]
|
|
511
|
-
|
|
512
|
-
jobs:
|
|
513
|
-
build:
|
|
514
|
-
runs-on: ubuntu-latest
|
|
515
|
-
|
|
516
|
-
strategy:
|
|
517
|
-
fail-fast: false
|
|
518
|
-
matrix:
|
|
519
|
-
node-version: [8.x]
|
|
520
|
-
# Set N number of parallel jobs you want to run tests on.
|
|
521
|
-
# Use higher number if you have slow tests to split them on more parallel jobs.
|
|
522
|
-
# Remember to update ci_node_index below to 0..N-1
|
|
523
|
-
ci_node_total: [2]
|
|
524
|
-
# set N-1 indexes for parallel jobs
|
|
525
|
-
# When you run 2 parallel jobs then first job will have index 0, the second job will have index 1 etc
|
|
526
|
-
ci_node_index: [0, 1]
|
|
527
|
-
|
|
528
|
-
steps:
|
|
529
|
-
- uses: actions/checkout@v1
|
|
530
|
-
|
|
531
|
-
- name: Use Node.js ${{ matrix.node-version }}
|
|
532
|
-
uses: actions/setup-node@v1
|
|
533
|
-
with:
|
|
534
|
-
node-version: ${{ matrix.node-version }}
|
|
535
|
-
|
|
536
|
-
- name: npm install and build
|
|
537
|
-
run: |
|
|
538
|
-
npm install
|
|
539
|
-
npm run build --if-present
|
|
540
|
-
|
|
541
|
-
- name: Run tests with Knapsack Pro
|
|
542
|
-
env:
|
|
543
|
-
KNAPSACK_PRO_TEST_SUITE_TOKEN_JEST: ${{ secrets.KNAPSACK_PRO_TEST_SUITE_TOKEN_JEST }}
|
|
544
|
-
KNAPSACK_PRO_CI_NODE_TOTAL: ${{ matrix.ci_node_total }}
|
|
545
|
-
KNAPSACK_PRO_CI_NODE_INDEX: ${{ matrix.ci_node_index }}
|
|
546
|
-
# allows rerun parallel jobs with the same set of tests
|
|
547
|
-
# that were consumed from Queue in the very first CI build run
|
|
548
|
-
KNAPSACK_PRO_FIXED_QUEUE_SPLIT: true
|
|
549
|
-
run: |
|
|
550
|
-
$(npm bin)/knapsack-pro-jest
|
|
551
|
-
```
|
|
552
|
-
|
|
553
|
-
#### Codefresh.io
|
|
554
|
-
|
|
555
|
-
`@knapsack-pro/jest` supports environment variables provided by Codefresh.io to run your tests. You have to define a few things in `.codefresh/codefresh.yml` config file.
|
|
556
|
-
|
|
557
|
-
- You need to set an API token `KNAPSACK_PRO_TEST_SUITE_TOKEN_JEST` in Codefresh dashboard, see left menu Pipelines -> settings (cog icon next to the pipeline) -> Variables tab (see a vertical menu on the right side).
|
|
558
|
-
- Set where Codefresh YAML file can be found. In Codefresh dashboard, see left menu Pipelines -> settings (cog icon next to pipeline) -> Workflow tab (horizontal menu on the top) -> Path to YAML (set there `./.codefresh/codefresh.yml`).
|
|
559
|
-
- Set how many parallel jobs (parallel CI nodes) you want to run with `KNAPSACK_PRO_CI_NODE_TOTAL` environment variable in `.codefresh/codefresh.yml` file.
|
|
560
|
-
- Ensure in the `matrix` section you listed all `KNAPSACK_PRO_CI_NODE_INDEX` environment variables with a value from `0` to `KNAPSACK_PRO_CI_NODE_TOTAL-1`. Codefresh will generate a matrix of parallel jobs where each job has a different value for `KNAPSACK_PRO_CI_NODE_INDEX`. Thanks to that Knapsack Pro knows what tests should be run on each parallel job.
|
|
561
|
-
|
|
562
|
-
Below you can find Codefresh YAML config and `Test.Dockerfile` used by Codefresh to run the project and Jest test suite inside of Docker container.
|
|
563
|
-
|
|
564
|
-
```yaml
|
|
565
|
-
# .codefresh/codefresh.yml
|
|
566
|
-
version: '1.0'
|
|
567
|
-
|
|
568
|
-
stages:
|
|
569
|
-
- 'clone'
|
|
570
|
-
- 'build'
|
|
571
|
-
- 'tests'
|
|
572
|
-
|
|
573
|
-
steps:
|
|
574
|
-
main_clone:
|
|
575
|
-
type: 'git-clone'
|
|
576
|
-
description: 'Cloning main repository...'
|
|
577
|
-
repo: '${{CF_REPO_OWNER}}/${{CF_REPO_NAME}}'
|
|
578
|
-
revision: '${{CF_BRANCH}}'
|
|
579
|
-
stage: 'clone'
|
|
580
|
-
BuildTestDockerImage:
|
|
581
|
-
title: Building Test Docker image
|
|
582
|
-
type: build
|
|
583
|
-
arguments:
|
|
584
|
-
image_name: '${{CF_ACCOUNT}}/${{CF_REPO_NAME}}-test'
|
|
585
|
-
tag: '${{CF_BRANCH_TAG_NORMALIZED}}-${{CF_SHORT_REVISION}}'
|
|
586
|
-
dockerfile: Test.Dockerfile
|
|
587
|
-
stage: 'build'
|
|
588
|
-
|
|
589
|
-
run_tests:
|
|
590
|
-
stage: 'tests'
|
|
591
|
-
image: '${{BuildTestDockerImage}}'
|
|
592
|
-
working_directory: /src
|
|
593
|
-
fail_fast: false
|
|
594
|
-
environment:
|
|
595
|
-
# set how many parallel jobs you want to run
|
|
596
|
-
- KNAPSACK_PRO_CI_NODE_TOTAL=2
|
|
597
|
-
matrix:
|
|
598
|
-
environment:
|
|
599
|
-
# please ensure you have here listed N-1 indexes
|
|
600
|
-
# where N is KNAPSACK_PRO_CI_NODE_TOTAL
|
|
601
|
-
- KNAPSACK_PRO_CI_NODE_INDEX=0
|
|
602
|
-
- KNAPSACK_PRO_CI_NODE_INDEX=1
|
|
603
|
-
commands:
|
|
604
|
-
- $(npm bin)/knapsack-pro-jest
|
|
605
|
-
```
|
|
606
|
-
|
|
607
|
-
Add `Test.Dockerfile` to your project repository.
|
|
608
|
-
|
|
609
|
-
```Dockerfile
|
|
610
|
-
FROM node:10.13
|
|
611
|
-
|
|
612
|
-
RUN apt-get update && \
|
|
613
|
-
apt-get install -y \
|
|
614
|
-
python3-dev \
|
|
615
|
-
python3-pip
|
|
616
|
-
|
|
617
|
-
# Install AWS CLI
|
|
618
|
-
RUN pip3 install awscli
|
|
619
|
-
|
|
620
|
-
# Install Codefresh CLI
|
|
621
|
-
RUN wget https://github.com/codefresh-io/cli/releases/download/v0.31.1/codefresh-v0.31.1-alpine-x64.tar.gz
|
|
622
|
-
RUN tar -xf codefresh-v0.31.1-alpine-x64.tar.gz -C /usr/local/bin/
|
|
623
|
-
|
|
624
|
-
COPY . /src
|
|
625
|
-
|
|
626
|
-
WORKDIR /src
|
|
627
|
-
|
|
628
|
-
RUN npm install
|
|
629
|
-
```
|
|
630
|
-
|
|
631
|
-
#### Other CI provider
|
|
632
|
-
|
|
633
|
-
You have to define `KNAPSACK_PRO_CI_NODE_TOTAL` and `KNAPSACK_PRO_CI_NODE_INDEX` for each parallel job running as part of the same CI build.
|
|
634
|
-
|
|
635
|
-
```
|
|
636
|
-
# Step for first CI node
|
|
637
|
-
KNAPSACK_PRO_CI_NODE_TOTAL=2 KNAPSACK_PRO_CI_NODE_INDEX=0 $(npm bin)/knapsack-pro-jest
|
|
638
|
-
|
|
639
|
-
# Step for second CI node
|
|
640
|
-
KNAPSACK_PRO_CI_NODE_TOTAL=2 KNAPSACK_PRO_CI_NODE_INDEX=1 $(npm bin)/knapsack-pro-jest
|
|
641
|
-
```
|
|
642
|
-
|
|
643
|
-
Please remember to set up API token `KNAPSACK_PRO_TEST_SUITE_TOKEN_JEST` as global environment variable.
|
|
29
|
+
See the [docs](https://docs.knapsackpro.com/jest/guide/) to get started.
|
|
644
30
|
|
|
645
31
|
## FAQ
|
|
646
32
|
|
|
@@ -654,7 +40,7 @@ Please remember to set up API token `KNAPSACK_PRO_TEST_SUITE_TOKEN_JEST` as glob
|
|
|
654
40
|
|
|
655
41
|
You can use [NVM](https://github.com/nvm-sh/nvm) to manage Node version in development.
|
|
656
42
|
|
|
657
|
-
- `>= Node
|
|
43
|
+
- `>= Node 18.13.0 LTS`
|
|
658
44
|
|
|
659
45
|
### Dependencies
|
|
660
46
|
|
|
@@ -805,4 +191,4 @@ If your feature requires code change in [@knapsack-pro/core](https://github.com/
|
|
|
805
191
|
To test `@knapsack-pro/jest` against real test suite we use:
|
|
806
192
|
|
|
807
193
|
- [jest-example-test-suite](https://github.com/KnapsackPro/jest-example-test-suite) project.
|
|
808
|
-
- [react](https://github.com/KnapsackPro/react)
|
|
194
|
+
- (deprecated) [react](https://github.com/KnapsackPro/react)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@knapsack-pro/jest",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.4.0",
|
|
4
4
|
"description": "Knapsack Pro Jest splits Jest tests across CI nodes and makes sure that tests will run in optimal time on each CI node.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"knapsack",
|
|
@@ -53,7 +53,7 @@
|
|
|
53
53
|
"knapsack-pro-jest": "lib/knapsack-pro-jest.js"
|
|
54
54
|
},
|
|
55
55
|
"dependencies": {
|
|
56
|
-
"@knapsack-pro/core": "^3.
|
|
56
|
+
"@knapsack-pro/core": "^3.3.1",
|
|
57
57
|
"glob": "^8.0.3",
|
|
58
58
|
"minimatch": "^5.1.0",
|
|
59
59
|
"minimist": "^1.2.6",
|