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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not possible to use custom reporters written in typescript #10281

Closed
Xotabu4 opened this issue Jul 17, 2020 · 2 comments
Closed

Not possible to use custom reporters written in typescript #10281

Xotabu4 opened this issue Jul 17, 2020 · 2 comments

Comments

@Xotabu4
Copy link

Xotabu4 commented Jul 17, 2020

馃悰 Bug Report (repost of - kulshekhar/ts-jest#1811)

This is probably more like a feature request. Jest allows to load own custom reporters with syntax:

preset: 'ts-jest',
testEnvironment: "node",
reporters: ["default", "./myReporter.ts"]

But when using ts-jest, these .ts reporters are not compiled automatically, so error is happens. It seems like jest does not pass reporters code to transformer

import type { Reporter, Test, AggregatedResult, TestResult, ReporterOnStartOptions, Context } from '@jest/reporters'
^^^^^^

SyntaxError: Cannot use import statement outside a module

Sample reporter code:

import type { Reporter, Test, AggregatedResult, TestResult, ReporterOnStartOptions, Context } from '@jest/reporters'

/**
 * @see https://github.com/facebook/jest/blob/master/packages/jest-reporters/src/types.ts
 * @see https://jestjs.io/docs/en/configuration#reporters-arraymodulename--modulename-options
*/
export default class MyReporter implements Reporter {
    onRunStart(results: AggregatedResult, options: ReporterOnStartOptions): void | Promise<void> {
        console.log('[MY REPORTER] onRunStart!')
    }
    onTestStart(test: Test): void | Promise<void> {
        console.log('[MY REPORTER] onTestStart!')
    }
    onTestResult(test: Test, testResult: TestResult, aggregatedResult: AggregatedResult): void | Promise<void> {
        console.log('[MY REPORTER] onTestResult!')
    }
    onRunComplete(contexts: Set<Context>, results: AggregatedResult): void | Promise<void> {
        console.log('[MY REPORTER] onRunComplete!')
    }
    getLastError(): void | Error {
        console.log('[MY REPORTER] last error!')
    }
}

To Reproduce

Steps to reproduce the behavior:

  • use sample reporter code and config example above, and just run tests as usual

Expected behavior

Expected ts compilation to be applied for custom jest typescript reporters as well

Debug log:

{"context":{"allowJs":false,"logLevel":20,"namespace":"jest-preset","package":"ts-jest","version":"26.1.1"},"message":"creating jest presets not handling JavaScript files","sequence":1,"time":"2020-07-17T08:50:43.404Z"}

## envinfo

System:
OS: MacOS

Npm packages:
jest: "jest": "^26.1.0",
ts-jest: "ts-jest": "^26.1.1",
typescript: "typescript": "^3.9.5",

@jeysal
Copy link
Contributor

jeysal commented Jul 17, 2020

#8810 tracks transforming all kinds of modules in the config

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants