kulshekhar / ts-jest

A Jest transformer with source map support that lets you use Jest to test projects written in TypeScript.
https://kulshekhar.github.io/ts-jest
MIT License
6.95k stars 451 forks source link

Async iterables don't work #471

Closed slikts closed 6 years ago

slikts commented 6 years ago

I have the following async generator in my code:

const a = {
  async *b() {
    yield 1;
  }
};

This causes an "Unexpected token" when trying to run from a .ts file, but not a .js file, since I'm running node with the --harmony-async-iteration flag so async iteration works in .js files.

Async iteration should also work with ts-jest since TS itself supports it.

https://gist.github.com/slikts/edf805370c43f97988c872dfa7ca5d48

Versions: "jest": "^22.4.2", "ts-jest": "^22.4.2", "ts-node": "^5.0.1", "typescript": "^2.8.0-rc"

kulshekhar commented 6 years ago

@slikts Can you please create a minimal repo that reproduces this issue?

slikts commented 6 years ago

Repo: https://github.com/slikts/ts-jest-issue

npm i
npm run test
...
/mnt/e/work/ts-jest-issue/Foo.spec.ts: Unexpected token (3:10)

Edit: it might be relevant that when installing the deps, I get this notice:

npm WARN ts-jest@22.4.2 requires a peer of typescript@2.x but none is installed. You must install peer dependencies yourself.

However, typescript is clearly in the dependencies and gets installed.

slikts commented 6 years ago

Using ts-node with no flags produces the same Unexpected token error, while running it with node --harmony-async-iteration --require ts-node/register Foo.spec.ts works.

slikts commented 6 years ago

I've also added a .js file to show that async generators there work fine.

kulshekhar commented 6 years ago

@slikts the target in tsconfig should be set to es5 for tests. The reason is that the output of ts-jest is processed by jest.

By default, jest uses babel to process js files (which is why there's no error in the js file) but it expects the other transformers to return code in a form that it understands without any further processing which can be es5 (or es6 depending on your version of node)

shaunc commented 6 years ago

Hmm... perhaps ts-jest should optionally allow compiled ts code to run through babel? I would guess that the underlying problem is https://github.com/facebook/jest/issues/6492 . However, it would seem that misalignment between javascript and jest is always possible going forward, which is precisely why the jest uses babel.

huafu commented 6 years ago

@shaunc it does, it's just an option to activate, see babelConfig option there https://kulshekhar.github.io/ts-jest/user/config/#options

shaunc commented 6 years ago

Ah - nice :)