|
| 1 | +import crypto from 'node:crypto'; |
| 2 | +import os from 'node:os'; |
| 3 | +import { env } from 'node:process'; |
| 4 | +import v8 from 'node:v8'; |
| 5 | +import { minimatch } from 'minimatch'; |
| 6 | +import type { JestConfigWithTsJest } from 'ts-jest'; |
| 7 | + |
| 8 | +const ci = !!process.env.CI; |
| 9 | + |
| 10 | +type JestConfig = JestConfigWithTsJest & { |
| 11 | + // https://github.com/renovatebot/renovate/issues/17034 |
| 12 | + workerIdleMemoryLimit?: string; |
| 13 | +}; |
| 14 | + |
| 15 | +const cpus = os.cpus(); |
| 16 | +const mem = os.totalmem(); |
| 17 | +const stats = v8.getHeapStatistics(); |
| 18 | + |
| 19 | +/** |
| 20 | + * https://docs.github.com/en/actions/using-github-hosted-runners/about-github-hosted-runners#supported-runners-and-hardware-resources |
| 21 | + * Currently it seems the runner only have 4GB |
| 22 | + */ |
| 23 | +function jestGithubRunnerSpecs(): JestConfig { |
| 24 | + // if (os.platform() === 'darwin') { |
| 25 | + // return { |
| 26 | + // maxWorkers: 2, |
| 27 | + // workerIdleMemoryLimit: '4GB', |
| 28 | + // }; |
| 29 | + // } |
| 30 | + |
| 31 | + return { |
| 32 | + maxWorkers: cpus.length, |
| 33 | + workerIdleMemoryLimit: '1500MB', // '2GB', |
| 34 | + }; |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * Configuration for single test shard. |
| 39 | + */ |
| 40 | +interface ShardConfig { |
| 41 | + /** |
| 42 | + * Path patterns to match against the test file paths, of two types: |
| 43 | + * |
| 44 | + * 1. Particular file, e.g. `lib/util/git/index.spec.ts` |
| 45 | + * |
| 46 | + * - File pattern MUST end with `.spec.ts` |
| 47 | + * - This will only search for the particular test file |
| 48 | + * - It enables coverage for the `*.ts` file with the same name, |
| 49 | + * e.g. `lib/util/git/index.ts` |
| 50 | + * - You probably want to use directory pattern instead |
| 51 | + * |
| 52 | + * 2. Whole directory, e.g. `lib/modules/datasource` |
| 53 | + * |
| 54 | + * - This will search for all `*.spec.ts` files under the directory |
| 55 | + * - It enables coverage all `*.ts` files under the directory, |
| 56 | + * e.g. `lib/modules/datasource/foo/bar/baz.ts` |
| 57 | + */ |
| 58 | + matchPaths: string[]; |
| 59 | +} |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | +const config: JestConfig = { |
| 64 | + ...configureShardingOrFallbackTo({ |
| 65 | + coverageDirectory: './coverage', |
| 66 | + }), |
| 67 | + collectCoverageFrom: [ |
| 68 | + 'lib/**/*.{js,ts}', |
| 69 | + '!lib/**/*.{d,spec}.ts', |
| 70 | + '!lib/**/{__fixtures__,__mocks__,__testutil__,test}/**/*.{js,ts}', |
| 71 | + '!lib/**/types.ts', |
| 72 | + ], |
| 73 | + coveragePathIgnorePatterns: getCoverageIgnorePatterns(), |
| 74 | + cacheDirectory: '.cache/jest', |
| 75 | + collectCoverage: true, |
| 76 | + coverageReporters: ci |
| 77 | + ? ['lcovonly', 'json'] |
| 78 | + : ['html', 'text-summary', 'json'], |
| 79 | + transform: { |
| 80 | + '\\.ts$': [ |
| 81 | + 'ts-jest', |
| 82 | + { |
| 83 | + tsconfig: '<rootDir>/tsconfig.spec.json', |
| 84 | + diagnostics: false, |
| 85 | + isolatedModules: true, |
| 86 | + }, |
| 87 | + ], |
| 88 | + }, |
| 89 | + modulePathIgnorePatterns: [ |
| 90 | + '<rootDir>/dist/', |
| 91 | + '/__fixtures__/', |
| 92 | + '/__mocks__/', |
| 93 | + ], |
| 94 | + reporters: ci ? ['default', 'github-actions'] : ['default'], |
| 95 | + resetMocks: true, |
| 96 | + setupFilesAfterEnv: [ |
| 97 | + 'jest-extended/all', |
| 98 | + 'expect-more-jest', |
| 99 | + '<rootDir>/test/setup.ts', |
| 100 | + '<rootDir>/test/to-migrate.ts', |
| 101 | + ], |
| 102 | + snapshotSerializers: ['<rootDir>/test/newline-snapshot-serializer.ts'], |
| 103 | + testEnvironment: 'node', |
| 104 | + testRunner: 'jest-circus/runner', |
| 105 | + watchPathIgnorePatterns: ['<rootDir>/.cache/', '<rootDir>/coverage/'], |
| 106 | + // We can play with that value later for best dev experience |
| 107 | + workerIdleMemoryLimit: '500MB', |
| 108 | + // add github runner specific limits |
| 109 | + ...(ci && jestGithubRunnerSpecs()), |
| 110 | +}; |
| 111 | + |
| 112 | +export default config; |
| 113 | + |
| 114 | +type RunsOn = 'ubuntu-latest' | 'windows-latest' | 'macos-latest'; |
| 115 | + |
| 116 | +interface ShardGroup { |
| 117 | + /** |
| 118 | + * Input for `runs-on` field. |
| 119 | + */ |
| 120 | + os: RunsOn; |
| 121 | + |
| 122 | + /** |
| 123 | + * Controls whether coverage is collected for this shard group. |
| 124 | + */ |
| 125 | + coverage: boolean; |
| 126 | + |
| 127 | + /** |
| 128 | + * Input for `name` field. |
| 129 | + */ |
| 130 | + name: string; |
| 131 | + |
| 132 | + /** |
| 133 | + * Space-separated list of shard keys, it's |
| 134 | + * meant to be inserted into bash for-loop. |
| 135 | + */ |
| 136 | + shards: string; |
| 137 | + |
| 138 | + /** |
| 139 | + * It's meant to be used for Jest caching. |
| 140 | + */ |
| 141 | + 'cache-key': string; |
| 142 | + |
| 143 | + /** |
| 144 | + * It's used to set test runner timeout. |
| 145 | + */ |
| 146 | + 'runner-timeout-minutes': number; |
| 147 | + |
| 148 | + /** |
| 149 | + * It's used to set `--test-timeout` Jest CLI flag. |
| 150 | + */ |
| 151 | + 'test-timeout-milliseconds': number; |
| 152 | + |
| 153 | + /** |
| 154 | + * It's used as the name for coverage artifact. |
| 155 | + */ |
| 156 | + 'upload-artifact-name': string; |
| 157 | +} |
0 commit comments