|
| 1 | +import type { Config } from '@jest/types'; |
| 2 | +import type * as d from '@stencil/core/internal'; |
| 3 | +import { isString } from '@utils'; |
| 4 | + |
| 5 | +import { Jest29Stencil } from './jest-facade'; |
| 6 | + |
| 7 | +/** |
| 8 | + * Builds the `argv` to be used when programmatically invoking the Jest CLI |
| 9 | + * @param config the Stencil config to use while generating Jest CLI arguments |
| 10 | + * @returns the arguments to pass to the Jest CLI, wrapped in an object |
| 11 | + */ |
| 12 | +export function buildJestArgv(config: d.ValidatedConfig): Config.Argv { |
| 13 | + const yargs = require('yargs'); |
| 14 | + |
| 15 | + const knownArgs = config.flags.knownArgs.slice(); |
| 16 | + |
| 17 | + if (!knownArgs.some((a) => a.startsWith('--max-workers') || a.startsWith('--maxWorkers'))) { |
| 18 | + knownArgs.push(`--max-workers=${config.maxConcurrentWorkers}`); |
| 19 | + } |
| 20 | + |
| 21 | + if (config.flags.devtools) { |
| 22 | + knownArgs.push('--runInBand'); |
| 23 | + } |
| 24 | + |
| 25 | + // we combine the modified args and the unknown args here and declare the |
| 26 | + // result read only, providing some type system-level assurance that we won't |
| 27 | + // mutate it after this point. |
| 28 | + // |
| 29 | + // We want that assurance because Jest likes to have any filepath match |
| 30 | + // patterns at the end of the args it receives. Those args are going to be |
| 31 | + // found in our `unknownArgs`, so while we want to do some stuff in this |
| 32 | + // function that adds to `knownArgs` we need a guarantee that all of the |
| 33 | + // `unknownArgs` are _after_ all the `knownArgs` in the array we end up |
| 34 | + // generating the Jest configuration from. |
| 35 | + const args: ReadonlyArray<string> = [...knownArgs, ...config.flags.unknownArgs]; |
| 36 | + |
| 37 | + config.logger.info(config.logger.magenta(`jest args: ${args.join(' ')}`)); |
| 38 | + |
| 39 | + const jestArgv = yargs(args).argv as Config.Argv; |
| 40 | + jestArgv.config = buildJestConfig(config); |
| 41 | + |
| 42 | + if (typeof jestArgv.maxWorkers === 'string') { |
| 43 | + try { |
| 44 | + jestArgv.maxWorkers = parseInt(jestArgv.maxWorkers, 10); |
| 45 | + } catch (e) {} |
| 46 | + } |
| 47 | + |
| 48 | + if (typeof jestArgv.ci === 'string') { |
| 49 | + jestArgv.ci = jestArgv.ci === 'true' || jestArgv.ci === ''; |
| 50 | + } |
| 51 | + |
| 52 | + return jestArgv; |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * Generate a Jest run configuration to be used as a part of the `argv` passed to the Jest CLI when it is invoked |
| 57 | + * programmatically |
| 58 | + * @param config the Stencil config to use while generating Jest CLI arguments |
| 59 | + * @returns the Jest Config to attach to the `argv` argument |
| 60 | + */ |
| 61 | +export function buildJestConfig(config: d.ValidatedConfig): string { |
| 62 | + const stencilConfigTesting = config.testing; |
| 63 | + const jestDefaults: Config.DefaultOptions = require('jest-config').defaults; |
| 64 | + |
| 65 | + const validJestConfigKeys = Object.keys(jestDefaults); |
| 66 | + |
| 67 | + const jestConfig: d.JestConfig = {}; |
| 68 | + |
| 69 | + Object.keys(stencilConfigTesting).forEach((key) => { |
| 70 | + if (validJestConfigKeys.includes(key)) { |
| 71 | + (jestConfig as any)[key] = (stencilConfigTesting as any)[key]; |
| 72 | + } |
| 73 | + }); |
| 74 | + |
| 75 | + jestConfig.rootDir = config.rootDir; |
| 76 | + |
| 77 | + if (isString(stencilConfigTesting.collectCoverage)) { |
| 78 | + jestConfig.collectCoverage = stencilConfigTesting.collectCoverage; |
| 79 | + } |
| 80 | + if (Array.isArray(stencilConfigTesting.collectCoverageFrom)) { |
| 81 | + jestConfig.collectCoverageFrom = stencilConfigTesting.collectCoverageFrom; |
| 82 | + } |
| 83 | + if (isString(stencilConfigTesting.coverageDirectory)) { |
| 84 | + jestConfig.coverageDirectory = stencilConfigTesting.coverageDirectory; |
| 85 | + } |
| 86 | + if (stencilConfigTesting.coverageThreshold) { |
| 87 | + jestConfig.coverageThreshold = stencilConfigTesting.coverageThreshold; |
| 88 | + } |
| 89 | + if (isString(stencilConfigTesting.globalSetup)) { |
| 90 | + jestConfig.globalSetup = stencilConfigTesting.globalSetup; |
| 91 | + } |
| 92 | + if (isString(stencilConfigTesting.globalTeardown)) { |
| 93 | + jestConfig.globalTeardown = stencilConfigTesting.globalTeardown; |
| 94 | + } |
| 95 | + if (isString(stencilConfigTesting.preset)) { |
| 96 | + jestConfig.preset = stencilConfigTesting.preset; |
| 97 | + } |
| 98 | + if (stencilConfigTesting.projects) { |
| 99 | + jestConfig.projects = stencilConfigTesting.projects; |
| 100 | + } |
| 101 | + if (Array.isArray(stencilConfigTesting.reporters)) { |
| 102 | + jestConfig.reporters = stencilConfigTesting.reporters; |
| 103 | + } |
| 104 | + if (isString(stencilConfigTesting.testResultsProcessor)) { |
| 105 | + jestConfig.testResultsProcessor = stencilConfigTesting.testResultsProcessor; |
| 106 | + } |
| 107 | + if (stencilConfigTesting.transform) { |
| 108 | + jestConfig.transform = stencilConfigTesting.transform; |
| 109 | + } |
| 110 | + if (stencilConfigTesting.verbose) { |
| 111 | + jestConfig.verbose = stencilConfigTesting.verbose; |
| 112 | + } |
| 113 | + |
| 114 | + jestConfig.testRunner = new Jest29Stencil().getDefaultJestRunner(); |
| 115 | + |
| 116 | + return JSON.stringify(jestConfig); |
| 117 | +} |
| 118 | + |
| 119 | +export function getProjectListFromCLIArgs(config: d.ValidatedConfig, argv: Config.Argv): string[] { |
| 120 | + const projects = argv.projects ? argv.projects : []; |
| 121 | + |
| 122 | + projects.push(config.rootDir); |
| 123 | + |
| 124 | + return projects; |
| 125 | +} |
0 commit comments