From c01ae7cd942ac53270aea7597e47961be0163074 Mon Sep 17 00:00:00 2001 From: dosas Date: Wed, 15 May 2024 09:03:26 +0200 Subject: [PATCH 1/2] Switch to tfm-test --- jest.config.js | 67 ++++++++++++++++-------------------- package.json | 3 +- webpack/global_test_setup.js | 11 ++++++ 3 files changed, 43 insertions(+), 38 deletions(-) create mode 100644 webpack/global_test_setup.js diff --git a/jest.config.js b/jest.config.js index ca60d8a5..7417b5ae 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,37 +1,30 @@ -module.exports = { - verbose: true, - testMatch: ['**/*.test.js'], - testPathIgnorePatterns: [ - '.local', - '.bundle', - '/node_modules/', - '/foreman/', - ], - moduleDirectories: ['node_modules', 'webpack'], - testURL: 'http://localhost/', - collectCoverage: true, - collectCoverageFrom: [ - 'webpack/**/*.js', - '!webpack/js-yaml.js', - '!webpack/index.js', - '!webpack/test_setup.js', - '!webpack/**/bundle*', - '!webpack/stories/**', - '!webpack/**/*stories.js', - ], - coverageReporters: ['text', 'lcov'], - moduleNameMapper: { - '^.+\\.(png|gif|css|scss)$': 'identity-obj-proxy', - }, - globals: { - __testing__: true, - }, - transform: { - '^.+\\.js$': 'babel-jest', - }, - setupFiles: [ - 'raf/polyfill', - 'jest-prop-type-error', - './webpack/test_setup.js', - ], -}; +const tfmConfig = require('@theforeman/test/src/pluginConfig'); +const { + foremanRelativePath, + foremanLocation, +} = require('@theforeman/find-foreman'); + +const foremanReactRelative = 'webpack/assets/javascripts/react_app'; +const foremanFull = foremanLocation(); +const foremanReactFull = foremanRelativePath(foremanReactRelative); + +// Find correct path to foremanReact so we do not have to mock it in tests +tfmConfig.moduleNameMapper['^foremanReact(.*)$'] = `${foremanReactFull}/$1`; + +tfmConfig.setupFiles = ['./webpack/test_setup.js']; +tfmConfig.setupFilesAfterEnv = [ + './webpack/global_test_setup.js', + '@testing-library/jest-dom', +]; + +// Do not use default resolver +tfmConfig.resolver = null; +// Specify module dirs instead +tfmConfig.moduleDirectories = [ + `${foremanFull}/node_modules`, + `${foremanFull}/node_modules/@theforeman/vendor-core/node_modules`, + 'node_modules', + 'webpack/test-utils', +]; + +module.exports = tfmConfig; diff --git a/package.json b/package.json index 2a61c07d..8987c47e 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "devDependencies": { "@theforeman/builder": ">= 12.0.1", "@theforeman/eslint-plugin-foreman": ">= 12.0.1", + "@theforeman/find-foreman": "^13.0.0", "@theforeman/test": ">= 12.0.1", "@theforeman/vendor-dev": ">= 12.0.1", "babel-plugin-transform-class-properties": "^6.24.1", @@ -31,7 +32,7 @@ "lint-fix": "eslint --fix *.js webpack", "stylelint": "stylelint webpack/**/*.scss", "stylelint-fix": "stylelint webpack/**/*.scss --fix", - "test": "jest --no-cache webpack" + "test": "tfm-test --plugin --config jest.config.js" }, "repository": { "type": "git", diff --git a/webpack/global_test_setup.js b/webpack/global_test_setup.js new file mode 100644 index 00000000..a3aba506 --- /dev/null +++ b/webpack/global_test_setup.js @@ -0,0 +1,11 @@ +// runs before each test to make sure console.error output will +// fail a test (i.e. default PropType missing). Check the error +// output and traceback for actual error. +global.console.error = (error, stack) => { + /* eslint-disable-next-line no-console */ + if (stack) console.log(stack); // Prints out original stack trace + throw new Error(error); +}; + +// Increase jest timeout as some tests using multiple http mocks can time out on CI systems. +jest.setTimeout(10000); From 3798be63ecf56c6408398ea0379623ab4e3bc4d5 Mon Sep 17 00:00:00 2001 From: dosas Date: Wed, 15 May 2024 09:20:35 +0200 Subject: [PATCH 2/2] Use shared foreman actions js workflow --- .github/workflows/javascript_tests.yml | 38 +++++++++++--------------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/.github/workflows/javascript_tests.yml b/.github/workflows/javascript_tests.yml index d377c3f6..2e24b4d6 100644 --- a/.github/workflows/javascript_tests.yml +++ b/.github/workflows/javascript_tests.yml @@ -1,29 +1,23 @@ --- -name: React and Javascript tests -on: - pull_request: +name: JavaScript Testing +on: # yamllint disable-line rule:truthy push: branches: - master + pull_request: + paths: + - 'webpack/**' + - 'package.json' + - '.github/workflows/javascript_tests.yml' + +concurrency: + group: ${{ github.ref_name }}-${{ github.workflow }} + cancel-in-progress: true jobs: - test_js: - runs-on: ubuntu-latest - strategy: - fail-fast: false - matrix: - node: [14] - steps: - - uses: actions/checkout@v2 - - name: Setup Node - uses: actions/setup-node@v3 - with: - node-version: ${{ matrix.node }} - - run: npm install - - name: Linting - run: | - npm run lint - npm run stylelint - - name: Testing - run: npm run test + test: + name: JavaScript + uses: theforeman/actions/.github/workflows/foreman_plugin_js.yml@v0 + with: + plugin: foreman_acd ...