Skip to content
New issue

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

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

Already on GitHub? # to your account

Add support for ES6 generator #2666

Closed
bibhas2 opened this issue Jan 22, 2017 · 11 comments · Fixed by #5166
Closed

Add support for ES6 generator #2666

bibhas2 opened this issue Jan 22, 2017 · 11 comments · Fixed by #5166

Comments

@bibhas2
Copy link

bibhas2 commented Jan 22, 2017

I am trying to test a generator function someFunc*() as follows.

describe('generator test', () => {
  it('should call generator function', function *() {
      var data = yield someFunc()

      expect(data).toBe(3);
  });
});

The test fails with this message:

Jest: `it` must return either a Promise or undefined.
@osterbergmarcus
Copy link

What if you try this approach?

describe('generator test', () => {
  it('should call generator function', function() {
      var data = someFunc()

      expect(data.next().value).toBe(3);
  });
});

@cpojer cpojer closed this as completed Jan 27, 2017
@cpojer
Copy link
Member

cpojer commented Jan 27, 2017

We are not adding support for this.

@vitalyrotari
Copy link

@cpojer This is sucks, because I use redux-saga.

FAIL  __tests__/index.android.js
  ● Test suite failed to run

   /app/redux/saga/auth.js: Unexpected token (58:20)
        56 |   try {
        57 |     const { email, password, name } = action.payload;
      > 58 |     const payload = yield call(Client.register, email, password, name);
           |                     ^
        59 |
        60 |     yield put(AuthActions.user(payload));
        61 |   } catch (error) {

Maybe exists some options how to resolve this problem?

@cpojer
Copy link
Member

cpojer commented Feb 6, 2017

It seems like you aren't properly transforming generators in this example, this is not a Jest error.

@Ajido
Copy link

Ajido commented Feb 25, 2017

@cpojer Generator Function is short code and easy to read. async/await is similar to it, but it's still early. Why don't you support this? This will be one reason for using ava.

@albertogasparin
Copy link

+1
As a workaround I'm using co to wrap the generator with a function that returns a promise:

import co from 'co';
describe('generator test', () => {
  it('should call generator function', co.wrap(function *() {
      var data = yield someFunc()

      expect(data).toBe(3);
  }));
});

@lautapercuspain
Copy link

lautapercuspain commented Jul 24, 2017

Hello @cpojer, what is your suggestion for testing generator functions ?
What is the Jest setup you recommend ?

Thanks in advance.

@6pm
Copy link

6pm commented Oct 9, 2017

Solution:
1 - add to package.json into jest section:
"jest": { "setupTestFrameworkScriptFile": "<rootDir>/testing/test-bundler.js" },
2 - create file '/testing/test-bundler.js' with content:
// needed for regenerator-runtime import 'babel-polyfill';

It's all!

@danrasmuson
Copy link

Generators are now part of javascript. Why is this outside the scope of what is supported?

SimenB added a commit to SimenB/jest that referenced this issue Dec 24, 2017
cpojer pushed a commit that referenced this issue Dec 24, 2017
* feat: add support for generator functions in specs

Fixes #2666

* docs: update changelog with PR link
@SimenB
Copy link
Member

SimenB commented Dec 24, 2017

This is now implemented, but not released.

#5166

@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 13, 2021
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants