This repository was archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 410
feat(zonespec): add a spec for asynchronous tests #275
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
(function() { | ||
class AsyncTestZoneSpec implements ZoneSpec { | ||
_finishCallback: Function; | ||
_failCallback: Function; | ||
_pendingMicroTasks: boolean = false; | ||
_pendingMacroTasks: boolean = false; | ||
_alreadyErrored: boolean = false; | ||
runZone = Zone.current; | ||
|
||
constructor(finishCallback: Function, failCallback: Function, namePrefix: string) { | ||
this._finishCallback = finishCallback; | ||
this._failCallback = failCallback; | ||
this.name = 'asyncTestZone for ' + namePrefix; | ||
} | ||
|
||
_finishCallbackIfDone() { | ||
if (!(this._pendingMicroTasks || this._pendingMacroTasks)) { | ||
// We do this because we would like to catch unhandled rejected promises. | ||
// To do this quickly when there are native promises, we must run using an unwrapped | ||
// promise implementation. | ||
var symbol = (<any>Zone).__symbol__; | ||
var NativePromise: typeof Promise = <any>window[symbol('Promise')]; | ||
if (NativePromise) { | ||
NativePromise.resolve(true)[symbol('then')](() => { | ||
if (!this._alreadyErrored) { | ||
this.runZone.run(this._finishCallback); | ||
} | ||
}); | ||
} else { | ||
// For implementations which do not have nativePromise, use setTimeout(0). This is slower, | ||
// but it also works because Zones will handle errors when rejected promises have no | ||
// listeners after one macrotask. | ||
this.runZone.run(() => { | ||
setTimeout(() => { | ||
if (!this._alreadyErrored) { | ||
this._finishCallback(); | ||
} | ||
}, 0); | ||
}); | ||
} | ||
} | ||
} | ||
|
||
// ZoneSpec implementation below. | ||
|
||
name: string; | ||
|
||
// Note - we need to use onInvoke at the moment to call finish when a test is | ||
// fully synchronous. TODO(juliemr): remove this when the logic for | ||
// onHasTask changes and it calls whenever the task queues are dirty. | ||
onInvoke(parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, | ||
delegate: Function, applyThis: any, applyArgs: any[], source: string): any { | ||
try { | ||
return parentZoneDelegate.invoke(targetZone, delegate, applyThis, applyArgs, source); | ||
} finally { | ||
this._finishCallbackIfDone(); | ||
} | ||
} | ||
|
||
onHandleError(parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, | ||
error: any): boolean { | ||
// Let the parent try to handle the error. | ||
var result = parentZoneDelegate.handleError(targetZone, error); | ||
if (result) { | ||
this._failCallback(error.message ? error.message : 'unknown error'); | ||
this._alreadyErrored = true; | ||
} | ||
return false; | ||
} | ||
|
||
onScheduleTask(delegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, task: Task): Task { | ||
if (task.type == 'macroTask' && task.source == 'setInterval') { | ||
this._failCallback('Cannot use setInterval from within an async zone test.'); | ||
return; | ||
} | ||
|
||
return delegate.scheduleTask(targetZone, task); | ||
} | ||
|
||
onHasTask(delegate: ZoneDelegate, current: Zone, target: Zone, hasTaskState: HasTaskState) { | ||
delegate.hasTask(target, hasTaskState); | ||
|
||
if (hasTaskState.change == 'microTask') { | ||
this._pendingMicroTasks = hasTaskState.microTask; | ||
this._finishCallbackIfDone(); | ||
} else if (hasTaskState.change == 'macroTask') { | ||
this._pendingMacroTasks = hasTaskState.macroTask; | ||
this._finishCallbackIfDone(); | ||
} | ||
} | ||
} | ||
|
||
// Export the class so that new instances can be created with proper | ||
// constructor params. | ||
Zone['AsyncTestZoneSpec'] = AsyncTestZoneSpec; | ||
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,232 @@ | ||
import '../lib/zone-spec/async-test'; | ||
|
||
describe('AsyncTestZoneSpec', function() { | ||
var log; | ||
var AsyncTestZoneSpec = Zone['AsyncTestZoneSpec']; | ||
|
||
function finishCallback() { | ||
log.push('finish'); | ||
} | ||
|
||
function failCallback() { | ||
log.push('fail'); | ||
} | ||
|
||
beforeEach(() => { | ||
log = []; | ||
}); | ||
|
||
it('should call finish after zone is run', (done) => { | ||
var finished = false; | ||
var testZoneSpec = new AsyncTestZoneSpec(() => { | ||
expect(finished).toBe(true); | ||
done(); | ||
}, failCallback, 'name'); | ||
|
||
var atz = Zone.current.fork(testZoneSpec); | ||
|
||
atz.run(function() { | ||
finished = true; | ||
}); | ||
}); | ||
|
||
it('should call finish after a setTimeout is done', (done) => { | ||
var finished = false; | ||
|
||
var testZoneSpec = new AsyncTestZoneSpec(() => { | ||
expect(finished).toBe(true); | ||
done(); | ||
}, () => { | ||
done.fail('async zone called failCallback unexpectedly'); | ||
}, 'name'); | ||
|
||
var atz = Zone.current.fork(testZoneSpec); | ||
|
||
atz.run(function() { | ||
setTimeout(() => { | ||
finished = true; | ||
}, 10); | ||
}); | ||
}); | ||
|
||
it('should call finish after microtasks are done', (done) => { | ||
var finished = false; | ||
|
||
var testZoneSpec = new AsyncTestZoneSpec(() => { | ||
expect(finished).toBe(true); | ||
done(); | ||
}, () => { | ||
done.fail('async zone called failCallback unexpectedly'); | ||
}, 'name'); | ||
|
||
var atz = Zone.current.fork(testZoneSpec); | ||
|
||
atz.run(function() { | ||
Promise.resolve().then(() => { | ||
finished = true; | ||
}); | ||
}); | ||
}); | ||
|
||
it('should call finish after both micro and macrotasks are done', (done) => { | ||
var finished = false; | ||
|
||
var testZoneSpec = new AsyncTestZoneSpec(() => { | ||
expect(finished).toBe(true); | ||
done(); | ||
}, () => { | ||
done.fail('async zone called failCallback unexpectedly'); | ||
}, 'name'); | ||
|
||
var atz = Zone.current.fork(testZoneSpec); | ||
|
||
atz.run(function() { | ||
var deferred = new Promise((resolve, reject) => { | ||
setTimeout(() => { | ||
resolve(); | ||
}, 10) | ||
}).then(() => { | ||
finished = true; | ||
}); | ||
}); | ||
}); | ||
|
||
it('should call finish after both macro and microtasks are done', (done) => { | ||
var finished = false; | ||
|
||
var testZoneSpec = new AsyncTestZoneSpec(() => { | ||
expect(finished).toBe(true); | ||
done(); | ||
}, () => { | ||
done.fail('async zone called failCallback unexpectedly'); | ||
}, 'name'); | ||
|
||
var atz = Zone.current.fork(testZoneSpec); | ||
|
||
atz.run(function() { | ||
Promise.resolve().then(() => { | ||
setTimeout(() => { | ||
finished = true; | ||
}, 10); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('event tasks', () => { | ||
var button; | ||
beforeEach(function() { | ||
button = document.createElement('button'); | ||
document.body.appendChild(button); | ||
}); | ||
afterEach(function() { | ||
document.body.removeChild(button); | ||
}); | ||
|
||
it('should call finish after an event task is done', (done) => { | ||
var finished = false; | ||
|
||
var testZoneSpec = new AsyncTestZoneSpec(() => { | ||
expect(finished).toBe(true); | ||
done(); | ||
}, () => { | ||
done.fail('async zone called failCallback unexpectedly'); | ||
}, 'name'); | ||
|
||
var atz = Zone.current.fork(testZoneSpec); | ||
|
||
atz.run(function() { | ||
button.addEventListener('click', () => { | ||
finished = true; | ||
}); | ||
|
||
var clickEvent = document.createEvent('Event'); | ||
clickEvent.initEvent('click', true, true); | ||
|
||
button.dispatchEvent(clickEvent); | ||
}); | ||
}); | ||
|
||
it('should call finish after an event task is done asynchronously', (done) => { | ||
var finished = false; | ||
|
||
var testZoneSpec = new AsyncTestZoneSpec(() => { | ||
expect(finished).toBe(true); | ||
done(); | ||
}, () => { | ||
done.fail('async zone called failCallback unexpectedly'); | ||
}, 'name'); | ||
|
||
var atz = Zone.current.fork(testZoneSpec); | ||
|
||
atz.run(function() { | ||
button.addEventListener('click', () => { | ||
setTimeout(() => { | ||
finished = true; | ||
}, 10); | ||
}); | ||
|
||
var clickEvent = document.createEvent('Event'); | ||
clickEvent.initEvent('click', true, true); | ||
|
||
button.dispatchEvent(clickEvent); | ||
}); | ||
}); | ||
}); | ||
|
||
it('should fail if setInterval is used', (done) => { | ||
var finished = false; | ||
|
||
var testZoneSpec = new AsyncTestZoneSpec(() => { | ||
done.fail('expected failCallback to be called'); | ||
}, (err) => { | ||
expect(err).toEqual('Cannot use setInterval from within an async zone test.'); | ||
done(); | ||
}, 'name'); | ||
|
||
var atz = Zone.current.fork(testZoneSpec); | ||
|
||
atz.run(function() { | ||
setInterval(() => { | ||
}, 100); | ||
}); | ||
}); | ||
|
||
it('should fail if an error is thrown asynchronously', (done) => { | ||
var finished = false; | ||
|
||
var testZoneSpec = new AsyncTestZoneSpec(() => { | ||
done.fail('expected failCallback to be called'); | ||
}, (err) => { | ||
expect(err).toEqual('my error'); | ||
done(); | ||
}, 'name'); | ||
|
||
var atz = Zone.current.fork(testZoneSpec); | ||
|
||
atz.run(function() { | ||
setTimeout(() => { | ||
throw new Error('my error'); | ||
}, 10); | ||
}); | ||
}); | ||
|
||
it('should fail if a promise rejection is unhandled', (done) => { | ||
var finished = false; | ||
|
||
var testZoneSpec = new AsyncTestZoneSpec(() => { | ||
done.fail('expected failCallback to be called'); | ||
}, (err) => { | ||
expect(err).toEqual('Uncaught (in promise): my reason'); | ||
done(); | ||
}, 'name'); | ||
|
||
var atz = Zone.current.fork(testZoneSpec); | ||
|
||
atz.run(function() { | ||
Promise.reject('my reason'); | ||
}); | ||
|
||
}); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The zone spec has explicit logic to test whether micro, macro and event tasks are empty. So should we have tests that mix the task types and wait for all of them to drain? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done! Added some. Also, that uncovered some bugs with the event task searching, so I fixed it up. Basically, we never want to wait on event tasks. |
||
|
||
export var __something__; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know the full details but just looking at the other specs - Do we also need a async-test.min.js?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope, I didn't add the build steps to make a min version, since this will only be for testing.