Added support for ES2017 async functions

This commit is contained in:
Steve Gravrock
2017-05-12 14:42:14 -07:00
parent a237ac5386
commit 9672689d40
6 changed files with 88 additions and 7 deletions

View File

@@ -91,6 +91,13 @@ describe("Env", function() {
env.it('pending spec'); env.it('pending spec');
}).not.toThrow(); }).not.toThrow();
}); });
it('accepts an async function', function() {
jasmine.getEnv().requireAsyncAwait();
expect(function() {
env.it('async', jasmine.getEnv().makeAsyncAwaitFunction());
}).not.toThrow();
});
}); });
describe('#xit', function() { describe('#xit', function() {
@@ -114,6 +121,13 @@ describe("Env", function() {
env.xit('pending spec'); env.xit('pending spec');
}).not.toThrow(); }).not.toThrow();
}); });
it('accepts an async function', function() {
jasmine.getEnv().requireAsyncAwait();
expect(function() {
env.xit('async', jasmine.getEnv().makeAsyncAwaitFunction());
}).not.toThrow();
});
}); });
describe('#fit', function () { describe('#fit', function () {
@@ -130,6 +144,13 @@ describe("Env", function() {
env.beforeEach(undefined); env.beforeEach(undefined);
}).toThrowError(/beforeEach expects a function argument; received \[object (Undefined|DOMWindow|Object)\]/); }).toThrowError(/beforeEach expects a function argument; received \[object (Undefined|DOMWindow|Object)\]/);
}); });
it('accepts an async function', function() {
jasmine.getEnv().requireAsyncAwait();
expect(function() {
env.beforeEach(jasmine.getEnv().makeAsyncAwaitFunction());
}).not.toThrow();
});
}); });
describe('#beforeAll', function () { describe('#beforeAll', function () {
@@ -138,6 +159,13 @@ describe("Env", function() {
env.beforeAll(undefined); env.beforeAll(undefined);
}).toThrowError(/beforeAll expects a function argument; received \[object (Undefined|DOMWindow|Object)\]/); }).toThrowError(/beforeAll expects a function argument; received \[object (Undefined|DOMWindow|Object)\]/);
}); });
it('accepts an async function', function() {
jasmine.getEnv().requireAsyncAwait();
expect(function() {
env.beforeAll(jasmine.getEnv().makeAsyncAwaitFunction());
}).not.toThrow();
});
}); });
describe('#afterEach', function () { describe('#afterEach', function () {
@@ -146,6 +174,13 @@ describe("Env", function() {
env.afterEach(undefined); env.afterEach(undefined);
}).toThrowError(/afterEach expects a function argument; received \[object (Undefined|DOMWindow|Object)\]/); }).toThrowError(/afterEach expects a function argument; received \[object (Undefined|DOMWindow|Object)\]/);
}); });
it('accepts an async function', function() {
jasmine.getEnv().requireAsyncAwait();
expect(function() {
env.afterEach(jasmine.getEnv().makeAsyncAwaitFunction());
}).not.toThrow();
});
}); });
describe('#afterAll', function () { describe('#afterAll', function () {
@@ -154,5 +189,12 @@ describe("Env", function() {
env.afterAll(undefined); env.afterAll(undefined);
}).toThrowError(/afterAll expects a function argument; received \[object (Undefined|DOMWindow|Object)\]/); }).toThrowError(/afterAll expects a function argument; received \[object (Undefined|DOMWindow|Object)\]/);
}); });
it('accepts an async function', function() {
jasmine.getEnv().requireAsyncAwait();
expect(function() {
env.afterAll(jasmine.getEnv().makeAsyncAwaitFunction());
}).not.toThrow();
});
}); });
}); });

View File

@@ -0,0 +1,27 @@
(function(env) {
function getAsyncCtor() {
try {
eval("var func = async function(){};");
} catch (e) {
return null;
}
return Object.getPrototypeOf(func).constructor;
}
function hasAsyncAwaitSupport() {
return getAsyncCtor() !== null;
}
env.makeAsyncAwaitFunction = function() {
var AsyncFunction = getAsyncCtor();
return new AsyncFunction("");
};
env.requireAsyncAwait = function() {
if (!hasAsyncAwaitSupport()) {
env.pending("Environment does not support async/await functions");
}
};
})(jasmine.getEnv());

View File

@@ -6,6 +6,7 @@
"npmPackage/**/*.js" "npmPackage/**/*.js"
], ],
"helpers": [ "helpers": [
"helpers/asyncAwait.js",
"helpers/checkForSet.js", "helpers/checkForSet.js",
"helpers/nodeDefineJasmineUnderTest.js" "helpers/nodeDefineJasmineUnderTest.js"
], ],

View File

@@ -16,6 +16,7 @@ src_files:
- '**/*.js' - '**/*.js'
stylesheets: stylesheets:
helpers: helpers:
- 'helpers/asyncAwait.js'
- 'helpers/BrowserFlags.js' - 'helpers/BrowserFlags.js'
- 'helpers/checkForSet.js' - 'helpers/checkForSet.js'
- 'helpers/defineJasmineUnderTest.js' - 'helpers/defineJasmineUnderTest.js'

View File

