From 0ada6d54388f363d6f2f3fb7e899b6f41010066c Mon Sep 17 00:00:00 2001 From: Sammy Jelin Date: Thu, 19 Jan 2017 12:53:09 -0800 Subject: [PATCH] feat(frameworks): Support `runner.afterEach` in jasmine and mocha adapter Closes https://github.com/angular/protractor/issues/3894, https://github.com/angular/protractor/issues/3908, and https://github.com/angular/protractor/issues/3909 --- ...rotractor_internal_afterEach_setup_spec.js | 10 +++++++ lib/frameworks/jasmine.js | 3 ++ lib/frameworks/mocha.js | 3 ++ lib/frameworks/setupAfterEach.js | 29 +++++++++++++++++++ scripts/test.js | 2 +- 5 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 lib/frameworks/__protractor_internal_afterEach_setup_spec.js create mode 100644 lib/frameworks/setupAfterEach.js diff --git a/lib/frameworks/__protractor_internal_afterEach_setup_spec.js b/lib/frameworks/__protractor_internal_afterEach_setup_spec.js new file mode 100644 index 000000000..a325c9b2b --- /dev/null +++ b/lib/frameworks/__protractor_internal_afterEach_setup_spec.js @@ -0,0 +1,10 @@ +// This is spec file is automatically added by protractor to implement our +// `afterEach` functionality for jasmine and mocha. + +var hooks = require('./setupAfterEach').hooks; + +afterEach(function() { + if (hooks.afterEach) { + return hooks.afterEach(); + } +}); diff --git a/lib/frameworks/jasmine.js b/lib/frameworks/jasmine.js index 0ecd95f9c..49750ae15 100644 --- a/lib/frameworks/jasmine.js +++ b/lib/frameworks/jasmine.js @@ -75,6 +75,9 @@ exports.run = function(runner, specs) { var reporter = new RunnerReporter(runner); jasmine.getEnv().addReporter(reporter); + // Add hooks for afterEach + require('./setupAfterEach').setup(runner, specs); + // Filter specs to run based on jasmineNodeOpts.grep and jasmineNodeOpts.invert. jasmine.getEnv().specFilter = function(spec) { var grepMatch = !jasmineNodeOpts || diff --git a/lib/frameworks/mocha.js b/lib/frameworks/mocha.js index 8b65007de..ea9bc65c5 100644 --- a/lib/frameworks/mocha.js +++ b/lib/frameworks/mocha.js @@ -12,6 +12,9 @@ exports.run = function(runner, specs) { var Mocha = require('mocha'), mocha = new Mocha(runner.getConfig().mochaOpts); + // Add hooks for afterEach + require('./setupAfterEach').setup(runner, specs); + var deferred = q.defer(); // Mocha doesn't set up the ui until the pre-require event, so diff --git a/lib/frameworks/setupAfterEach.js b/lib/frameworks/setupAfterEach.js new file mode 100644 index 000000000..98be559d3 --- /dev/null +++ b/lib/frameworks/setupAfterEach.js @@ -0,0 +1,29 @@ +/** + * Setup afterEach hook for jasmine/mocha tests. + * + * One of the main purposes of this file is to give `__protractor_internal_afterEach_setup_spec.js` + * a place to look up `runner.afterEach` at runtime without using globals. + * This file needs to be separate from `__protractor_internal_afterEach_setup_spec.js` so that that + * file is not prematurely executed. + */ + +var path = require('path'); + +// Queried by `protractor_internal_afterEach_setup_spec.js` for the `afterEach` hook +var hooks = { + afterEach: null +}; + +exports.hooks = hooks; + +/** + * Setup `runner.afterEach` to be called after every spec. + * + * @param {Runner} runner The current Protractor Runner. + * @param {Array} specs Array of Directory Path Strings. Must be a reference to the same array + * instance used by the framework + */ +exports.setup = function(runner, specs) { + hooks.afterEach = runner.afterEach.bind(runner); + specs.push(path.resolve(__dirname, '__protractor_internal_afterEach_setup_spec.js')); +}; diff --git a/scripts/test.js b/scripts/test.js index 13aa58646..b2f8421cd 100755 --- a/scripts/test.js +++ b/scripts/test.js @@ -76,7 +76,7 @@ executor.addCommandlineTest('node built/cli.js spec/errorTest/timeoutConf.js') message: 'Timeout - Async callback was not invoked within timeout ' + 'specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.' }) - .expectTestDuration(0, 100); + .expectTestDuration(0, 1000); executor.addCommandlineTest('node built/cli.js spec/errorTest/afterLaunchChangesExitCodeConf.js') .expectExitCode(11)