Using Got, JEST and Typescript, getting nexpected token error

459 Views Asked by At

Here is the code, let's call it mycode.ts:

import { Tracer } from 'dd-trace';
import got from 'got';
....
export class MyClass {...}

And then in test, let call it mycode.test.ts, I just load the file

import { MyClass } from './mycode';

I get the following error:

({"Object.":function(module,exports,require,__dirname,__filename,jest){import create from './create.js'; ^^^^^^ Test suite failed to run

Jest encountered an unexpected token
...
SyntaxError: Cannot use import statement outside a module

  1 | import { Tracer } from 'dd-trace';
> 2 | import got from 'got';
    | ^

Any idea what is wrong?

1

There are 1 best solutions below

2
Ashiq Ali On

use this instead

const got = require('got');

and then

got.yourfunctionhere