1

I created a npm package using TSdx for a small Jest reporter. If I use that package in another project, I get the following error.

Uncaught TypeError: Class constructor BaseReporter cannot be invoked without 'new'
    at new ExampleReporter (..\dist\example-reporter.cjs.development.js:37:26)

The ExampleReporter inherits from the BaseReporter class provided by Jest.

import BaseReporter from '@jest/reporters/build/base_reporter';

export class ExampleReporter extends BaseReporter {
  // ...
}

As mentioned in Class constructor cannot be invoked without 'new', it can be fixed by setting up Babel. But I couldn't find a working solution with the TSdx documentation.

How can I configure the TSdx build?


You can reproduce the issue using the watch mode.

$ npx tsdx watch --onSuccess node .
...
  Watching for changes

Welcome to Node.js v12.18.2.
Type ".help" for more information.
> new (require('.').ExampleReporter)()
Uncaught TypeError: Class constructor BaseReporter cannot be invoked without 'new'
sschmeck
  • 7,233
  • 4
  • 40
  • 67
  • In a environment without `rollout` but using `tsc` as transpiler, setting `compilerOptions.target = "ES6"` fixes the issue. But it doesn;t help for the TSdx build. :/ – sschmeck Sep 29 '20 at 07:39

1 Answers1

2

You can just set --target node to fix the issue (since the default value is browser).

$ npx tsdx build --target node

Alternatively you can add a Babel configuration file .babelrc to your TDdx project and setup your target environment.

{
  "presets": [
    ["@babel/preset-env", { "targets": { "esmodules": true } }]
  ]
}
sschmeck
  • 7,233
  • 4
  • 40
  • 67