diff --git a/.eslintrc.js b/.eslintrc.js index 2ede77591..6825127d3 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -2,7 +2,7 @@ module.exports = { parser: "@babel/eslint-parser", // https://github.com/babel/babel/tree/main/eslint/babel-eslint-parser parserOptions: { babelOptions: { - configFile: "./.babelrc.json" + configFile: "./babel.config.json" }, ecmaFeatures: { arrowFunctions: true, @@ -25,7 +25,10 @@ module.exports = { }, plugins: [ "perfectionist", // https://github.com/azat-io/eslint-plugin-perfectionist - "react" // https://github.com/yannickcr/eslint-plugin-react + "react", // https://github.com/yannickcr/eslint-plugin-react + "jest", // https://github.com/jest-community/eslint-plugin-jest + "jest-dom", // https://github.com/testing-library/eslint-plugin-jest-dom + "testing-library" // https://github.com/testing-library/eslint-plugin-testing-library ], extends: [ "eslint:recommended", @@ -36,6 +39,12 @@ module.exports = { browser: true, // browser global variables node: true // Node.js global variables and Node.js-specific rules }, + overrides: [ + { + files: ["tests/**/*"], + env: { jest: true } + } + ], settings: { react: { version: "detect" diff --git a/.gitignore b/.gitignore index aa1c83173..399c86878 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ node_modules/ +coverage/ +.idea/ npm-debug.log # On a fresh install with yarn 3.3.0 these extra files were generated. diff --git a/.babelrc.json b/babel.config.json similarity index 61% rename from .babelrc.json rename to babel.config.json index fe63b8a84..89c452411 100644 --- a/.babelrc.json +++ b/babel.config.json @@ -4,12 +4,7 @@ "@babel/plugin-transform-object-rest-spread" ], "presets": [ - [ - "@babel/preset-env", - { - "modules": false - } - ], + "@babel/preset-env", "@babel/preset-react" ] } diff --git a/jest.config.json b/jest.config.json new file mode 100644 index 000000000..070c3c04c --- /dev/null +++ b/jest.config.json @@ -0,0 +1,20 @@ +{ + "collectCoverageFrom": [ + "./**/*.{js,jsx}" + ], + "coverageDirectory": "coverage", + "moduleNameMapper": { + "^openlayers$": "/libs/openlayers.js", + "\\.(css|less|svg|png)$": "/tests/mocks/AssetMock.js" + }, + "setupFilesAfterEnv": [ + "/tests/jest.setup.js" + ], + "testEnvironment": "jsdom", + "testMatch": [ + "/tests/**/*.test.(js|jsx|ts|tsx)" + ], + "transformIgnorePatterns": [ + "node_modules/(?!(color-name|color-parse|color-rgba|color-space|flat|ol|ol-ext)/)" + ] +} diff --git a/package.json b/package.json index 630893c4b..1a7f6c760 100644 --- a/package.json +++ b/package.json @@ -73,16 +73,28 @@ "@babel/preset-env": "^7.24.5", "@babel/preset-react": "^7.24.1", "@furkot/webfonts-generator": "^2.0.2", + "@testing-library/dom": "^10.1.0", + "@testing-library/jest-dom": "^6.4.6", + "@testing-library/react": "^16.0.0", "@types/react": "^18.3.1", "eslint": "^8.56.0", + "eslint-plugin-jest": "^28.6.0", + "eslint-plugin-jest-dom": "^5.4.0", "eslint-plugin-perfectionist": "^2.10.0", - "eslint-plugin-react": "^7.33.2", + "eslint-plugin-react": "^7.34.1", + "eslint-plugin-testing-library": "^6.2.2", + "jest": "^29.7.0", + "jest-environment-jsdom": "^29.7.0", "mkdirp": "^3.0.1", "object-path": "^0.11.8", "react-docgen": "^5.4.3", + "redux-mock-store": "^1.5.4", "typescript": "^5.4.5" }, "scripts": { - "plugindoc": "node scripts/gen-plugin-docs.js" + "plugindoc": "node scripts/gen-plugin-docs.js", + "lint": "eslint . --config .eslintrc.js --ext .js,.jsx,.ts,.tsx", + "test": "jest", + "coverage": "jest --coverage" } } diff --git a/tests/jest.setup.js b/tests/jest.setup.js new file mode 100644 index 000000000..7b0828bfa --- /dev/null +++ b/tests/jest.setup.js @@ -0,0 +1 @@ +import '@testing-library/jest-dom'; diff --git a/tests/libs/openlayers.test.js b/tests/libs/openlayers.test.js new file mode 100644 index 000000000..fe31e0658 --- /dev/null +++ b/tests/libs/openlayers.test.js @@ -0,0 +1,5 @@ +import ol from 'openlayers'; + +test('import openlayers', () => { + expect(ol).not.toBe(undefined); +}); diff --git a/tests/mocks/AssetMock.js b/tests/mocks/AssetMock.js new file mode 100644 index 000000000..f053ebf79 --- /dev/null +++ b/tests/mocks/AssetMock.js @@ -0,0 +1 @@ +module.exports = {};