{"action":"opened","issue":{"url":"https://api.github.com/repos/webdriverio/webdriverio/issues/1628","repository_url":"https://api.github.com/repos/webdriverio/webdriverio","labels_url":"https://api.github.com/repos/webdriverio/webdriverio/issues/1628/labels{/name}","comments_url":"https://api.github.com/repos/webdriverio/webdriverio/issues/1628/comments","events_url":"https://api.github.com/repos/webdriverio/webdriverio/issues/1628/events","html_url":"https://github.com/webdriverio/webdriverio/issues/1628","id":181005667,"number":1628,"title":"Failure in root-level Mocha hooks causes a TypeError","user":{"login":"msegado","id":1887854,"avatar_url":"https://avatars.githubusercontent.com/u/1887854?v=3","gravatar_id":"","url":"https://api.github.com/users/msegado","html_url":"https://github.com/msegado","followers_url":"https://api.github.com/users/msegado/followers","following_url":"https://api.github.com/users/msegado/following{/other_user}","gists_url":"https://api.github.com/users/msegado/gists{/gist_id}","starred_url":"https://api.github.com/users/msegado/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/msegado/subscriptions","organizations_url":"https://api.github.com/users/msegado/orgs","repos_url":"https://api.github.com/users/msegado/repos","events_url":"https://api.github.com/users/msegado/events{/privacy}","received_events_url":"https://api.github.com/users/msegado/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2016-10-04T21:09:58Z","updated_at":"2016-10-04T21:09:58Z","closed_at":null,"body":"_(Note, this probably isn't critical to fix since there's presumably an easy workaround [using WDIO's hooks instead of Mocha's], but it should be mentioned in the docs to avoid confusion when people try to use the Mocha hooks and things break. Thanks for the great free tool!)_\r\n\r\n## The problem\r\n\r\nMocha allows for [root-level hooks](https://mochajs.org/#root-level-hooks) which run before all test suites. A failure in one of these currently results in a TypeError:\r\n\r\n```\r\n/[...]/webdriverio/build/lib/utils/ReporterStats.js:403\r\n            testStats.state = 'fail';\r\n                            ^\r\n\r\nTypeError: Cannot set property 'state' of undefined\r\n    at ReporterStats.testFail (/[...]/webdriverio/build/lib/utils/ReporterStats.js:403:29)\r\n    at BaseReporter.<anonymous> (/[...]/webdriverio/build/lib/utils/BaseReporter.js:159:25)\r\n    at emitOne (events.js:96:13)\r\n    at BaseReporter.emit (events.js:188:7)\r\n    at BaseReporter.handleEvent (/[...]/webdriverio/build/lib/utils/BaseReporter.js:300:27)\r\n    at Launcher.messageHandler (/[...]/webdriverio/build/lib/launcher.js:594:28)\r\n    at emitTwo (events.js:106:13)\r\n    at ChildProcess.emit (events.js:191:7)\r\n    at process.nextTick (internal/child_process.js:744:12)\r\n    at _combinedTickCallback (internal/process/next_tick.js:67:7)\r\n```\r\n\r\nRelated:\r\n- #1460 (\"Errors in beforeSuite don't get correctly reported and prevent onComplete\")... see comments further down in particular and some of the attempted/reverted fixes for it.\r\n- webdriverio/wdio-mocha-framework@2e5e7642f41c5cdcdf73c1a42c0f1ce1e35b44be (\"make sure user hooks don't interfer with test process - closes webdriverio/webdriverio1460\"). I suspect this fixed the problem for WDIO's global hooks but not for Mocha's root-level hooks.\r\n- #1624 (\"ReporterStats throws a TypeError if nested test suites are in a single spec\"). May result from a different issue altogether, but it leads to the same error being thrown in ReporterStats.js so it is probably good to keep in mind when considering any fixes.\r\n\r\n## Environment\r\n\r\n* WebdriverIO version: **4.2.16**\r\n* Node.js version: **6.5.0**\r\n* [Standalone mode or wdio testrunner](http://webdriver.io/guide/getstarted/modes.html): **wdio**\r\n* if wdio testrunner, running synchronous or asynchronous tests: **sync**\r\n* Additional wdio packages used (if applicable):\r\n   - \"wdio-mocha-framework\": \"^0.4.3\",\r\n   - \"wdio-phantomjs-service\": \"^0.1.0\",\r\n   - \"wdio-spec-reporter\": \"0.0.3\",\r\n\r\n## Link to Selenium/WebdriverIO logs\r\n\r\nhttps://gist.github.com/msegado/af5efd3e7778d6e9eed09a9b9ccb97cf\r\n\r\n## Code To Reproduce Issue\r\ntest.js:\r\n```js\r\nbefore(() => {\r\n  browser.url('/login');\r\n  browser.waitForExist('#does-not-exist');  // cause a failure\r\n});\r\n\r\ndescribe('Some suite', () => {\r\n  it('does nothing', () => {});\r\n});\r\n```\r\n\r\nwdio.conf.js:\r\n```js\r\nexports.config = {\r\n  specs: [\r\n    './test.js',\r\n  ],\r\n  capabilities: [\r\n    { browserName: 'phantomjs' },\r\n  ],\r\n  sync: true,\r\n  logLevel: 'verbose',\r\n  coloredLogs: true,\r\n  baseUrl: 'http://localhost:8000',\r\n  waitforTimeout: 3000,\r\n  services: ['phantomjs'],\r\n  framework: 'mocha',\r\n  reporters: ['spec'],\r\n  mochaOpts: {\r\n    ui: 'bdd',\r\n  },\r\n};\r\n```\r\n"}}
{"actor":{"display_login":"msegado"}}