diff --git a/jest.config.js b/jest.config.js index daddb04b2..39283f8ad 100644 --- a/jest.config.js +++ b/jest.config.js @@ -15,6 +15,12 @@ const esModules = [ 'uint8array-extras', ].join('|') +import path from 'path' +import { fileURLToPath } from 'url' + +const filename = fileURLToPath(import.meta.url) +const dirname = path.dirname(filename) + /** @type {import('jest').Config} */ const baseJestConfig = { extensionsToTreatAsEsm: ['.ts', '.tsx'], @@ -36,10 +42,14 @@ const baseJestConfig = { '^.+\\.(t|j)sx?$': ['@swc/jest'], }, verbose: true, + reporters: [ + path.resolve(dirname, './test/jest-spec-reporter.cjs'), + path.resolve(dirname, './test/jestreporter.cjs'), + ], } if (process.env.CI) { - baseJestConfig.reporters = [['github-actions', { silent: false }], 'summary'] + baseJestConfig.reporters.push(['github-actions', { silent: false }], 'summary') } export default baseJestConfig diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 9c6ceb495..09063f9b3 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -2787,9 +2787,11 @@ packages: '@esbuild-kit/core-utils@3.3.2': resolution: {integrity: sha512-sPRAnw9CdSsRmEtnsl2WXWdyquogVpB3yZ3dgwJfe8zrOzTsV7cJvmwrKVa+0ma5BoiGJ+BoqkMvawbayKUsqQ==} + deprecated: 'Merged into tsx: https://tsx.is' '@esbuild-kit/esm-loader@2.6.5': resolution: {integrity: sha512-FxEMIkJKnodyA1OaCUoEvbYRkoZlLZ4d/eXFu9Fh8CbBBgP5EmZxrfTRyN0qpXZ4vOvqnE5YdRdcrmUUXuU+dA==} + deprecated: 'Merged into tsx: https://tsx.is' '@esbuild/aix-ppc64@0.19.12': resolution: {integrity: sha512-bmoCYyWdEL3wDQIVbcyzRyeKLgk2WtWLTWz1ZIAZF/EGbNOwSA6ew3PftJ1PqMiOOGu0OyFMzG53L0zqIpPeNA==} diff --git a/test/_community/payload-types.ts b/test/_community/payload-types.ts index 828764d8f..74282f914 100644 --- a/test/_community/payload-types.ts +++ b/test/_community/payload-types.ts @@ -273,4 +273,4 @@ export interface Auth { declare module 'payload' { // @ts-ignore export interface GeneratedTypes extends Config {} -} +} \ No newline at end of file diff --git a/test/jest-spec-reporter.cjs b/test/jest-spec-reporter.cjs new file mode 100644 index 000000000..1af65b4d3 --- /dev/null +++ b/test/jest-spec-reporter.cjs @@ -0,0 +1,100 @@ +// From https://github.com/robertbradleyux/jest-ci-spec-reporter/blob/main/src/jest-ci-spec-reporter.ts +/* +MIT License + +Copyright (c) 2023 Robert Bradley + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ +'use strict' +Object.defineProperty(exports, '__esModule', { value: true }) +class JestCiSpecReporter { + onRunStart({ numTotalTestSuites }) { + console.log() + console.log(`Found ${numTotalTestSuites} test suites.`) + console.log() + } + onRunComplete(_, results) { + const { + numFailedTests, + numPassedTests, + numPendingTests, + testResults, + numTotalTests, + startTime, + } = results + testResults.forEach(({ failureMessage }) => { + if (failureMessage) { + console.log(failureMessage) + } + }) + const testResultText = numFailedTests === 0 ? 'SUCCESS' : 'FAILED' + const numNotSkippedTests = numPassedTests + numFailedTests + const runDuration = this._getRunDuration(startTime) + console.log() + console.log( + `Executed ${numNotSkippedTests} of ${numTotalTests} (skipped ${numPendingTests}) ${testResultText} (${runDuration})`, + ) + console.log(`TOTAL: ${numNotSkippedTests} ${testResultText}`) + } + onTestResult(test, { testResults }) { + testResults.forEach((result) => { + var _a, _b + const { title, duration, status, ancestorTitles } = result + const { name } = + (_b = (_a = test.context.config) === null || _a === void 0 ? void 0 : _a.displayName) !== + null && _b !== void 0 + ? _b + : {} + if (name) { + ancestorTitles.unshift(name) + } + const breadcrumbs = `${ancestorTitles.join(' > ')} >` + + console.log( + ` ${this._getTestStatus(status)} ${breadcrumbs} ${title} ${this._getTestDuration(duration)}`, + ) + }) + } + getLastError() { + return undefined + } + _getRunDuration(startTime) { + const deltaInMillis = new Date().getTime() - new Date(startTime).getTime() + const seconds = ((deltaInMillis % 60000) / 1000).toFixed(3) + return `${seconds} secs` + } + _getTestDuration(duration) { + return `\x1b[1m\x1b[30m(${duration !== null && duration !== void 0 ? duration : 0}ms)\x1b[0m` + } + _getTestStatus(status) { + switch (status) { + case 'passed': + return '\x1b[1m\x1b[32m[PASS]\x1b[0m' + case 'pending': + return '\x1b[1m\x1b[33m[SKIP]\x1b[0m' + case 'todo': + return '\x1b[1m\x1b[34m[TODO]\x1b[0m' + case 'failed': + default: + return '\x1b[1m\x1b[31m[FAIL]\x1b[0m' + } + } +} +exports.default = JestCiSpecReporter diff --git a/test/jest.config.js b/test/jest.config.js index 40c5eed18..06d34a82c 100644 --- a/test/jest.config.js +++ b/test/jest.config.js @@ -22,8 +22,4 @@ const customJestConfig = { }, } -if (process.env.CI) { - customJestConfig.reporters = [['github-actions', { silent: false }], 'summary'] -} - export default customJestConfig diff --git a/test/jest.setup.js b/test/jest.setup.js index 3d5ca8adc..1e8f74cce 100644 --- a/test/jest.setup.js +++ b/test/jest.setup.js @@ -1,3 +1,6 @@ +import console from 'console' +global.console = console + import { generateDatabaseAdapter } from './generateDatabaseAdapter.js' process.env.PAYLOAD_DISABLE_ADMIN = 'true' diff --git a/test/jestreporter.cjs b/test/jestreporter.cjs new file mode 100644 index 000000000..6eac18a61 --- /dev/null +++ b/test/jestreporter.cjs @@ -0,0 +1,16 @@ +class CustomReporter { + constructor(globalConfig, reporterOptions, reporterContext) { + this._globalConfig = globalConfig + this._options = reporterOptions + this._context = reporterContext + } + + onTestCaseResult(test, testCaseResult) { + if (testCaseResult.status === 'passed') { + return + } + console.log('Test case result:', testCaseResult) + } +} + +module.exports = CustomReporter