12
12
const fs = require ( 'fs' ) ;
13
13
const chalk = require ( 'chalk' ) ;
14
14
const paths = require ( '../../config/paths' ) ;
15
+ const { hasPlugin } = require ( 'react-dev-utils/plugins' ) ;
15
16
16
17
module . exports = ( resolve , rootDir , isEjecting ) => {
18
+ const hasTypeScript = hasPlugin ( 'typescript' ) ;
19
+
17
20
// Use this instead of `paths.testsSetup` to avoid putting
18
21
// an absolute filename into configuration after ejecting.
19
22
const setupTestsFile = fs . existsSync ( paths . testsSetup )
@@ -23,27 +26,54 @@ module.exports = (resolve, rootDir, isEjecting) => {
23
26
// TODO: I don't know if it's safe or not to just use / as path separator
24
27
// in Jest configs. We need help from somebody with Windows to determine this.
25
28
const config = {
26
- collectCoverageFrom : [ 'src/**/*.{js,jsx}' ] ,
29
+ collectCoverageFrom : [
30
+ 'src/**/*.{js,jsx}' ,
31
+ ...( hasTypeScript ? [ 'src/**/*.{ts,tsx}' ] : [ ] ) ,
32
+ ] ,
27
33
setupFiles : [ resolve ( 'config/polyfills.js' ) ] ,
28
34
setupTestFrameworkScriptFile : setupTestsFile ,
29
35
testMatch : [
30
36
'<rootDir>/src/**/__tests__/**/*.js?(x)' ,
31
37
'<rootDir>/src/**/?(*.)(spec|test).js?(x)' ,
38
+ ...( hasTypeScript
39
+ ? [
40
+ '<rootDir>/src/**/__tests__/**/*.ts?(x)' ,
41
+ '<rootDir>/src/**/?(*.)(spec|test).ts?(x)' ,
42
+ ]
43
+ : [ ] ) ,
32
44
] ,
33
45
testEnvironment : 'node' ,
34
46
testURL : 'http://localhost' ,
35
- transform : {
36
- '^.+\\.(js|jsx)$' : isEjecting
37
- ? '<rootDir>/node_modules/babel-jest'
38
- : resolve ( 'config/jest/babelTransform.js' ) ,
39
- '^.+\\.css$' : resolve ( 'config/jest/cssTransform.js' ) ,
40
- '^(?!.*\\.(js|jsx|css|json)$)' : resolve ( 'config/jest/fileTransform.js' ) ,
41
- } ,
42
- transformIgnorePatterns : [ '[/\\\\]node_modules[/\\\\].+\\.(js|jsx)$' ] ,
47
+ transform : Object . assign (
48
+ {
49
+ '^.+\\.(js|jsx)$' : isEjecting
50
+ ? '<rootDir>/node_modules/babel-jest'
51
+ : resolve ( 'config/jest/babelTransform.js' ) ,
52
+ '^.+\\.css$' : resolve ( 'config/jest/cssTransform.js' ) ,
53
+ } ,
54
+ hasTypeScript
55
+ ? { '^.+\\.(ts|tsx)$' : resolve ( 'config/jest/typescriptTransform.js' ) }
56
+ : { } ,
57
+ {
58
+ '^(?!.*\\.(js|jsx|css|json)$)' : resolve ( 'config/jest/fileTransform.js' ) ,
59
+ }
60
+ ) ,
61
+ transformIgnorePatterns : [
62
+ '[/\\\\]node_modules[/\\\\].+\\.(js|jsx)$' ,
63
+ ...( hasTypeScript ? [ '[/\\\\]node_modules[/\\\\].+\\.(ts|tsx)$' ] : [ ] ) ,
64
+ ] ,
43
65
moduleNameMapper : {
44
66
'^react-native$' : 'react-native-web' ,
45
67
} ,
46
- moduleFileExtensions : [ 'web.js' , 'js' , 'json' , 'web.jsx' , 'jsx' , 'node' ] ,
68
+ moduleFileExtensions : [
69
+ 'web.js' ,
70
+ 'js' ,
71
+ ...( hasTypeScript ? [ 'tsx' , 'ts' ] : [ ] ) ,
72
+ 'json' ,
73
+ 'web.jsx' ,
74
+ 'jsx' ,
75
+ 'node' ,
76
+ ] ,
47
77
} ;
48
78
if ( rootDir ) {
49
79
config . rootDir = rootDir ;
0 commit comments