From ad1e3a981cfe3df6abdf753f7a73f506c330337f Mon Sep 17 00:00:00 2001 From: thelovekesh Date: Tue, 12 Apr 2022 16:41:29 +0530 Subject: [PATCH 1/4] Add jest packages --- package-lock.json | 137 ++++++++++++++++++++++++++++++++++++++++++++++ package.json | 7 ++- 2 files changed, 143 insertions(+), 1 deletion(-) diff --git a/package-lock.json b/package-lock.json index b84505be..fbd9dcb9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,6 +14,7 @@ "@wordpress/browserslist-config": "4.1.2", "@wordpress/env": "^4.4.0", "@wordpress/eslint-plugin": "11.1.0", + "@wordpress/jest-preset-default": "8.1.1", "@wordpress/scripts": "22.2.1", "browserslist": "4.20.0", "cross-env": "7.0.3", @@ -23,6 +24,7 @@ "eslint-plugin-import": "2.25.4", "eslint-plugin-jest": "26.1.3", "husky": "7.0.4", + "jest-silent-reporter": "0.5.0", "lint-staged": "12.3.7", "npm-run-all": "4.1.5", "webpack-remove-empty-scripts": "0.7.3" @@ -10055,6 +10057,24 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/is-ci": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/is-ci/-/is-ci-2.0.0.tgz", + "integrity": "sha512-YfJT7rkpQB0updsdHLGWrvhBJfcfzNNawYDNIyQXJz0IViGf75O8EBPKSdvw2rF+LGCsX4FZ8tcr3b19LcZq4w==", + "dev": true, + "dependencies": { + "ci-info": "^2.0.0" + }, + "bin": { + "is-ci": "bin.js" + } + }, + "node_modules/is-ci/node_modules/ci-info": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ci-info/-/ci-info-2.0.0.tgz", + "integrity": "sha512-5tK7EtrZ0N+OLFMthtqOj4fI2Jeb88C4CAZPu25LDVUgXJ0A3Js4PMGqrn0JU1W0Mh1/Z8wZzYPxqUrXeBboCQ==", + "dev": true + }, "node_modules/is-core-module": { "version": "2.8.1", "resolved": "https://registry.npmjs.org/is-core-module/-/is-core-module-2.8.1.tgz", @@ -11005,6 +11025,58 @@ "node": "^10.13.0 || ^12.13.0 || ^14.15.0 || >=15.0.0" } }, + "node_modules/jest-silent-reporter": { + "version": "0.5.0", + "resolved": "https://registry.npmjs.org/jest-silent-reporter/-/jest-silent-reporter-0.5.0.tgz", + "integrity": "sha512-epdLt8Oj0a1AyRiR6F8zx/1SVT1Mi7VU3y4wB2uOBHs/ohIquC7v2eeja7UN54uRPyHInIKWdL+RdG228n5pJQ==", + "dev": true, + "dependencies": { + "chalk": "^4.0.0", + "jest-util": "^26.0.0" + } + }, + "node_modules/jest-silent-reporter/node_modules/@jest/types": { + "version": "26.6.2", + "resolved": "https://registry.npmjs.org/@jest/types/-/types-26.6.2.tgz", + "integrity": "sha512-fC6QCp7Sc5sX6g8Tvbmj4XUTbyrik0akgRy03yjXbQaBWWNWGE7SGtJk98m0N8nzegD/7SggrUlivxo5ax4KWQ==", + "dev": true, + "dependencies": { + "@types/istanbul-lib-coverage": "^2.0.0", + "@types/istanbul-reports": "^3.0.0", + "@types/node": "*", + "@types/yargs": "^15.0.0", + "chalk": "^4.0.0" + }, + "engines": { + "node": ">= 10.14.2" + } + }, + "node_modules/jest-silent-reporter/node_modules/@types/yargs": { + "version": "15.0.14", + "resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-15.0.14.tgz", + "integrity": "sha512-yEJzHoxf6SyQGhBhIYGXQDSCkJjB6HohDShto7m8vaKg9Yp0Yn8+71J9eakh2bnPg6BfsH9PRMhiRTZnd4eXGQ==", + "dev": true, + "dependencies": { + "@types/yargs-parser": "*" + } + }, + "node_modules/jest-silent-reporter/node_modules/jest-util": { + "version": "26.6.2", + "resolved": "https://registry.npmjs.org/jest-util/-/jest-util-26.6.2.tgz", + "integrity": "sha512-MDW0fKfsn0OI7MS7Euz6h8HNDXVQ0gaM9uW6RjfDmd1DAFcaxX9OqIakHIqhbnmF08Cf2DLDG+ulq8YQQ0Lp0Q==", + "dev": true, + "dependencies": { + "@jest/types": "^26.6.2", + "@types/node": "*", + "chalk": "^4.0.0", + "graceful-fs": "^4.2.4", + "is-ci": "^2.0.0", + "micromatch": "^4.0.2" + }, + "engines": { + "node": ">= 10.14.2" + } + }, "node_modules/jest-snapshot": { "version": "27.5.1", "resolved": "https://registry.npmjs.org/jest-snapshot/-/jest-snapshot-27.5.1.tgz", @@ -25428,6 +25500,23 @@ "integrity": "sha512-nsuwtxZfMX67Oryl9LCQ+upnC0Z0BgpwntpS89m1H/TLF0zNfzfLMV/9Wa/6MZsj0acpEjAO0KF1xT6ZdLl95w==", "dev": true }, + "is-ci": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/is-ci/-/is-ci-2.0.0.tgz", + "integrity": "sha512-YfJT7rkpQB0updsdHLGWrvhBJfcfzNNawYDNIyQXJz0IViGf75O8EBPKSdvw2rF+LGCsX4FZ8tcr3b19LcZq4w==", + "dev": true, + "requires": { + "ci-info": "^2.0.0" + }, + "dependencies": { + "ci-info": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ci-info/-/ci-info-2.0.0.tgz", + "integrity": "sha512-5tK7EtrZ0N+OLFMthtqOj4fI2Jeb88C4CAZPu25LDVUgXJ0A3Js4PMGqrn0JU1W0Mh1/Z8wZzYPxqUrXeBboCQ==", + "dev": true + } + } + }, "is-core-module": { "version": "2.8.1", "resolved": "https://registry.npmjs.org/is-core-module/-/is-core-module-2.8.1.tgz", @@ -26174,6 +26263,54 @@ "graceful-fs": "^4.2.9" } }, + "jest-silent-reporter": { + "version": "0.5.0", + "resolved": "https://registry.npmjs.org/jest-silent-reporter/-/jest-silent-reporter-0.5.0.tgz", + "integrity": "sha512-epdLt8Oj0a1AyRiR6F8zx/1SVT1Mi7VU3y4wB2uOBHs/ohIquC7v2eeja7UN54uRPyHInIKWdL+RdG228n5pJQ==", + "dev": true, + "requires": { + "chalk": "^4.0.0", + "jest-util": "^26.0.0" + }, + "dependencies": { + "@jest/types": { + "version": "26.6.2", + "resolved": "https://registry.npmjs.org/@jest/types/-/types-26.6.2.tgz", + "integrity": "sha512-fC6QCp7Sc5sX6g8Tvbmj4XUTbyrik0akgRy03yjXbQaBWWNWGE7SGtJk98m0N8nzegD/7SggrUlivxo5ax4KWQ==", + "dev": true, + "requires": { + "@types/istanbul-lib-coverage": "^2.0.0", + "@types/istanbul-reports": "^3.0.0", + "@types/node": "*", + "@types/yargs": "^15.0.0", + "chalk": "^4.0.0" + } + }, + "@types/yargs": { + "version": "15.0.14", + "resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-15.0.14.tgz", + "integrity": "sha512-yEJzHoxf6SyQGhBhIYGXQDSCkJjB6HohDShto7m8vaKg9Yp0Yn8+71J9eakh2bnPg6BfsH9PRMhiRTZnd4eXGQ==", + "dev": true, + "requires": { + "@types/yargs-parser": "*" + } + }, + "jest-util": { + "version": "26.6.2", + "resolved": "https://registry.npmjs.org/jest-util/-/jest-util-26.6.2.tgz", + "integrity": "sha512-MDW0fKfsn0OI7MS7Euz6h8HNDXVQ0gaM9uW6RjfDmd1DAFcaxX9OqIakHIqhbnmF08Cf2DLDG+ulq8YQQ0Lp0Q==", + "dev": true, + "requires": { + "@jest/types": "^26.6.2", + "@types/node": "*", + "chalk": "^4.0.0", + "graceful-fs": "^4.2.4", + "is-ci": "^2.0.0", + "micromatch": "^4.0.2" + } + } + } + }, "jest-snapshot": { "version": "27.5.1", "resolved": "https://registry.npmjs.org/jest-snapshot/-/jest-snapshot-27.5.1.tgz", diff --git a/package.json b/package.json index 9edd7fde..1fa4d1fb 100644 --- a/package.json +++ b/package.json @@ -22,8 +22,9 @@ "@babel/core": "7.17.8", "@wordpress/babel-preset-default": "6.7.0", "@wordpress/browserslist-config": "4.1.2", - "@wordpress/eslint-plugin": "11.1.0", "@wordpress/env": "^4.4.0", + "@wordpress/eslint-plugin": "11.1.0", + "@wordpress/jest-preset-default": "8.1.1", "@wordpress/scripts": "22.2.1", "browserslist": "4.20.0", "cross-env": "7.0.3", @@ -33,6 +34,7 @@ "eslint-plugin-import": "2.25.4", "eslint-plugin-jest": "26.1.3", "husky": "7.0.4", + "jest-silent-reporter": "0.5.0", "lint-staged": "12.3.7", "npm-run-all": "4.1.5", "webpack-remove-empty-scripts": "0.7.3" @@ -54,6 +56,9 @@ "lint:staged": "lint-staged", "prepare": "husky install", "start": "wp-scripts start", + "test": "npm-run-all --parallel test:*", + "test:js": "wp-scripts test-unit-js --config=tests/js/jest.config.js", + "test:js:watch": "npm run test:js -- --watch", "pretest:php": "wp-env run composer 'install --no-interaction'", "test:php": "wp-env run phpunit 'phpunit -c /var/www/html/wp-content/themes/$(basename \"$PWD\")/phpunit.xml.dist --verbose'", "wp-env": "wp-env" From db2574a156b8101bdc86da661b63a1f792f08cb9 Mon Sep 17 00:00:00 2001 From: thelovekesh Date: Tue, 12 Apr 2022 16:41:44 +0530 Subject: [PATCH 2/4] Add config and rules for jest --- .eslintrc | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/.eslintrc b/.eslintrc index 72ba8415..61d4eb96 100644 --- a/.eslintrc +++ b/.eslintrc @@ -11,5 +11,19 @@ "rules": { "jsdoc/check-indentation": "error", "@wordpress/dependency-group": "error" - } + }, + "overrides": [{ + "files": [ + "**/__tests__/**/*.js", + "**/test/*.js", + "**/?(*.)test.js", + "tests/js/**/*.js" + ], + "extends": [ + "plugin:jest/all" + ], + "rules": { + // Add Rules for Jest here + } + }] } From 99c3b1eae8c45d6b4e7867c65198839202e12c94 Mon Sep 17 00:00:00 2001 From: thelovekesh Date: Tue, 12 Apr 2022 16:43:42 +0530 Subject: [PATCH 3/4] Add jest config file --- tests/js/jest.config.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 tests/js/jest.config.js diff --git a/tests/js/jest.config.js b/tests/js/jest.config.js new file mode 100644 index 00000000..ed13ea53 --- /dev/null +++ b/tests/js/jest.config.js @@ -0,0 +1,32 @@ +module.exports = { + rootDir: '../../', + ...require( '@wordpress/scripts/config/jest-unit.config' ), + transform: { + '^.+\\.[jt]sx?$': '/node_modules/@wordpress/scripts/config/babel-transform', + }, + setupFiles: [ + '/tests/js/setup-globals', + ], + preset: '@wordpress/jest-preset-default', + testPathIgnorePatterns: [ + '/.git', + '/node_modules', + '/assets/build', + '/vendor', + // Add more specific patterns here if needed. + ], + coveragePathIgnorePatterns: [ + '/node_modules', + '/assets/build/', + // Add more specific patterns here if needed. + ], + modulePathIgnorePatterns: [ + // Add more specific patterns here if needed. + ], + coverageReporters: [ 'lcov' ], + coverageDirectory: '/tests/logs', + reporters: [ + [ 'jest-silent-reporter', { useDots: true } ], + '/node_modules/@wordpress/scripts/config/jest-github-actions-reporter', + ], +}; From f5b9c52c53530f06c05fc29b5fd85c0b0044bbec Mon Sep 17 00:00:00 2001 From: thelovekesh Date: Tue, 12 Apr 2022 16:43:58 +0530 Subject: [PATCH 4/4] Add setup-globals file for jest --- tests/js/setup-globals.js | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 tests/js/setup-globals.js diff --git a/tests/js/setup-globals.js b/tests/js/setup-globals.js new file mode 100644 index 00000000..163f4181 --- /dev/null +++ b/tests/js/setup-globals.js @@ -0,0 +1,2 @@ +// Define globals for the test +// global.CSS = {};