Skip to content

Commit

Permalink
feat: add support for generator functions in specs (jestjs#5166)
Browse files Browse the repository at this point in the history
* feat: add support for generator functions in specs

Fixes jestjs#2666

* docs: update changelog with PR link
  • Loading branch information
SimenB authored and cpojer committed Dec 24, 2017
1 parent 5a37891 commit 23b7e98
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 10 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
## master

None for now

### Fixes

* `[babel-jest]` moduleFileExtensions not passed to babel transformer.
([#4637](https://github.com/facebook/jest/issues/4637))

### Features

* `[jest-jasmine2]` Support generator functions as specs.
([#5166](https://github.com/facebook/jest/pull/5166))

### Chore & Maintenance

## jest 22.0.1
Expand Down
14 changes: 8 additions & 6 deletions docs/GlobalAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ environment. You don't have to require or import anything to use them.
### `afterAll(fn, timeout)`

Runs a function after all the tests in this file have completed. If the function
returns a promise, Jest waits for that promise to resolve before continuing.
returns a promise or is a generator, Jest waits for that promise to resolve
before continuing.

Optionally, you can provide a `timeout` (in milliseconds) for specifying how
long to wait before aborting. _Note: The default timeout is 5 seconds._
Expand Down Expand Up @@ -63,8 +64,8 @@ If you want to run some cleanup after every test instead of after all tests, use
### `afterEach(fn, timeout)`

Runs a function after each one of the tests in this file completes. If the
function returns a promise, Jest waits for that promise to resolve before
continuing.
function returns a promise or is a generator, Jest waits for that promise to
resolve before continuing.

Optionally, you can provide a `timeout` (in milliseconds) for specifying how
long to wait before aborting. _Note: The default timeout is 5 seconds._
Expand Down Expand Up @@ -110,7 +111,8 @@ If you want to run some cleanup just once, after all of the tests run, use
### `beforeAll(fn, timeout)`

Runs a function before any of the tests in this file run. If the function
returns a promise, Jest waits for that promise to resolve before running tests.
returns a promise or is a generator, Jest waits for that promise to resolve
before running tests.

Optionally, you can provide a `timeout` (in milliseconds) for specifying how
long to wait before aborting. _Note: The default timeout is 5 seconds._
Expand Down Expand Up @@ -154,8 +156,8 @@ use `beforeEach` instead.
### `beforeEach(fn, timeout)`

Runs a function before each of the tests in this file runs. If the function
returns a promise, Jest waits for that promise to resolve before running the
test.
returns a promise or is a generator, Jest waits for that promise to resolve
before running the test.

Optionally, you can provide a `timeout` (in milliseconds) for specifying how
long to wait before aborting. _Note: The default timeout is 5 seconds._
Expand Down
6 changes: 6 additions & 0 deletions integration_tests/__tests__/jasmine_async.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,10 @@ describe('async jasmine', () => {
expect.stringContaining('Expected value to be truthy, instead received'),
);
});

it('generator test', () => {
const result = runJest('jasmine_async', ['generator.test.js']);

expect(result.status).toBe(0);
});
});
25 changes: 25 additions & 0 deletions integration_tests/jasmine_async/__tests__/generator.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';

function* someFunc() {
return 3;
}

describe('generators', () => {
beforeEach(function*() {
// This shouldn't throw
yield someFunc();
});

it('in spec', function*() {
const data = yield someFunc();

expect(data).toBe(3);
});
});
2 changes: 2 additions & 0 deletions packages/jest-jasmine2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
"dependencies": {
"callsites": "^2.0.0",
"chalk": "^2.0.1",
"co": "^4.6.0",
"expect": "^22.0.3",
"graceful-fs": "^4.1.11",
"is-generator-fn": "^1.0.0",
"jest-diff": "^22.0.3",
"jest-matcher-utils": "^22.0.3",
"jest-message-util": "^22.0.3",
Expand Down
9 changes: 7 additions & 2 deletions packages/jest-jasmine2/src/jasmine_async.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

import type {Global} from 'types/Global';

import isGeneratorFn from 'is-generator-fn';
import co from 'co';

function isPromise(obj) {
return obj && typeof obj.then === 'function';
}
Expand All @@ -34,7 +37,8 @@ function promisifyLifeCycleFunction(originalFn, env) {
// We make *all* functions async and run `done` right away if they
// didn't return a promise.
const asyncFn = function(done) {
const returnValue = fn.call({});
const wrappedFn = isGeneratorFn(fn) ? co.wrap(fn) : fn;
const returnValue = wrappedFn.call({});

if (isPromise(returnValue)) {
returnValue.then(done.bind(null, null), done.fail);
Expand Down Expand Up @@ -64,7 +68,8 @@ function promisifyIt(originalFn, env) {
}

const asyncFn = function(done) {
const returnValue = fn.call({});
const wrappedFn = isGeneratorFn(fn) ? co.wrap(fn) : fn;
const returnValue = wrappedFn.call({});

if (isPromise(returnValue)) {
returnValue.then(done.bind(null, null), done.fail);
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3988,6 +3988,10 @@ is-fullwidth-code-point@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/is-fullwidth-code-point/-/is-fullwidth-code-point-2.0.0.tgz#a3b30a5c4f199183167aaab93beefae3ddfb654f"

is-generator-fn@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/is-generator-fn/-/is-generator-fn-1.0.0.tgz#969d49e1bb3329f6bb7f09089be26578b2ddd46a"

is-glob@^2.0.0, is-glob@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/is-glob/-/is-glob-2.0.1.tgz#d096f926a3ded5600f3fdfd91198cb0888c2d863"
Expand Down

0 comments on commit 23b7e98

Please sign in to comment.