|
1 | 1 | import { Logger } from '@stryker-mutator/api/logging';
|
2 | 2 | import { StrykerOptions } from '@stryker-mutator/api/core';
|
3 | 3 | import { commonTokens, Injector, tokens } from '@stryker-mutator/api/plugin';
|
4 |
| -import semver from 'semver'; |
| 4 | +import semver, { SemVer } from 'semver'; |
5 | 5 |
|
6 | 6 | import { JestPluginContext, pluginTokens } from '../plugin-di.js';
|
7 | 7 | import { JestWrapper } from '../utils/jest-wrapper.js';
|
8 | 8 |
|
9 | 9 | import { JestLessThan25TestAdapter } from './jest-less-than-25-adapter.js';
|
10 | 10 | import { JestGreaterThan25TestAdapter } from './jest-greater-than-25-adapter.js';
|
| 11 | +interface CoercedVersion { |
| 12 | + version: SemVer; |
| 13 | + raw: string; |
| 14 | +} |
| 15 | + |
| 16 | +function coerceVersion(version: string): CoercedVersion { |
| 17 | + return { version: semver.coerce(version)!, raw: version }; |
| 18 | +} |
11 | 19 |
|
12 | 20 | export function jestTestAdapterFactory(
|
13 | 21 | log: Logger,
|
14 | 22 | jestWrapper: JestWrapper,
|
15 | 23 | options: StrykerOptions,
|
16 | 24 | injector: Injector<JestPluginContext>,
|
17 | 25 | ): JestGreaterThan25TestAdapter | JestLessThan25TestAdapter {
|
18 |
| - const version = jestWrapper.getVersion(); |
19 |
| - log.debug('Detected Jest version %s', version); |
20 |
| - guardJestVersion(version, options, log); |
| 26 | + const coercedVersion = coerceVersion(jestWrapper.getVersion()); |
| 27 | + log.debug('Detected Jest version %s', coercedVersion.raw); |
| 28 | + guardJestVersion(coercedVersion, options, log); |
21 | 29 |
|
22 |
| - if (semver.satisfies(version, '<25.0.0')) { |
| 30 | + if (semver.satisfies(coercedVersion.version, '<25.0.0')) { |
23 | 31 | return injector.injectClass(JestLessThan25TestAdapter);
|
24 | 32 | } else {
|
25 | 33 | return injector.injectClass(JestGreaterThan25TestAdapter);
|
26 | 34 | }
|
27 | 35 | }
|
28 | 36 | jestTestAdapterFactory.inject = tokens(commonTokens.logger, pluginTokens.jestWrapper, commonTokens.options, commonTokens.injector);
|
29 | 37 |
|
30 |
| -function guardJestVersion(jest: string, options: StrykerOptions, log: Logger) { |
31 |
| - if (semver.satisfies(jest, '<22.0.0')) { |
32 |
| - throw new Error(`You need Jest version >= 22.0.0 to use the @stryker-mutator/jest-runner plugin, found ${jest}`); |
33 |
| - } else if (semver.satisfies(jest, '<24')) { |
| 38 | +function guardJestVersion({ version, raw }: CoercedVersion, options: StrykerOptions, log: Logger) { |
| 39 | + if (semver.satisfies(version, '<22.0.0')) { |
| 40 | + throw new Error(`You need Jest version >= 22.0.0 to use the @stryker-mutator/jest-runner plugin, found ${raw}`); |
| 41 | + } else if (semver.satisfies(version, '<24')) { |
34 | 42 | if (options.coverageAnalysis !== 'off') {
|
35 | 43 | throw new Error(
|
36 | 44 | `You need Jest version >= 24.0.0 to use the @stryker-mutator/jest-runner with "coverageAnalysis": "${options.coverageAnalysis}", you're currently using version 23.0.0. Please upgrade your jest version, or set "coverageAnalysis": "off".`,
|
37 | 45 | );
|
38 | 46 | }
|
39 | 47 | log.warn(
|
40 | 48 | '[DEPRECATED] Support for Jest version < 24 is deprecated and will be removed in the next major version of Stryker, please upgrade your jest version (your current version is %s).',
|
41 |
| - jest, |
| 49 | + raw, |
42 | 50 | );
|
43 | 51 | }
|
44 | 52 | }
|
0 commit comments