Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to Jest 24 #6278

Merged
merged 16 commits into from Mar 15, 2019
Merged
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -25,7 +25,7 @@
"get-port": "^4.2.0",
"globby": "^9.1.0",
"husky": "^1.3.1",
"jest": "^23.6.0",
"jest": "^24.5.0",
"lerna": "2.9.1",
"lerna-changelog": "~0.8.2",
"lint-staged": "^8.0.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-plugin-named-asset-import/package.json
Expand Up @@ -16,7 +16,7 @@
},
"devDependencies": {
"babel-plugin-tester": "^5.5.1",
"jest": "^23.6.0"
"jest": "^24.5.0"
},
"scripts": {
"test": "jest"
Expand Down
2 changes: 1 addition & 1 deletion packages/confusing-browser-globals/package.json
Expand Up @@ -16,6 +16,6 @@
"index.js"
],
"devDependencies": {
"jest": "23.6.0"
"jest": "24.5.0"
}
}
2 changes: 1 addition & 1 deletion packages/react-dev-utils/package.json
Expand Up @@ -74,7 +74,7 @@
},
"devDependencies": {
"cross-env": "^5.2.0",
"jest": "^23.6.0"
"jest": "^24.5.0"
},
"scripts": {
"test": "cross-env FORCE_COLOR=true jest"
Expand Down
6 changes: 3 additions & 3 deletions packages/react-error-overlay/package.json
Expand Up @@ -35,7 +35,7 @@
"anser": "1.4.8",
"babel-core": "7.0.0-bridge.0",
"babel-eslint": "10.0.1",
"babel-jest": "23.6.0",
"babel-jest": "24.5.0",
"babel-loader": "8.0.5",
"babel-preset-react-app": "^7.0.2",
"chalk": "^2.4.2",
Expand All @@ -49,8 +49,8 @@
"eslint-plugin-react": "7.12.4",
"flow-bin": "^0.63.1",
"html-entities": "1.2.1",
"jest": "23.6.0",
"jest-fetch-mock": "1.6.6",
"jest": "24.5.0",
"jest-fetch-mock": "2.1.1",
"object-assign": "4.1.1",
"promise": "8.0.2",
"raw-loader": "^1.0.0",
Expand Down
@@ -1,7 +1,7 @@
{
"dependencies": {
"bootstrap": "4.1.1",
"jest": "23.6.0",
"jest": "24.5.0",
"node-sass": "4.8.3",
"normalize.css": "7.0.0",
"prop-types": "15.5.6",
Expand Down
7 changes: 3 additions & 4 deletions packages/react-scripts/package.json
Expand Up @@ -28,7 +28,7 @@
"@svgr/webpack": "4.1.0",
"babel-core": "7.0.0-bridge.0",
"babel-eslint": "10.0.1",
"babel-jest": "23.6.0",
"babel-jest": "24.5.0",
"babel-loader": "8.0.5",
"babel-plugin-named-asset-import": "^0.3.1",
"babel-preset-react-app": "^7.0.2",
Expand All @@ -48,9 +48,8 @@
"fs-extra": "7.0.1",
"html-webpack-plugin": "4.0.0-beta.5",
"identity-obj-proxy": "3.0.0",
"jest": "23.6.0",
"jest-pnp-resolver": "1.0.2",
"jest-resolve": "23.6.0",
"jest": "24.5.0",
"jest-resolve": "24.5.0",
"jest-watch-typeahead": "^0.2.1",
"mini-css-extract-plugin": "0.5.0",
"optimize-css-assets-webpack-plugin": "5.0.1",
Expand Down
14 changes: 4 additions & 10 deletions packages/react-scripts/scripts/utils/createJestConfig.js
Expand Up @@ -24,20 +24,13 @@ module.exports = (resolve, rootDir, isEjecting) => {
const config = {
collectCoverageFrom: ['src/**/*.{js,jsx,ts,tsx}', '!src/**/*.d.ts'],

// TODO: this breaks Yarn PnP on eject.
// But we can't simply emit this because it'll be an absolute path.
// The proper fix is to write jest.config.js on eject instead of a package.json key.
// Then these can always stay as require.resolve()s.
resolver: isEjecting
? 'jest-pnp-resolver'
: require.resolve('jest-pnp-resolver'),
setupFiles: [
isEjecting
? 'react-app-polyfill/jsdom'
: require.resolve('react-app-polyfill/jsdom'),
],

setupTestFrameworkScriptFile: setupTestsFile,
setupFilesAfterEnv: setupTestsFile ? [setupTestsFile] : [],

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe rather than this here you modify the assignment of setupTestsFile above to potentially allow multiple files to be assigned:

function setupTestsFiles(rootDir) {
    const setupTestsFiles = [];
    if(fs.existsSync(paths.testsSetup)){
      const setupTestsMatches = paths.testsSetup.match(/src[/\\]setupTests\.(.+)/);
      const setupTestsFileExtension = (setupTestsMatches && setupTestsMatches[1]) || 'js';
      setupTestsFiles.push(`${rootDir}/src/setupTests.${setupTestsFileExtension}`)
    }
    return setupTestsFiles
}

...
module.exports = (resolve, rootDir, isEjecting) => {
...

setupFilesAfterEnv: setupTestsFiles(rootDir),

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can totally do this, although i think that most users would use setupFilesAfterEnv like:

setupFilesAfterEnv: [
  'jest-dom/extend-expect',
  'react-testing-library/cleanup-after-each'
]

rather than creating multiple file themselves.

IMO we should allow overriding setupFilesAfterEnv in package.json if we really want to support this.

testMatch: [
'<rootDir>/src/**/__tests__/**/*.{js,jsx,ts,tsx}',
'<rootDir>/src/**/?(*.)(spec|test).{js,jsx,ts,tsx}',
Expand Down Expand Up @@ -77,6 +70,7 @@ module.exports = (resolve, rootDir, isEjecting) => {
'collectCoverageFrom',
'coverageReporters',
'coverageThreshold',
'extraGlobals',
'globalSetup',
'globalTeardown',
'resetMocks',
Expand All @@ -94,13 +88,13 @@ module.exports = (resolve, rootDir, isEjecting) => {
const unsupportedKeys = Object.keys(overrides);
if (unsupportedKeys.length) {
const isOverridingSetupFile =
unsupportedKeys.indexOf('setupTestFrameworkScriptFile') > -1;
unsupportedKeys.indexOf('setupFilesAfterEnv') > -1;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey @loryman

there is list of supported keys

https://github.com/facebook/create-react-app/blob/537c115217cccd70e451a8268c3762df82d0c4b6/packages/react-scripts/scripts/utils/createJestConfig.js#L76-L86

Can I ask you to add extraGlobals key there?

It drastically improves performance of jest image snapshot test

jest docs: https://jestjs.io/docs/en/configuration#extraglobals-array-string

Isssues: jestjs/jest#5163

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iansu Any thoughts on this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's probably okay to add that to the supported keys.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!


if (isOverridingSetupFile) {
console.error(
chalk.red(
'We detected ' +
chalk.bold('setupTestFrameworkScriptFile') +
chalk.bold('setupFilesAfterEnv') +
' in your package.json.\n\n' +
'Remove it from Jest configuration, and put the initialization code in ' +
chalk.bold('src/setupTests.js') +
Expand Down
16 changes: 8 additions & 8 deletions tasks/e2e-installs.sh
Expand Up @@ -120,7 +120,7 @@ npx create-react-app --version
# ******************************************************************************

cd "$temp_app_path"
npx create-react-app --scripts-version=@latest test-app-dist-tag
npx create-react-app test-app-dist-tag --scripts-version=@latest
cd test-app-dist-tag

# Check corresponding scripts version is installed and no TypeScript is present.
Expand All @@ -135,7 +135,7 @@ checkDependencies
# ******************************************************************************

cd "$temp_app_path"
npx create-react-app --scripts-version=1.0.17 test-app-version-number
npx create-react-app test-app-version-number --scripts-version=1.0.17
cd test-app-version-number

# Check corresponding scripts version is installed.
Expand All @@ -148,7 +148,7 @@ checkDependencies
# ******************************************************************************

cd "$temp_app_path"
npx create-react-app --use-npm --scripts-version=1.0.17 test-use-npm-flag
npx create-react-app test-use-npm-flag --use-npm --scripts-version=1.0.17
cd test-use-npm-flag

# Check corresponding scripts version is installed.
Expand Down Expand Up @@ -196,7 +196,7 @@ CI=true yarn test
# ******************************************************************************

cd "$temp_app_path"
npx create-react-app --scripts-version=https://registry.npmjs.org/react-scripts/-/react-scripts-1.0.17.tgz test-app-tarball-url
npx create-react-app test-app-tarball-url --scripts-version=https://registry.npmjs.org/react-scripts/-/react-scripts-1.0.17.tgz
cd test-app-tarball-url

# Check corresponding scripts version is installed.
Expand All @@ -209,7 +209,7 @@ checkDependencies
# ******************************************************************************

cd "$temp_app_path"
npx create-react-app --scripts-version=react-scripts-fork test-app-fork
npx create-react-app test-app-fork --scripts-version=react-scripts-fork
cd test-app-fork

# Check corresponding scripts version is installed.
Expand All @@ -221,7 +221,7 @@ exists node_modules/react-scripts-fork

cd "$temp_app_path"
# we will install a non-existing package to simulate a failed installataion.
npx create-react-app --scripts-version=`date +%s` test-app-should-not-exist || true
npx create-react-app test-app-should-not-exist --scripts-version=`date +%s` || true
# confirm that the project files were deleted
test ! -e test-app-should-not-exist/package.json
test ! -d test-app-should-not-exist/node_modules
Expand All @@ -234,7 +234,7 @@ cd "$temp_app_path"
mkdir test-app-should-remain
echo '## Hello' > ./test-app-should-remain/README.md
# we will install a non-existing package to simulate a failed installataion.
npx create-react-app --scripts-version=`date +%s` test-app-should-remain || true
npx create-react-app test-app-should-remain --scripts-version=`date +%s` || true
# confirm the file exist
test -e test-app-should-remain/README.md
# confirm only README.md and error log are the only files in the directory
Expand All @@ -248,7 +248,7 @@ fi

cd $temp_app_path
curl "https://registry.npmjs.org/@enoah_netzach/react-scripts/-/react-scripts-0.9.0.tgz" -o enoah-scripts-0.9.0.tgz
npx create-react-app --scripts-version=$temp_app_path/enoah-scripts-0.9.0.tgz test-app-scoped-fork-tgz
npx create-react-app test-app-scoped-fork-tgz --scripts-version=$temp_app_path/enoah-scripts-0.9.0.tgz
cd test-app-scoped-fork-tgz

# Check corresponding scripts version is installed.
Expand Down
2 changes: 1 addition & 1 deletion tasks/e2e-kitchensink-eject.sh
Expand Up @@ -100,7 +100,7 @@ git clean -df

# Install the app in a temporary location
cd $temp_app_path
npx create-react-app --internal-testing-template="$root_path"/packages/react-scripts/fixtures/kitchensink test-kitchensink
npx create-react-app test-kitchensink --internal-testing-template="$root_path"/packages/react-scripts/fixtures/kitchensink

# Install the test module
cd "$temp_module_path"
Expand Down
2 changes: 1 addition & 1 deletion tasks/e2e-kitchensink.sh
Expand Up @@ -100,7 +100,7 @@ git clean -df

# Install the app in a temporary location
cd $temp_app_path
npx create-react-app --internal-testing-template="$root_path"/packages/react-scripts/fixtures/kitchensink test-kitchensink
npx create-react-app test-kitchensink --internal-testing-template="$root_path"/packages/react-scripts/fixtures/kitchensink

# Install the test module
cd "$temp_module_path"
Expand Down