From 45d89878e9837e61357e9f6514ea75999b5dc9a2 Mon Sep 17 00:00:00 2001 From: John Mitsch Date: Tue, 28 Apr 2020 18:50:07 +0000 Subject: [PATCH] Refs #29637 - Updating to dynamically find path --- jest.config.js | 68 +++++++++++++------ webpack/__mocks__/react-intl/index.js | 3 + .../__mocks__/react-intl/locale-data/en.js | 1 + 3 files changed, 51 insertions(+), 21 deletions(-) create mode 100644 webpack/__mocks__/react-intl/index.js create mode 100644 webpack/__mocks__/react-intl/locale-data/en.js diff --git a/jest.config.js b/jest.config.js index ba7831fecc5..69909ebac45 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,38 +1,64 @@ -const path = require('path') -//const foremanReact = path.join(__dirname, "../foreman/webpack/assets/javascripts/react_app") -const foremanReact = "/home/jenkins/workspace/katello-pr-test/foreman/webpack/assets/javascripts/react_app" +const path = require('path'); +const fs = require('fs'); +// Check for foremanReact files provided by Foreman and make available as module in Jest. +const checkForForemanReact = (foremanLocations, foremanReact) => { + const currentDir = process.cwd(); + + let foremanReactFullPath; + foremanLocations.forEach((relativeForemanPath) => { + if (fs.existsSync(path.join(currentDir, relativeForemanPath))) { + const fullPath = path.join(currentDir, relativeForemanPath, foremanReact); + if (fs.existsSync(fullPath)) foremanReactFullPath = fullPath; + } + }); + return foremanReactFullPath; +}; + +const foremanReactRelative = 'webpack/assets/javascripts/react_app'; +const possibleForemanLocations = ['./foreman', '../foreman', '../../foreman']; +const notFound = 'Foreman directory can not be found! These tests require Foreman to be present ' + +'in either a parent, sibling, or child directory relative to Katello and contain the expected ' + +`files in ./${foremanReactRelative}, relative to the Foreman directory.`; + +const foremanReactFull = checkForForemanReact(possibleForemanLocations, foremanReactRelative); +if (!foremanReactFull) throw new Error(notFound); + +// Jest configuration module.exports = { collectCoverage: true, collectCoverageFrom: [ - "webpack/**/*.js", - "!webpack/**/bundle*" + 'webpack/**/*.js', + '!webpack/**/bundle*', ], coverageReporters: [ - "lcov" + 'lcov', ], - testURL: "http://localhost/", + testURL: 'http://localhost/', setupFiles: [ - "raf/polyfill", - "./webpack/test_setup.js" + 'raf/polyfill', + './webpack/test_setup.js', ], setupFilesAfterEnv: [ - "./webpack/global_test_setup.js" + './webpack/global_test_setup.js', ], testPathIgnorePatterns: [ - "/node_modules/", - "/foreman/", - "/.+fixtures.+", - "/engines" + '/node_modules/', + '/foreman/', + '/.+fixtures.+', + '/engines', ], moduleDirectories: [ - "node_modules/@theforeman/vendor-core/node_modules", - "node_modules", - "webpack/test-utils" + 'node_modules/@theforeman/vendor-core/node_modules', + 'node_modules', + 'webpack/test-utils', + ], + modulePathIgnorePatterns: [ + '/foreman/', ], moduleNameMapper: { - "^.+\\.(css|scss)$": "identity-obj-proxy", - "foremanReact(.*)$": `${foremanReact}$1` - } -} + '^.+\\.(css|scss)$': 'identity-obj-proxy', + '^foremanReact(.*)$': `${foremanReactFull}$1`, + }, +}; diff --git a/webpack/__mocks__/react-intl/index.js b/webpack/__mocks__/react-intl/index.js new file mode 100644 index 00000000000..71efa7aed59 --- /dev/null +++ b/webpack/__mocks__/react-intl/index.js @@ -0,0 +1,3 @@ +export const addLocaleData = jest.fn(); +export const FormattedRelative = jest.fn(); +export const intlShape = jest.fn(); diff --git a/webpack/__mocks__/react-intl/locale-data/en.js b/webpack/__mocks__/react-intl/locale-data/en.js new file mode 100644 index 00000000000..ff8b4c56321 --- /dev/null +++ b/webpack/__mocks__/react-intl/locale-data/en.js @@ -0,0 +1 @@ +export default {};