diff --git a/package-lock.json b/package-lock.json index 67b118394cfb79..6558739c520546 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,14 +16,14 @@ "test/e2e", "test/performance", "test/storybook-playwright", + "test/unit", "tools/api-docs" ], "devDependencies": { - "@apidevtools/json-schema-ref-parser": "11.6.4", "@babel/core": "7.25.7", "@emotion/babel-plugin": "11.13.5", "@emotion/is-prop-valid": "1.4.0", - "@emotion/jest": "11.14.2", + "@emotion/jest": "^11.14.2", "@emotion/native": "11.11.0", "@emotion/react": "11.14.0", "@emotion/styled": "11.14.1", @@ -34,8 +34,6 @@ "@inquirer/prompts": "7.2.0", "@octokit/rest": "16.26.0", "@testing-library/jest-dom": "6.9.1", - "@testing-library/react": "16.3.2", - "@testing-library/user-event": "14.6.1", "@types/node": "^20.17.10", "@types/prettier": "2.4.4", "@types/react": "^18.3.27", @@ -73,10 +71,7 @@ "globals": "14.0.0", "husky": "7.0.0", "jest": "29.6.2", - "jest-environment-jsdom": "^30.2.0", - "jest-jasmine2": "29.6.2", - "jest-junit": "13.0.0", - "jest-message-util": "^29.6.2", + "jest-junit": "^13.0.0", "jest-watch-typeahead": "2.2.2", "jsonc-parser": "3.3.1", "lerna": "8.1.9", @@ -92,14 +87,12 @@ "react-dom": "18.3.1", "react-scanner": "1.2.0", "reassure": "0.7.1", - "resize-observer-polyfill": "1.5.1", "rimraf": "5.0.10", "sass": "^1.54.0", "sass-embedded": "^1.97.2", "sass-loader": "16.0.3", "semver": "7.5.4", "simple-git": "3.24.0", - "snapshot-diff": "0.10.0", "sprintf-js": "1.1.1", "style-loader": "3.2.1", "stylelint-plugin-logical-css": "^1.2.3", @@ -21034,6 +21027,10 @@ "resolved": "packages/undo-manager", "link": true }, + "node_modules/@wordpress/unit-tests": { + "resolved": "test/unit", + "link": true + }, "node_modules/@wordpress/upload-media": { "resolved": "packages/upload-media", "link": true @@ -63564,6 +63561,29 @@ "storybook": "^10.1.11" } }, + "test/unit": { + "name": "@wordpress/unit-tests", + "version": "0.0.0", + "license": "GPL-2.0-or-later", + "devDependencies": { + "@emotion/jest": "^11.14.2", + "@testing-library/jest-dom": "^6.9.1", + "@testing-library/react": "^16.3.2", + "@testing-library/user-event": "^14.6.1", + "@wordpress/jest-preset-default": "file:../../packages/jest-preset-default", + "@wordpress/scripts": "file:../../packages/scripts", + "babel-jest": "^29.7.0", + "glob": "^7.1.2", + "jest": "^29.6.2", + "jest-environment-jsdom": "^30.2.0", + "jest-jasmine2": "^29.6.2", + "jest-junit": "^13.0.0", + "jest-message-util": "^29.6.2", + "jest-watch-typeahead": "^2.2.2", + "resize-observer-polyfill": "^1.5.1", + "snapshot-diff": "^0.10.0" + } + }, "tools/api-docs": { "name": "@wordpress/api-docs-generator", "version": "0.0.0", diff --git a/package.json b/package.json index 356d949750d79e..bb59621be112bb 100644 --- a/package.json +++ b/package.json @@ -40,11 +40,10 @@ "IS_GUTENBERG_PLUGIN": true }, "devDependencies": { - "@apidevtools/json-schema-ref-parser": "11.6.4", "@babel/core": "7.25.7", "@emotion/babel-plugin": "11.13.5", "@emotion/is-prop-valid": "1.4.0", - "@emotion/jest": "11.14.2", + "@emotion/jest": "^11.14.2", "@emotion/native": "11.11.0", "@emotion/react": "11.14.0", "@emotion/styled": "11.14.1", @@ -55,8 +54,6 @@ "@inquirer/prompts": "7.2.0", "@octokit/rest": "16.26.0", "@testing-library/jest-dom": "6.9.1", - "@testing-library/react": "16.3.2", - "@testing-library/user-event": "14.6.1", "@types/node": "^20.17.10", "@types/prettier": "2.4.4", "@types/react": "^18.3.27", @@ -94,10 +91,7 @@ "globals": "14.0.0", "husky": "7.0.0", "jest": "29.6.2", - "jest-environment-jsdom": "^30.2.0", - "jest-jasmine2": "29.6.2", - "jest-junit": "13.0.0", - "jest-message-util": "^29.6.2", + "jest-junit": "^13.0.0", "jest-watch-typeahead": "2.2.2", "jsonc-parser": "3.3.1", "lerna": "8.1.9", @@ -113,14 +107,12 @@ "react-dom": "18.3.1", "react-scanner": "1.2.0", "reassure": "0.7.1", - "resize-observer-polyfill": "1.5.1", "rimraf": "5.0.10", "sass": "^1.54.0", "sass-embedded": "^1.97.2", "sass-loader": "16.0.3", "semver": "7.5.4", "simple-git": "3.24.0", - "snapshot-diff": "0.10.0", "sprintf-js": "1.1.1", "style-loader": "3.2.1", "stylelint-plugin-logical-css": "^1.2.3", @@ -150,7 +142,7 @@ "docs:theme-ref": "npm run --workspace tools/api-docs docs:theme-ref", "env": "wp-env", "fixtures:clean": "rimraf --glob \"test/integration/fixtures/blocks/*.{json,serialized.html}\"", - "fixtures:generate": "cross-env GENERATE_MISSING_FIXTURES=y npm run test:unit test/integration/full-content/ && npm run format test/integration/fixtures/blocks/*.json", + "fixtures:generate": "cross-env GENERATE_MISSING_FIXTURES=y npm run test:unit -- -- test/integration/full-content/ && npm run format test/integration/fixtures/blocks/*.json", "fixtures:regenerate": "npm-run-all fixtures:clean fixtures:generate", "format": "wp-scripts format", "preformat:php": "npm run other:update-packages:php", @@ -203,10 +195,10 @@ "test:performance:debug": "npm exec --workspace @wordpress/performance-tests -- wp-scripts test-playwright --config playwright.config.ts --debug", "test:php": "npm-run-all lint:php test:unit:php", "test:php:watch": "wp-env --config .wp-env.test.json run --env-cwd='wp-content/plugins/gutenberg' cli composer run-script test:watch", - "test:unit": "wp-scripts test-unit-js --config test/unit/jest.config.js", + "test:unit": "npm run --workspace @wordpress/unit-tests test:unit", "test:unit:date": "bash ./bin/unit-test-date.sh", - "test:unit:debug": "wp-scripts --inspect-brk test-unit-js --runInBand --no-cache --verbose --config test/unit/jest.config.js ", - "test:unit:profile": "wp-scripts --cpu-prof test-unit-js --runInBand --no-cache --verbose --config test/unit/jest.config.js ", + "test:unit:debug": "npm run --workspace @wordpress/unit-tests test:unit:debug", + "test:unit:profile": "npm run --workspace @wordpress/unit-tests test:unit:profile", "test:unit:php:setup": "wp-env --config .wp-env.test.json start", "test:unit:php:setup:debug": "wp-env --config .wp-env.test.json start --xdebug", "test:unit:php:base": "wp-env --config .wp-env.test.json run --env-cwd='wp-content/plugins/gutenberg' wordpress vendor/bin/phpunit -c phpunit.xml.dist --verbose", @@ -215,8 +207,8 @@ "test:unit:php:multisite:base": "wp-env --config .wp-env.test.json run --env-cwd='wp-content/plugins/gutenberg' wordpress vendor/bin/phpunit -c phpunit/multisite.xml --verbose", "test:unit:php:multisite": "npm-run-all test:unit:php:setup test:unit:php:multisite:base", "test:unit:php:multisite:debug": "npm-run-all test:unit:php:setup:debug test:unit:php:multisite:base", - "test:unit:update": "npm run test:unit -- --updateSnapshot", - "test:unit:watch": "npm run test:unit -- --watch", + "test:unit:update": "npm run --workspace @wordpress/unit-tests test:unit -- --updateSnapshot", + "test:unit:watch": "npm run --workspace @wordpress/unit-tests test:unit:watch", "wp-env": "wp-env", "wp-env-test": "wp-env --config .wp-env.test.json" }, @@ -260,6 +252,7 @@ "test/e2e", "test/performance", "test/storybook-playwright", + "test/unit", "tools/api-docs" ] } diff --git a/test/unit/jest.config.js b/test/unit/jest.config.js index da23dc791722ee..df877d277e28b3 100644 --- a/test/unit/jest.config.js +++ b/test/unit/jest.config.js @@ -1,12 +1,25 @@ /** * External dependencies */ +const path = require( 'path' ); const glob = require( 'glob' ).sync; +/** + * Path to root project directory. + */ +const ROOT_DIR = path.resolve( __dirname, '../..' ); + +// Ensure Babel config resolution works from the repo root, +// even when Jest runs from the workspace directory. +process.chdir( ROOT_DIR ); + // Finds all packages which are transpiled with Babel to force Jest to use their source code. -const transpiledPackageNames = glob( 'packages/*/src/index.{js,ts,tsx}' ).map( - ( fileName ) => fileName.split( '/' )[ 1 ] -); +const transpiledPackageNames = glob( + path.join( ROOT_DIR, 'packages/*/src/index.{js,ts,tsx}' ) +).map( ( fileName ) => { + const relative = path.relative( ROOT_DIR, fileName ); + return relative.split( path.sep )[ 1 ]; +} ); // Make sure the tests run in UTC timezone, regardless of the system timezone. process.env.TZ = 'UTC'; diff --git a/test/unit/package.json b/test/unit/package.json new file mode 100644 index 00000000000000..d5572ec9f9b0cd --- /dev/null +++ b/test/unit/package.json @@ -0,0 +1,49 @@ +{ + "name": "@wordpress/unit-tests", + "version": "0.0.0", + "description": "Unit tests for WordPress Gutenberg.", + "private": true, + "author": "The WordPress Contributors", + "license": "GPL-2.0-or-later", + "keywords": [ + "wordpress", + "gutenberg", + "unit-tests" + ], + "homepage": "https://github.com/WordPress/gutenberg/tree/HEAD/test/unit/README.md", + "repository": { + "type": "git", + "url": "git+https://github.com/WordPress/gutenberg.git", + "directory": "test/unit" + }, + "bugs": { + "url": "https://github.com/WordPress/gutenberg/issues" + }, + "devDependencies": { + "@emotion/jest": "^11.14.2", + "@testing-library/jest-dom": "^6.9.1", + "@testing-library/react": "^16.3.2", + "@testing-library/user-event": "^14.6.1", + "@wordpress/jest-preset-default": "file:../../packages/jest-preset-default", + "@wordpress/scripts": "file:../../packages/scripts", + "babel-jest": "^29.7.0", + "glob": "^7.1.2", + "jest": "^29.6.2", + "jest-environment-jsdom": "^30.2.0", + "jest-jasmine2": "^29.6.2", + "jest-junit": "^13.0.0", + "jest-message-util": "^29.6.2", + "jest-watch-typeahead": "^2.2.2", + "resize-observer-polyfill": "^1.5.1", + "snapshot-diff": "^0.10.0" + }, + "publishConfig": { + "access": "public" + }, + "scripts": { + "test:unit": "wp-scripts test-unit-js --config jest.config.js", + "test:unit:profile": "wp-scripts --cpu-prof test-unit-js --runInBand --no-cache --verbose --config jest.config.js", + "test:unit:watch": "npm run test:unit -- --watch", + "test:unit:debug": "wp-scripts --inspect-brk test-unit-js --runInBand --no-cache --verbose --config jest.config.js" + } +}