Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat: add support for generator functions in specs #5166

Merged
merged 2 commits into from
Dec 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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