import proxyquire from 'proxyquire' import createJestConfig from 'react-scripts/scripts/utils/createJestConfig' import { getThisModulePath } from '../utils/getThisModulePath' import { getProcessArg } from '../utils/processArg/getProcessArg' import { isProcessArg } from '../utils/processArg/isProcessArg' import { removeProcessArg } from '../utils/processArg/removeProcessArg' export default (processArg: any[]) => { const sourceFolder = getProcessArg(processArg, 'src', 'src') const withLatestReactEnzyme = isProcessArg(processArg, 'withLatestReactEnzyme') let setupFile = getProcessArg(processArg, 'setupTestFile', undefined) removeProcessArg(processArg, 'src') removeProcessArg(processArg, 'setupTestFile') removeProcessArg(processArg, 'withLatestReactEnzyme', false) // since "test" is used as a param by this script and misinterpreted by jest we remove "test" param here processArg.splice(2, 1) if (withLatestReactEnzyme) { setupFile = getThisModulePath('dist/defaultConfigurations/setupTestFile.js') } proxyquire('react-scripts/scripts/test.js', { // When test.js asks for '../utils/createJestConfig' it will get this instead: './utils/createJestConfig': (...args: any[]) => { const defaultJestConfig: jest.ProjectConfig & any = createJestConfig(...args) defaultJestConfig.moduleFileExtensions.push('tsx', 'ts') defaultJestConfig.collectCoverageFrom = [sourceFolder + '/**/*.{js,jsx,mjs,ts,tsx}'] defaultJestConfig.testMatch = ['/' + sourceFolder + '/**/?(*.)(spec|test).{js,jsx,mjs,ts,tsx}'] defaultJestConfig.transform = { '.+\\.(css|styl|less|sass|scss)$': require.resolve('jest-css-modules-transform'), '^.+\\.(js|jsx)$': require.resolve('babel-jest'), '^.+\\.(ts|tsx)$': require.resolve('ts-jest'), } defaultJestConfig.globals = { ...defaultJestConfig.globals ? defaultJestConfig.globals : {}, 'ts-jest': { skipBabel: true, }, } const tsConfig = require(defaultJestConfig.rootDir + '/tsconfig') if (tsConfig.compilerOptions.baseUrl && tsConfig.compilerOptions.paths) { const moduleNameMapper = Object.keys(tsConfig.compilerOptions.paths).reduce((current, onePath) => { const jestRegex = '^' + onePath.replace('*', '(.*)$') const jestSource = tsConfig.compilerOptions.paths[onePath][0] const jestResolve = '/' + tsConfig.compilerOptions.baseUrl + '/' + jestSource.replace('*', '$1') current[jestRegex] = jestResolve return current }, {}) defaultJestConfig.moduleNameMapper = { ...defaultJestConfig.moduleNameMapper ? defaultJestConfig.moduleNameMapper : {}, ...moduleNameMapper, } } defaultJestConfig.testEnvironment = 'jsdom' if (setupFile) { defaultJestConfig.setupTestFrameworkScriptFile = '/' + setupFile } return defaultJestConfig }, }) }