Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: custom reporter getLastError() -> this._shouldFail is undefined #15373

Open
mickeyjc opened this issue Nov 8, 2024 · 0 comments
Open

Comments

@mickeyjc
Copy link

mickeyjc commented Nov 8, 2024

Version

29.7.0

Steps to reproduce

I have followed the below document to create my own custom reporter,
https://jestjs.io/docs/configuration#custom-reporters

class CustomReporter {
  constructor(globalConfig, reporterOptions, reporterContext) {
    this._globalConfig = globalConfig;
    this._options = reporterOptions;
    this._context = reporterContext;
  }

  // Optionally, reporters can force Jest to exit with non zero code by returning
  // an `Error` from `getLastError()` method.
  getLastError() {
    console.log('this._shouldFail', this._shouldFail);
    if (this._shouldFail) {
      return new Error('Custom error reported!');
    }
  }
}

Expected behavior

when test fails., it should print out this._shouldFail true, and also should throw non 0 exit code

Actual behavior

console logged this._shouldFail undefined., and no error was thrown

Additional context

my jest.config.js as below

module.exports = {
  preset: 'ts-jest',
  collectCoverage: true,
  collectCoverageFrom: [
    'src/**/*.js',
    'src/**/*.ts',
    'src/**/*.tsx',
  ],
  transform: {
    '^.+\\.(ts|tsx)?$': 'ts-jest',
    '^.+\\.(js|jsx)$': 'babel-jest',
  },
  testMatch: ['**/_test_/**/*.test.js?(x)'],
  coverageReporters: ['json-summary'],
  reporters: ['default', '<rootDir>/scripts/customTestReporter.js'],
};

Environment

System:
OS: macOS 14.7
CPU: (8) arm64 Apple M3
Binaries:
Node: 18.14.0 - ~/.nvm/versions/node/v18.14.0/bin/node
Yarn: 1.22.22 - ~/.nvm/versions/node/v18.14.0/bin/yarn
npm: 9.3.1 - ~/.nvm/versions/node/v18.14.0/bin/npm
npmPackages:
jest: ^29.7.0 => 29.7.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant