Skip to content

Commit

Permalink
feat(jest): JEST_SHARDS support (experimental)
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillgroshkov committed May 20, 2022
1 parent 1098c6d commit bdcd4ab
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
11 changes: 11 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,17 @@ For manual tests:
pre-defined sequencer file that sorts all filenames alphabetically). Set `JEST_NO_ALPHABETIC` env
variable to disable it.

##### Shard support (experimental)

Jest 28 introduced [--shard](https://jestjs.io/docs/cli#--shard) feature.

Set `JEST_SHARDS` environment variable (e.g `export JEST_SHARDS=3`), so that your `yarn test*`
commands will automatically split your tests by N number of shards and execute them **one after
another** (serially, **not** in parallel). Might be helpful to avoid Jest's notorious memory leaks.

If you need to execute shards **in parallel**, you can follow e.g
[this instruction](https://medium.com/@mfreundlich1/speed-up-your-jest-tests-with-shards-776e9f02f637).

#### Lint commands

- `lint-all`: runs ESLint, Stylelint, Prettier, in the right order.
Expand Down
21 changes: 16 additions & 5 deletions src/util/jest.util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as fs from 'fs'
import { _uniq } from '@naturalcycles/js-lib'
import { _range, _uniq } from '@naturalcycles/js-lib'
import { dimGrey, white } from '@naturalcycles/nodejs-lib/dist/colors'
import { execWithArgs } from '@naturalcycles/nodejs-lib/dist/exec'
import { cfgDir } from '../cnst/paths.cnst'
Expand Down Expand Up @@ -48,7 +48,7 @@ export async function runJest(opt: RunJestOpt = {}): Promise<void> {
return
}

const { CI, TZ = 'UTC', APP_ENV, JEST_NO_ALPHABETIC, NODE_OPTIONS } = process.env
const { CI, TZ = 'UTC', APP_ENV, JEST_NO_ALPHABETIC, JEST_SHARDS, NODE_OPTIONS } = process.env
const { integration, manual, leaks } = opt
const processArgs = process.argv.slice(2)

Expand Down Expand Up @@ -125,7 +125,18 @@ export async function runJest(opt: RunJestOpt = {}): Promise<void> {
console.log(`${dimGrey('NODE_OPTIONS are not defined')}`)
}

await execWithArgs('jest', _uniq(args), {
env,
})
if (JEST_SHARDS) {
const totalShards = Number(JEST_SHARDS)
const shards = _range(1, totalShards + 1)

for await (const shard of shards) {
await execWithArgs('jest', _uniq([...args, `--shard=${shard}/${totalShards}`]), {
env,
})
}
} else {
await execWithArgs('jest', _uniq(args), {
env,
})
}
}

0 comments on commit bdcd4ab

Please sign in to comment.