-
Notifications
You must be signed in to change notification settings - Fork 5
Add support for create-react-app using Typescript #48
Changes from 9 commits
b3b3dd3
0dd9e7a
2218750
4bc7a57
5c4f025
18f6706
1a41d33
2a9dc13
4203d7e
d175aae
a6ae1cc
bc3b01d
394d4f3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ import { Config, ConfigEditor } from 'stryker-api/config'; | |
import JestConfigLoader from './configLoaders/JestConfigLoader'; | ||
import DefaultJestConfigLoader from './configLoaders/DefaultJestConfigLoader'; | ||
import ReactScriptsJestConfigLoader from './configLoaders/ReactScriptsJestConfigLoader'; | ||
import ReactScriptsTsJestConfigLoader from './configLoaders/ReactScriptsTsJestConfigLoader'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know, and to be honest, I don't really care either. If you prefer |
||
import JestConfiguration from './configLoaders/JestConfiguration'; | ||
import JEST_OVERRIDE_OPTIONS from './jestOverrideOptions'; | ||
|
||
|
@@ -29,10 +30,13 @@ export default class JestConfigEditor implements ConfigEditor { | |
switch (project.toLowerCase()) { | ||
case DEFAULT_PROJECT_NAME: | ||
configLoader = new DefaultJestConfigLoader(process.cwd(), fs); | ||
break; | ||
break; | ||
case 'react': | ||
configLoader = new ReactScriptsJestConfigLoader(process.cwd()); | ||
break; | ||
break; | ||
case 'react-ts': | ||
configLoader = new ReactScriptsTsJestConfigLoader(process.cwd()); | ||
break; | ||
default: | ||
throw new Error(`No configLoader available for ${project}`); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import JestConfigLoader from './JestConfigLoader'; | ||
import { createReactTsJestConfig } from '../utils/createReactJestConfig'; | ||
import * as path from 'path'; | ||
import JestConfiguration from './JestConfiguration'; | ||
|
||
export default class ReactScriptsTsJestConfigLoader implements JestConfigLoader { | ||
private loader: NodeRequire; | ||
private projectRoot: string; | ||
|
||
public constructor(projectRoot: string, loader?: NodeRequire) { | ||
this.loader = loader || /* istanbul ignore next */ require; | ||
this.projectRoot = projectRoot; | ||
} | ||
|
||
public loadConfig(): JestConfiguration { | ||
// Get the location of react-ts script, this is later used to generate the Jest configuration used for React projects. | ||
const reactScriptsTsLocation = path.join(this.loader.resolve('react-scripts-ts/package.json'), '..'); | ||
|
||
// Create the React configuration for Jest | ||
const jestConfiguration = this.createJestConfig(reactScriptsTsLocation); | ||
|
||
// Set test environment to jsdom (otherwise Jest won't run) | ||
jestConfiguration.testEnvironment = 'jsdom'; | ||
|
||
return jestConfiguration; | ||
} | ||
|
||
private createJestConfig(reactScriptsTsLocation: string): any { | ||
return createReactTsJestConfig( | ||
(relativePath: string): string => path.join(reactScriptsTsLocation, relativePath), | ||
this.projectRoot, | ||
false | ||
); | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,20 @@ | ||
import { getLogger } from 'log4js'; | ||
|
||
import JestTestAdapter from './JestTestAdapter'; | ||
import JestPromiseAdapter from './JestPromiseTestAdapter'; | ||
import JestCallbackAdapter from './JestCallbackTestAdapter'; | ||
import * as semver from 'semver'; | ||
|
||
export default class JestTestAdapterFactory { | ||
private static log = getLogger(JestTestAdapterFactory.name); | ||
|
||
public static getJestTestAdapter(loader?: NodeRequire): JestTestAdapter { | ||
const jestVersion = this.getJestVersion(loader || /* istanbul ignore next */ require); | ||
|
||
if (semver.satisfies(jestVersion, '<20.0.0')) { | ||
throw new Error('You need Jest version >= 20.0.0 to use Stryker'); | ||
} else if (semver.satisfies(jestVersion, '>=20.0.0 <21.0.0')) { | ||
return new JestCallbackAdapter(); | ||
if (semver.satisfies(jestVersion, '<22.0.0')) { | ||
JestTestAdapterFactory.log.debug(`Detected Jest below 22.0.0`); | ||
throw new Error('You need Jest version >= 22.0.0 to use Stryker'); | ||
} else { | ||
JestTestAdapterFactory.log.debug(`Detected Jest between above 22.0.0`); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This log message is confusing to me. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a bit of a left-over from back when Jest 19 till 22 was supported. I'll be remove it. |
||
return new JestPromiseAdapter(); | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,13 @@ | ||
export default function createReactJestConfig(resolve: Function, projectRoot: string, ejected: boolean, loader?: NodeRequire): string { | ||
const resolveCreateJestConfig = (path: string, loader?: NodeRequire): Function => { | ||
loader = loader || /* istanbul ignore next */ require; | ||
|
||
return loader('react-scripts/scripts/utils/createJestConfig')(resolve, projectRoot, ejected); | ||
} | ||
return loader(path); | ||
}; | ||
|
||
export function createReactJestConfig(resolve: Function, projectRoot: string, ejected: boolean, loader?: NodeRequire): string { | ||
return resolveCreateJestConfig('react-scripts/scripts/utils/createJestConfig', loader)(resolve, projectRoot, ejected); | ||
} | ||
|
||
export function createReactTsJestConfig(resolve: Function, projectRoot: string, ejected: boolean, loader?: NodeRequire): string { | ||
return resolveCreateJestConfig('react-scripts-ts/scripts/utils/createJestConfig', loader)(resolve, projectRoot, ejected); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does ejected mean? Is it a React thing? Maybe we can add a link to where the concept is explained.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See the CRA documentation. This particular remark was already there since #46.
Basically, CRA allows you to "eject", which