mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Add package for sharing jest configuration for upcoming Features (#7198)
* Add package for shared jest configurations for Lens applications, Features and libraries. Signed-off-by: Janne Savolainen <janne.savolainen@live.fi> * Add root level script for running unit tests in monorepo mindset Signed-off-by: Janne Savolainen <janne.savolainen@live.fi> * Ignore coverage files from VCS Signed-off-by: Janne Savolainen <janne.savolainen@live.fi> --------- Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
parent
114db72485
commit
73230c8659
1
.gitignore
vendored
1
.gitignore
vendored
@ -6,3 +6,4 @@ lens.log
|
||||
docs/extensions/api
|
||||
site/
|
||||
lerna-debug.log
|
||||
coverage
|
||||
|
||||
4
jest.config.js
Normal file
4
jest.config.js
Normal file
@ -0,0 +1,4 @@
|
||||
const { monorepoRootConfig } = require("@k8slens/jest");
|
||||
|
||||
module.exports = monorepoRootConfig(__dirname);
|
||||
|
||||
1821
package-lock.json
generated
1821
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -22,6 +22,7 @@
|
||||
"mkdocs:serve-local": "docker build -t mkdocs-serve-local:latest mkdocs/ && docker run --rm -it -p 8000:8000 -v ${PWD}:/docs mkdocs-serve-local:latest",
|
||||
"mkdocs:verify": "docker build -t mkdocs-serve-local:latest mkdocs/ && docker run --rm -v ${PWD}:/docs mkdocs-serve-local:latest build --strict",
|
||||
"test:unit": "lerna run --stream test:unit",
|
||||
"test:unit:watch": "jest --watch",
|
||||
"test:integration": "lerna run --stream test:integration",
|
||||
"bump-version": "lerna version --no-git-tag-version --no-push",
|
||||
"precreate-release-pr": "cd packages/release-tool && npm run build",
|
||||
|
||||
55
packages/infrastructure/jest/README.md
Normal file
55
packages/infrastructure/jest/README.md
Normal file
@ -0,0 +1,55 @@
|
||||
# @k8slens/jest
|
||||
|
||||
This package contains jest configurations and scripts for Lens packages.
|
||||
|
||||
## Install
|
||||
|
||||
```
|
||||
$ npm install @k8slens/jest
|
||||
```
|
||||
|
||||
## Features
|
||||
|
||||
### Package configurations
|
||||
Shared configurations for minimal duplication.
|
||||
|
||||
#### Node
|
||||
|
||||
**./packages/<any-package>/jest.config.js**
|
||||
```javascript
|
||||
module.exports = require("@k8slens/jest").monorepoPackageConfig(__dirname).configForNode;
|
||||
```
|
||||
|
||||
#### React
|
||||
|
||||
**./packages/<any-package>/jest.config.js**
|
||||
```javascript
|
||||
module.exports = require("@k8slens/jest").monorepoPackageConfig(__dirname).configForReact;
|
||||
```
|
||||
|
||||
### Root configuration
|
||||
You may want to enable testing of packages using single command from root level. This allows you to utilize `jest --watch` between all packages.
|
||||
|
||||
|
||||
**./jest.config.js**
|
||||
```javascript
|
||||
module.exports = require("@k8slens/jest").monorepoRootConfig(__dirname);
|
||||
```
|
||||
|
||||
### Scripts
|
||||
|
||||
#### lens-test
|
||||
Test package with coverage enforcement. Automatically opens coverage report in case of failure.
|
||||
|
||||
**./packages/<any-package>/package.json**
|
||||
```json
|
||||
{
|
||||
"scripts": {
|
||||
"test": "lens-test"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
6
packages/infrastructure/jest/bin/test.sh
Executable file
6
packages/infrastructure/jest/bin/test.sh
Executable file
@ -0,0 +1,6 @@
|
||||
jest --coverage --runInBand
|
||||
result=$?
|
||||
|
||||
[ $result != 0 ] && [ -v $CI ] && open ./coverage/lcov-report/index.html
|
||||
|
||||
exit $result
|
||||
4
packages/infrastructure/jest/index.js
Normal file
4
packages/infrastructure/jest/index.js
Normal file
@ -0,0 +1,4 @@
|
||||
module.exports = {
|
||||
monorepoRootConfig: require("./monorepo-root-config"),
|
||||
monorepoPackageConfig: require("./monorepo-package-config"),
|
||||
};
|
||||
55
packages/infrastructure/jest/monorepo-package-config.js
Normal file
55
packages/infrastructure/jest/monorepo-package-config.js
Normal file
@ -0,0 +1,55 @@
|
||||
module.exports = (rootDir) => {
|
||||
const shared = {
|
||||
transform: {
|
||||
"^.+\\.(t|j)sx?$": ["@swc/jest", { cwd: rootDir }],
|
||||
},
|
||||
|
||||
clearMocks: true,
|
||||
coverageDirectory: "coverage",
|
||||
coverageProvider: "v8",
|
||||
coverageReporters: ["lcov"],
|
||||
collectCoverage: true,
|
||||
testMatch: ["**/?(*.)+(test).{js,ts,tsx}"],
|
||||
watchPathIgnorePatterns: ["/node_modules/", "/coverage/", "/build/"],
|
||||
|
||||
collectCoverageFrom: [
|
||||
"<rootDir>/src/**/*.{ts,tsx}",
|
||||
"!<rootDir>/src/**/*.no-coverage.ts",
|
||||
],
|
||||
|
||||
moduleNameMapper: {
|
||||
"^electron$": "identity-obj-proxy",
|
||||
},
|
||||
|
||||
coverageThreshold: {
|
||||
global: {
|
||||
branches: 100,
|
||||
functions: 100,
|
||||
lines: 100,
|
||||
statements: 100,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const configForNode = {
|
||||
...shared,
|
||||
testEnvironment: "node",
|
||||
};
|
||||
|
||||
const configForReact = {
|
||||
...shared,
|
||||
|
||||
moduleNameMapper: {
|
||||
"\\.(css|scss)$": "identity-obj-proxy",
|
||||
...shared.moduleNameMapper,
|
||||
},
|
||||
|
||||
testEnvironment: "jsdom",
|
||||
setupFilesAfterEnv: [`${__dirname}/setup-react-tests.ts`],
|
||||
};
|
||||
|
||||
return {
|
||||
configForReact,
|
||||
configForNode,
|
||||
};
|
||||
};
|
||||
73
packages/infrastructure/jest/monorepo-root-config.js
Normal file
73
packages/infrastructure/jest/monorepo-root-config.js
Normal file
@ -0,0 +1,73 @@
|
||||
const path = require("path");
|
||||
const glob = require("glob");
|
||||
const { omit } = require("lodash/fp");
|
||||
|
||||
const getProjectColor = (projectNumber) => {
|
||||
const colors = [
|
||||
"red",
|
||||
"green",
|
||||
"yellow",
|
||||
"magenta",
|
||||
"cyan",
|
||||
"white",
|
||||
"redBright",
|
||||
"greenBright",
|
||||
"yellowBright",
|
||||
"blueBright",
|
||||
"magentaBright",
|
||||
"cyanBright",
|
||||
"whiteBright",
|
||||
];
|
||||
|
||||
return colors[projectNumber % colors.length];
|
||||
};
|
||||
|
||||
const nonMultiProjectConfigs = [
|
||||
"coverageDirectory",
|
||||
"coverageProvider",
|
||||
"coverageReporters",
|
||||
"collectCoverage",
|
||||
"collectCoverageFrom",
|
||||
"coveragePathIgnorePatterns",
|
||||
"coverageThreshold",
|
||||
];
|
||||
|
||||
const toJestMultiProjectConfig = (
|
||||
{ packageJson, jestConfig, packagePath },
|
||||
projectNumber
|
||||
) => ({
|
||||
rootDir: packagePath,
|
||||
|
||||
displayName: {
|
||||
name: packageJson.name,
|
||||
color: getProjectColor(projectNumber),
|
||||
},
|
||||
|
||||
...omit(nonMultiProjectConfigs, jestConfig),
|
||||
});
|
||||
|
||||
const getJestConfigsAndPackageJsons = (rootDir) => {
|
||||
const packageJsonPaths = glob
|
||||
.sync(`${rootDir}/packages/**/jest.config.js`, {
|
||||
ignore: "./**/node_modules/**/*",
|
||||
})
|
||||
.map((filePath) => path.dirname(filePath));
|
||||
|
||||
return packageJsonPaths.map((packagePath) => ({
|
||||
packagePath,
|
||||
packageJson: require(`${packagePath}/package.json`),
|
||||
jestConfig: require(`${packagePath}/jest.config.js`),
|
||||
}));
|
||||
};
|
||||
|
||||
module.exports = (rootDir) => ({
|
||||
projects: getJestConfigsAndPackageJsons(rootDir).map(
|
||||
toJestMultiProjectConfig
|
||||
),
|
||||
|
||||
watchPlugins: [
|
||||
"jest-watch-typeahead/filename",
|
||||
"jest-watch-typeahead/testname",
|
||||
"jest-watch-select-projects",
|
||||
],
|
||||
});
|
||||
35
packages/infrastructure/jest/package.json
Normal file
35
packages/infrastructure/jest/package.json
Normal file
@ -0,0 +1,35 @@
|
||||
{
|
||||
"name": "@k8slens/jest",
|
||||
"private": false,
|
||||
"version": "0.0.1",
|
||||
"description": "Jest configuration and scripts for Lens packages.",
|
||||
"type": "commonjs",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/lensapp/lens.git"
|
||||
},
|
||||
"main": "index.js",
|
||||
"author": {
|
||||
"name": "OpenLens Authors",
|
||||
"email": "info@k8slens.dev"
|
||||
},
|
||||
"license": "MIT",
|
||||
"homepage": "https://github.com/lensapp/lens",
|
||||
"bin": {
|
||||
"lens-test": "bin/test.sh"
|
||||
},
|
||||
"dependencies": {
|
||||
"@swc/core": "^1.3.20",
|
||||
"@swc/jest": "^0.2.23",
|
||||
"@testing-library/jest-dom": "^5.16.5",
|
||||
"@testing-library/react": "^13.4.0",
|
||||
"@types/jest": "^29.2.2",
|
||||
"identity-obj-proxy": "^3.0.0",
|
||||
"jest": "^29.3.1",
|
||||
"jest-environment-jsdom": "^29.3.1",
|
||||
"jest-watch-select-projects": "^2.0.0",
|
||||
"jest-watch-typeahead": "^2.2.1",
|
||||
"lodash": "^4.17.21",
|
||||
"ts-jest": "^29.0.3"
|
||||
}
|
||||
}
|
||||
1
packages/infrastructure/jest/setup-react-tests.ts
Normal file
1
packages/infrastructure/jest/setup-react-tests.ts
Normal file
@ -0,0 +1 @@
|
||||
import "@testing-library/jest-dom";
|
||||
Loading…
Reference in New Issue
Block a user