Created
March 3, 2025 12:17
-
-
Save deskoh/2452ec3be9dbf5c5766dbc41ebc01aa9 to your computer and use it in GitHub Desktop.
Create React App Jest Config
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"jest": { | |
"roots": [ | |
"<rootDir>/src" | |
], | |
"collectCoverageFrom": [ | |
"src/**/*.{js,jsx,ts,tsx}", | |
"!src/**/*.d.ts" | |
], | |
"setupFiles": [ | |
"react-app-polyfill/jsdom" | |
], | |
"setupFilesAfterEnv": [ | |
"<rootDir>/src/setupTests.ts" | |
], | |
"testMatch": [ | |
"<rootDir>/src/**/__tests__/**/*.{js,jsx,ts,tsx}", | |
"<rootDir>/src/**/*.{spec,test}.{js,jsx,ts,tsx}" | |
], | |
"testEnvironment": "jsdom", | |
"transform": { | |
"^.+\\.(js|jsx|mjs|cjs|ts|tsx)$": "<rootDir>/config/jest/babelTransform.js", | |
"^.+\\.css$": "<rootDir>/config/jest/cssTransform.js", | |
"^(?!.*\\.(js|jsx|mjs|cjs|ts|tsx|css|json)$)": "<rootDir>/config/jest/fileTransform.js" | |
}, | |
"transformIgnorePatterns": [ | |
"[/\\\\]node_modules[/\\\\].+\\.(js|jsx|mjs|cjs|ts|tsx)$", | |
"^.+\\.module\\.(css|sass|scss)$" | |
], | |
"modulePaths": [], | |
"moduleNameMapper": { | |
"^react-native$": "react-native-web", | |
"^.+\\.module\\.(css|sass|scss)$": "identity-obj-proxy" | |
}, | |
"moduleFileExtensions": [ | |
"web.js", | |
"js", | |
"web.ts", | |
"ts", | |
"web.tsx", | |
"tsx", | |
"json", | |
"web.jsx", | |
"jsx", | |
"node" | |
], | |
"watchPlugins": [ | |
"jest-watch-typeahead/filename", | |
"jest-watch-typeahead/testname" | |
], | |
"resetMocks": true | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <reference types="node" /> | |
/// <reference types="react" /> | |
/// <reference types="react-dom" /> | |
declare namespace NodeJS { | |
interface ProcessEnv { | |
readonly NODE_ENV: 'development' | 'production' | 'test'; | |
readonly PUBLIC_URL: string; | |
} | |
} | |
declare module '*.avif' { | |
const src: string; | |
export default src; | |
} | |
declare module '*.bmp' { | |
const src: string; | |
export default src; | |
} | |
declare module '*.gif' { | |
const src: string; | |
export default src; | |
} | |
declare module '*.jpg' { | |
const src: string; | |
export default src; | |
} | |
declare module '*.jpeg' { | |
const src: string; | |
export default src; | |
} | |
declare module '*.png' { | |
const src: string; | |
export default src; | |
} | |
declare module '*.webp' { | |
const src: string; | |
export default src; | |
} | |
declare module '*.svg' { | |
import * as React from 'react'; | |
export const ReactComponent: React.FunctionComponent<React.SVGProps< | |
SVGSVGElement | |
> & { title?: string }>; | |
const src: string; | |
export default src; | |
} | |
declare module '*.module.css' { | |
const classes: { readonly [key: string]: string }; | |
export default classes; | |
} | |
declare module '*.module.scss' { | |
const classes: { readonly [key: string]: string }; | |
export default classes; | |
} | |
declare module '*.module.sass' { | |
const classes: { readonly [key: string]: string }; | |
export default classes; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// jest-dom adds custom jest matchers for asserting on DOM nodes. | |
// allows you to do things like: | |
// expect(element).toHaveTextContent(/react/i) | |
// learn more: https://github.com/testing-library/jest-dom | |
import '@testing-library/jest-dom'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
// Do this as the first thing so that any code reading it knows the right env. | |
process.env.BABEL_ENV = 'test'; | |
process.env.NODE_ENV = 'test'; | |
process.env.PUBLIC_URL = ''; | |
// Makes the script crash on unhandled rejections instead of silently | |
// ignoring them. In the future, promise rejections that are not handled will | |
// terminate the Node.js process with a non-zero exit code. | |
process.on('unhandledRejection', err => { | |
throw err; | |
}); | |
// Ensure environment variables are read. | |
require('../config/env'); | |
const jest = require('jest'); | |
const execSync = require('child_process').execSync; | |
let argv = process.argv.slice(2); | |
function isInGitRepository() { | |
try { | |
execSync('git rev-parse --is-inside-work-tree', { stdio: 'ignore' }); | |
return true; | |
} catch (e) { | |
return false; | |
} | |
} | |
function isInMercurialRepository() { | |
try { | |
execSync('hg --cwd . root', { stdio: 'ignore' }); | |
return true; | |
} catch (e) { | |
return false; | |
} | |
} | |
// Watch unless on CI or explicitly running all tests | |
if ( | |
!process.env.CI && | |
argv.indexOf('--watchAll') === -1 && | |
argv.indexOf('--watchAll=false') === -1 | |
) { | |
// https://github.com/facebook/create-react-app/issues/5210 | |
const hasSourceControl = isInGitRepository() || isInMercurialRepository(); | |
argv.push(hasSourceControl ? '--watch' : '--watchAll'); | |
} | |
jest.run(argv); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment