Skip to content

Commit

Permalink
feat: Make CRA use jest.config.js + add babelCRA
Browse files Browse the repository at this point in the history
  • Loading branch information
nvh95 committed Apr 26, 2022
1 parent 22dbfbe commit ccdf71f
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 7 deletions.
48 changes: 48 additions & 0 deletions examples/create-react-app/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
module.exports = {
roots: ['<rootDir>/src'],
collectCoverageFrom: ['src/**/*.{js,jsx,ts,tsx}', '!src/**/*.d.ts'],
setupFiles: ['react-app-polyfill/jsdom'],
// Use `setupTests.js` if you use javascript
setupFilesAfterEnv: ['<rootDir>/src/setupTests.ts'],
testMatch: [
'<rootDir>/src/**/__tests__/**/*.{js,jsx,ts,tsx}',
'<rootDir>/src/**/*.{spec,test}.{js,jsx,ts,tsx}',
],
testEnvironment: 'jsdom',
testRunner: 'jest-circus/runner.js',
transform: {
'^.+\\.(js|jsx|mjs|cjs|ts|tsx)$': 'jest-preview/transforms/babelCRA',
// Update the regex to support css and sass
'^.+\\.(css|scss|sass)$': 'jest-preview/transforms/css',
'^(?!.*\\.(js|jsx|mjs|cjs|ts|tsx|css|json)$)':
'jest-preview/transforms/fileCRA',
},
transformIgnorePatterns: [
'[/\\\\]node_modules[/\\\\].+\\.(js|jsx|mjs|cjs|ts|tsx)$',
// Remove to support CSS Modules
// '^.+\\.module\\.(css|sass|scss)$',
],
modulePaths: [],
moduleNameMapper: {
'^react-native$': 'react-native-web',
// Remove to support CSS Modules
// '^.+\\.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,
};
8 changes: 1 addition & 7 deletions examples/create-react-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"test": "react-scripts test -- --config=jest.config.js",
"test:nc": "npm run test -- --no-cache",
"eject": "react-scripts eject",
"jest-preview": "jest-preview",
Expand All @@ -46,11 +46,5 @@
"devDependencies": {
"jest-preview": "file:../..",
"npm-run-all": "^4.1.5"
},
"jest": {
"transform": {
"^.+\\.css$": "jest-preview/transforms/css",
"^(?!.*\\.(js|jsx|mjs|cjs|ts|tsx|css|json)$)": "jest-preview/transforms/fileCRA"
}
}
}
2 changes: 2 additions & 0 deletions examples/create-react-app/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useState } from 'react';
import { ReactComponent as Logo } from './logo.svg';
import logo from './logo.svg';
import logo2 from './assets/images/logo.svg';
import styles from './style.module.css';

import './App.css';
import './assets/css/App.css';
Expand All @@ -15,6 +16,7 @@ function App() {
<img src={logo} className="App-logo" alt="logo" />
<img src={logo2} className="logo2" alt="logo2" />
<p>Create React App example</p>
<p className={styles.textOrange}>Styled by CSS Modules</p>
<button
data-testid="increase"
type="button"
Expand Down
3 changes: 3 additions & 0 deletions examples/create-react-app/src/style.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.textOrange {
color: orange;
}
4 changes: 4 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ export default defineConfig([
filePath: 'src/preconfigTransform/fileCRA.ts',
dir: 'transforms',
}),
makeBundle({
filePath: 'src/preconfigTransform/babelCRA.ts',
dir: 'transforms',
}),
]);
31 changes: 31 additions & 0 deletions src/preconfigTransform/babelCRA.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';
const babelJest = require('babel-jest').default || require('babel-jest');

const hasJsxRuntime = (() => {
if (process.env.DISABLE_NEW_JSX_TRANSFORM === 'true') {
return false;
}

try {
require.resolve('react/jsx-runtime');
return true;
} catch (e) {
return false;
}
})();

export default babelJest.createTransformer({
presets: [
[
require.resolve('babel-preset-react-app', {
// Find `babel-preset-react-app` in user's node_modules
paths: [process.cwd()],
}),
{
runtime: hasJsxRuntime ? 'automatic' : 'classic',
},
],
],
babelrc: false,
configFile: false,
});

0 comments on commit ccdf71f

Please sign in to comment.