@@ -319,6 +319,12 @@ getJasmineRequireObj().Env = function(j$) {
} }
}; };
var ensureIsFunctionOrAsync = function(fn, caller) {
if (!j$.isFunction_(fn) && !j$.isAsyncFunction_(fn)) {
throw new Error(caller + ' expects a function argument; received ' + j$.getType_(fn));
}
};
var suiteFactory = function(description) { var suiteFactory = function(description) {
var suite = new j$.Suite({ var suite = new j$.Suite({
env: self, env: self,
@@ -457,7 +463,7 @@ getJasmineRequireObj().Env = function(j$) {
// it() sometimes doesn't have a fn argument, so only check the type if // it() sometimes doesn't have a fn argument, so only check the type if
// it's given. // it's given.
if (arguments.length > 1 && typeof fn !== 'undefined') { if (arguments.length > 1 && typeof fn !== 'undefined') {
ensureIsFunction(fn, 'it'); ensureIsFunctionOrAsync(fn, 'it');
} }
var spec = specFactory(description, fn, currentDeclarationSuite, timeout); var spec = specFactory(description, fn, currentDeclarationSuite, timeout);
if (currentDeclarationSuite.markedPending) { if (currentDeclarationSuite.markedPending) {
@@ -471,7 +477,7 @@ getJasmineRequireObj().Env = function(j$) {
// xit(), like it(), doesn't always have a fn argument, so only check the // xit(), like it(), doesn't always have a fn argument, so only check the
// type when needed. // type when needed.
if (arguments.length > 1 && typeof fn !== 'undefined') { if (arguments.length > 1 && typeof fn !== 'undefined') {
ensureIsFunction(fn, 'xit'); ensureIsFunctionOrAsync(fn, 'xit');
} }
var spec = this.it.apply(this, arguments); var spec = this.it.apply(this, arguments);
spec.pend('Temporarily disabled with xit'); spec.pend('Temporarily disabled with xit');
@@ -479,7 +485,7 @@ getJasmineRequireObj().Env = function(j$) {
}; };
this.fit = function(description, fn, timeout){ this.fit = function(description, fn, timeout){
ensureIsFunction(fn, 'fit'); ensureIsFunctionOrAsync(fn, 'fit');
var spec = specFactory(description, fn, currentDeclarationSuite, timeout); var spec = specFactory(description, fn, currentDeclarationSuite, timeout);
currentDeclarationSuite.addChild(spec); currentDeclarationSuite.addChild(spec);
focusedRunnables.push(spec.id); focusedRunnables.push(spec.id);
@@ -496,7 +502,7 @@ getJasmineRequireObj().Env = function(j$) {
}; };
this.beforeEach = function(beforeEachFunction, timeout) { this.beforeEach = function(beforeEachFunction, timeout) {
ensureIsFunction(beforeEachFunction, 'beforeEach'); ensureIsFunctionOrAsync(beforeEachFunction, 'beforeEach');
currentDeclarationSuite.beforeEach({ currentDeclarationSuite.beforeEach({
fn: beforeEachFunction, fn: beforeEachFunction,
timeout: function() { return timeout || j$.DEFAULT_TIMEOUT_INTERVAL; } timeout: function() { return timeout || j$.DEFAULT_TIMEOUT_INTERVAL; }
@@ -504,7 +510,7 @@ getJasmineRequireObj().Env = function(j$) {
}; };
this.beforeAll = function(beforeAllFunction, timeout) { this.beforeAll = function(beforeAllFunction, timeout) {
ensureIsFunction(beforeAllFunction, 'beforeAll'); ensureIsFunctionOrAsync(beforeAllFunction, 'beforeAll');
currentDeclarationSuite.beforeAll({ currentDeclarationSuite.beforeAll({
fn: beforeAllFunction, fn: beforeAllFunction,
timeout: function() { return timeout || j$.DEFAULT_TIMEOUT_INTERVAL; } timeout: function() { return timeout || j$.DEFAULT_TIMEOUT_INTERVAL; }
@@ -512,7 +518,7 @@ getJasmineRequireObj().Env = function(j$) {
}; };
this.afterEach = function(afterEachFunction, timeout) { this.afterEach = function(afterEachFunction, timeout) {
ensureIsFunction(afterEachFunction, 'afterEach'); ensureIsFunctionOrAsync(afterEachFunction, 'afterEach');
currentDeclarationSuite.afterEach({ currentDeclarationSuite.afterEach({
fn: afterEachFunction, fn: afterEachFunction,
timeout: function() { return timeout || j$.DEFAULT_TIMEOUT_INTERVAL; } timeout: function() { return timeout || j$.DEFAULT_TIMEOUT_INTERVAL; }
@@ -520,7 +526,7 @@ getJasmineRequireObj().Env = function(j$) {
}; };
this.afterAll = function(afterAllFunction, timeout) { this.afterAll = function(afterAllFunction, timeout) {
ensureIsFunction(afterAllFunction, 'afterAll'); ensureIsFunctionOrAsync(afterAllFunction, 'afterAll');
currentDeclarationSuite.afterAll({ currentDeclarationSuite.afterAll({
fn: afterAllFunction, fn: afterAllFunction,
timeout: function() { return timeout || j$.DEFAULT_TIMEOUT_INTERVAL; } timeout: function() { return timeout || j$.DEFAULT_TIMEOUT_INTERVAL; }

View File

@@ -58,6 +58,10 @@ getJasmineRequireObj().base = function(j$, jasmineGlobal) {
return j$.isA_('Function', value); return j$.isA_('Function', value);
}; };
j$.isAsyncFunction_ = function(value) {
return j$.isA_('AsyncFunction', value);
};
j$.isA_ = function(typeName, value) { j$.isA_ = function(typeName, value) {
return j$.getType_(value) === '[object ' + typeName + ']'; return j$.getType_(value) === '[object ' + typeName + ']';
}; };