Skip to content

Commit

Permalink
build(testing): ent-3643 standalone tests (#753)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdcabrera committed Aug 17, 2021
1 parent a9214e3 commit 83012b8
Show file tree
Hide file tree
Showing 10 changed files with 767 additions and 54 deletions.
6 changes: 6 additions & 0 deletions config/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
# Configuration
## Build
Build support scripts.

## cspell config
The cspell config file(s) contain spelling configuration and include project specific terms.

## Spandx config
The Spandx config file(s) has multiple team and build dependencies. **Before relocating/moving this file(s) the appropriate teams should be informed.**

[See CONTRIBUTING.md for up-to-date information](../CONTRIBUTING.md#spandx-config)

## Testing
Jest configuration setup and transform scripts.
62 changes: 62 additions & 0 deletions config/build.dotenv.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const path = require('path');
const dotenv = require('dotenv');
const dotenvExpand = require('dotenv-expand');

/**
* Setup, and access, a dotenv file and the related set of parameters.
*
* @param {string} filePath
* @returns {void}
*/
const setupDotenvFile = filePath => {
const dotenvInitial = dotenv.config({ path: filePath });
dotenvExpand(dotenvInitial);
};

/**
* Setup and access local and specific dotenv file parameters.
*
* @param {object} params
* @param {string} params.env
* @param {string} params.relativePath
* @param {string} params.dotenvNamePrefix
* @param {boolean} params.setBuildDefaults
*/
const setupDotenvFilesForEnv = ({
env,
relativePath = path.resolve(__dirname, '..'),
dotenvNamePrefix = 'BUILD',
setBuildDefaults = true
} = {}) => {
if (env) {
setupDotenvFile(path.resolve(relativePath, `.env.${env}.local`));
setupDotenvFile(path.resolve(relativePath, `.env.${env}`));
}

setupDotenvFile(path.resolve(relativePath, '.env.local'));
setupDotenvFile(path.resolve(relativePath, '.env'));

if (setBuildDefaults) {
const STATIC_DIR = process.env[`${dotenvNamePrefix}_STATIC_DIR`] || 'public';
const PUBLIC_PATH = process.env[`${dotenvNamePrefix}_PUBLIC_PATH`] || '/';
const SRC_DIR = path.resolve(relativePath, process.env[`${dotenvNamePrefix}_SRC_DIR`] || 'src');
const DIST_DIR = path.resolve(relativePath, process.env[`${dotenvNamePrefix}_DIST_DIR`] || 'dist');
const HOST = process.env[`${dotenvNamePrefix}_HOST`] || 'localhost';
const PORT = process.env[`${dotenvNamePrefix}_PORT`] || '3000';
const DEV_MODE = process.env[`${dotenvNamePrefix}_DEV_MODE`] || undefined;
const OUTPUT_ONLY = process.env[`_${dotenvNamePrefix}_OUTPUT_ONLY`] === 'true';

process.env[`_${dotenvNamePrefix}_ENV`] = process.env.NODE_ENV;
process.env[`_${dotenvNamePrefix}_STATIC_DIR`] = STATIC_DIR;
process.env[`_${dotenvNamePrefix}_RELATIVE_DIRNAME`] = relativePath;
process.env[`_${dotenvNamePrefix}_PUBLIC_PATH`] = PUBLIC_PATH;
process.env[`_${dotenvNamePrefix}_SRC_DIR`] = SRC_DIR;
process.env[`_${dotenvNamePrefix}_DIST_DIR`] = DIST_DIR;
process.env[`_${dotenvNamePrefix}_HOST`] = HOST;
process.env[`_${dotenvNamePrefix}_PORT`] = PORT;
process.env[`_${dotenvNamePrefix}_OUTPUT_ONLY`] = OUTPUT_ONLY;
process.env[`_${dotenvNamePrefix}_DEV_MODE`] = DEV_MODE;
}
};

module.exports = { setupDotenvFilesForEnv };
33 changes: 27 additions & 6 deletions src/setupTests.js → config/jest.setupTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,16 @@ import Adapter from '@wojtekmaj/enzyme-adapter-react-17';
import { act } from 'react-dom/test-utils';
import * as pfReactCoreComponents from '@patternfly/react-core';
import * as pfReactChartComponents from '@patternfly/react-charts';
import { setupDotenvFilesForEnv } from './build.dotenv';

/**
* Set dotenv params.
*/
setupDotenvFilesForEnv({ env: 'test' });

/**
* Set enzyme adapter.
*/
configure({ adapter: new Adapter() });

/**
Expand Down Expand Up @@ -220,21 +229,33 @@ global.mockWindowLocation = async (
window.location = location;
};

// FixMe: revisit squashing log and group messaging, redux leaks log messaging
// ToDo: revisit squashing "popper" alerts
/*
* For applying a global Jest "beforeAll", based on
* jest-prop-type-error, https://www.npmjs.com/package/jest-prop-type-error
*/
beforeAll(() => {
const { error } = console;
const { error, group, log } = console;

const interceptConsoleMessaging = (method, callback) => {
console[method.name] = (message, ...args) => {
const isValid = callback(message, ...args);
if (isValid === true) {
method.apply(console, [message, ...args]);
}
};
};

interceptConsoleMessaging(group, () => process.env.CI !== 'true');

interceptConsoleMessaging(log, () => process.env.CI !== 'true');

console.error = (message, ...args) => {
interceptConsoleMessaging(error, (message, ...args) => {
if (/(Invalid prop|Failed prop type)/gi.test(message)) {
throw new Error(message);
}

if (!/(Not implemented: navigation)/gi.test(message) && !/Popper/gi.test(args?.[0])) {
error.apply(console, [message, ...args]);
}
};
return !/(Not implemented: navigation)/gi.test(message) && !/Popper/gi.test(args?.[0]);
});
});
8 changes: 8 additions & 0 deletions config/jest.transform.file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const path = require('path');

module.exports = {
process(src, filename) {
const assetFilename = JSON.stringify(path.basename(filename));
return `module.exports = ${assetFilename};`;
}
};
8 changes: 8 additions & 0 deletions config/jest.transform.style.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
process() {
return 'module.exports = {};';
},
getCacheKey() {
return 'cssTransform';
}
};
48 changes: 48 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
module.exports = {
collectCoverageFrom: [
'src/**/*.{js,jsx}',
'!src/**/.*/**',
'!src/index.js',
'!src/setupTests.js',
'!src/components/app.js',
'!src/components/**/index.js',
'!src/common/index.js',
'!src/redux/index.js',
'!src/redux/store.js',
'!src/redux/middleware/**',
'!src/redux/actions/index.js',
'!src/redux/common/index.js',
'!src/redux/reducers/index.js',
'!src/redux/selectors/index.js'
],
coverageThreshold: {
global: {
branches: 80,
functions: 80,
lines: 90,
statements: 90
}
},
moduleFileExtensions: ['web.js', 'js', 'web.ts', 'ts', 'web.tsx', 'tsx', 'json', 'web.jsx', 'jsx', 'node'],
moduleNameMapper: {
'^react-native$': 'react-native-web',
'^.+\\.module\\.(css|sass|scss)$': 'identity-obj-proxy'
},
modulePaths: [],
resetMocks: true,
roots: ['<rootDir>/src', '<rootDir>/tests'],
setupFilesAfterEnv: ['<rootDir>/config/jest.setupTests.js'],
snapshotSerializers: ['enzyme-to-json/serializer'],
testMatch: ['<rootDir>/**/__tests__/**/*.{js,jsx,ts,tsx}', '<rootDir>/**/*.{spec,test}.{js,jsx,ts,tsx}'],
testEnvironment: 'jsdom',
transform: {
'^.+\\.(js|jsx|ts|tsx)$': '<rootDir>/node_modules/babel-jest',
'^.+\\.css$': '<rootDir>/config/jest.transform.style.js',
'^(?!.*\\.(js|jsx|ts|tsx|css|json)$)': '<rootDir>/config/jest.transform.file.js'
},
transformIgnorePatterns: [
'[/\\\\]node_modules[/\\\\].+\\.(js|jsx|mjs|cjs|ts|tsx)$',
'^.+\\.module\\.(css|sass|scss)$'
],
watchPlugins: ['jest-watch-typeahead/filename', 'jest-watch-typeahead/testname']
};
61 changes: 25 additions & 36 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,33 +30,9 @@
"last 1 safari version"
]
},
"jest": {
"coverageThreshold": {
"global": {
"branches": 80,
"functions": 80,
"lines": 90,
"statements": 90
}
},
"collectCoverageFrom": [
"src/**/*.{js,jsx}",
"!src/**/.*/**",
"!src/index.js",
"!src/setupTests.js",
"!src/components/app.js",
"!src/components/**/index.js",
"!src/common/index.js",
"!src/redux/index.js",
"!src/redux/store.js",
"!src/redux/middleware/**",
"!src/redux/actions/index.js",
"!src/redux/common/index.js",
"!src/redux/reducers/index.js",
"!src/redux/selectors/index.js"
],
"snapshotSerializers": [
"enzyme-to-json/serializer"
"babel": {
"presets": [
"react-app"
]
},
"standard-version": {
Expand All @@ -83,6 +59,10 @@
{
"type": "style",
"section": "Styles"
},
{
"type": "build",
"section": "Build"
}
]
},
Expand All @@ -103,15 +83,15 @@
"start:proxy": "sh -ac '. ./.env.proxy; run-s api:proxy-hosts; run-p -l start:js api:proxy'",
"start:standalone": "rm ./.env.development.local; run-p -l api:dev start:js",
"test": "run-s test:spell* test:lint test:ci",
"test:ci": "export CI=true; react-scripts test --env=jsdom --roots=./src --coverage",
"test:clearCache": "react-scripts test --clearCache",
"test:ci": "export CI=true; jest ./src --coverage",
"test:clearCache": "jest --clearCache",
"test:dev": "run-s test:spell test:lint test:local",
"test:integration": "jest --roots=./tests",
"test:integration-dev": "jest ./tests/* --watchAll",
"test:integration": "jest ./tests",
"test:integration-dev": "jest --roots=./tests --watch",
"test:lint": "eslint --ext=json --ext=js --ext=jsx src",
"test:spell-support": "cspell ./README.md ./config/README.md ./CONTRIBUTING.md --config ./config/cspell.config.json",
"test:spell": "cspell './public/locales/**/en*json' './src/**/*.js' --config ./config/cspell.config.json",
"test:local": "react-scripts test --env=jsdom --roots=./src"
"test:local": "jest --roots=./src --watch"
},
"dependencies": {
"@patternfly/patternfly": "4.115.2",
Expand Down Expand Up @@ -153,15 +133,24 @@
"victory-create-container": "^35.8.5"
},
"devDependencies": {
"@babel/core": "7.14.8",
"@babel/preset-react": "^7.14.5",
"@wojtekmaj/enzyme-adapter-react-17": "^0.6.2",
"apidoc-mock": "^4.0.0",
"babel-eslint": "^10.1.0",
"babel-jest": "^26.6.0",
"cspell": "^5.6.6",
"dotenv": "^10.0.0",
"dotenv-expand": "^5.1.0",
"enzyme": "^3.11.0",
"enzyme-to-json": "^3.6.2",
"eslint": "7.31.0",
"eslint-config-airbnb": "^18.2.1",
"eslint-config-prettier": "^8.3.0",
"eslint-config-react-app": "^6.0.0",
"eslint-plugin-flowtype": "^5.8.2",
"eslint-plugin-import": "^2.23.4",
"eslint-plugin-jest": "^24.3.6",
"eslint-plugin-jest": "^24.4.0",
"eslint-plugin-jsdoc": "^35.4.5",
"eslint-plugin-json": "^3.0.0",
"eslint-plugin-jsx-a11y": "^6.4.1",
Expand All @@ -170,15 +159,15 @@
"eslint-plugin-react-hooks": "^4.2.0",
"express": "^4.17.1",
"glob": "^7.1.7",
"jest": "26.6.0",
"jest-resolve": "26.6.0",
"jest-watch-typeahead": "0.6.4",
"moxios": "^0.4.0",
"npm-run-all": "^4.1.5",
"prettier": "^2.2.1",
"redux-mock-store": "^1.5.4",
"standard-version": "^9.3.1",
"swagger-ui-express": "^4.1.6",
"yamljs": "^0.3.0"
},
"resolutions": {
"**/eslint": "7.19.0"
}
}
6 changes: 3 additions & 3 deletions src/common/__tests__/__snapshots__/helpers.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Object {
"UI_LOGGER_FILE": "curiosity_debug_log_{0}.json",
"UI_LOGGER_ID": "curiosity",
"UI_NAME": "subscriptions",
"UI_PATH": "/",
"UI_PATH": "/apps/subscriptions/",
"UI_VERSION": "0.0.0.0000000",
"UI_WINDOW_ID": "curiosity",
"browserExpose": [Function],
Expand Down Expand Up @@ -62,7 +62,7 @@ Object {
"UI_LOGGER_FILE": "curiosity_debug_log_{0}.json",
"UI_LOGGER_ID": "curiosity",
"UI_NAME": "subscriptions",
"UI_PATH": "/",
"UI_PATH": "/apps/subscriptions/",
"UI_VERSION": "0.0.0.0000000",
"UI_WINDOW_ID": "curiosity",
"browserExpose": [Function],
Expand Down Expand Up @@ -100,7 +100,7 @@ Object {
"UI_LOGGER_FILE": "curiosity_debug_log_{0}.json",
"UI_LOGGER_ID": "curiosity",
"UI_NAME": "subscriptions",
"UI_PATH": "/",
"UI_PATH": "/apps/subscriptions/",
"UI_VERSION": "0.0.0.0000000",
"UI_WINDOW_ID": "curiosity",
"browserExpose": [Function],
Expand Down
1 change: 0 additions & 1 deletion tests/__snapshots__/code.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ Array [
"components/inventorySubscriptions/inventorySubscriptions.js:60: console.warn(\`Sorting can only be performed on select fields, confirm field \${id} is allowed.\`);",
"redux/common/reduxHelpers.js:250: console.error(\`Error: Property \${prop} does not exist within the passed state.\`, state);",
"redux/common/reduxHelpers.js:254: console.warn(\`Warning: Property \${prop} does not exist within the passed initialState.\`, initialState);",
"setupTests.js:231: console.error = (message, ...args) => {",
]
`;
Loading

0 comments on commit 83012b8

Please sign in to comment.