diff --git a/packages/driver/src/cypress/runner.js b/packages/driver/src/cypress/runner.js index 1a3cc85fa1c5..56e057e2341c 100644 --- a/packages/driver/src/cypress/runner.js +++ b/packages/driver/src/cypress/runner.js @@ -693,11 +693,19 @@ const _runnerListeners = (_runner, Cypress, _emissions, getTestById, getTest, se delete hook.ctx.currentTest } - // set the hook's id from the test because - // hooks do not have their own id, their - // commands need to grouped with the test - // and we can only associate them by this id - const test = getTestFromHookOrFindTest(hook) + let test = getTest() + + // https://github.com/cypress-io/cypress/issues/9162 + // In https://github.com/cypress-io/cypress/issues/8113, getTest() call was removed to handle skip() properly. + // But it caused tests to hang when there's a failure in before(). + // That's why getTest() is revived and checks if the state is 'pending'. + if (!test || test.state === 'pending') { + // set the hook's id from the test because + // hooks do not have their own id, their + // commands need to grouped with the test + // and we can only associate them by this id + test = getTestFromHookOrFindTest(hook) + } if (!test) { // we couldn't find a test to run with this hook diff --git a/packages/runner/cypress/fixtures/issues/issue-9162.js b/packages/runner/cypress/fixtures/issues/issue-9162.js new file mode 100644 index 000000000000..6353de515c26 --- /dev/null +++ b/packages/runner/cypress/fixtures/issues/issue-9162.js @@ -0,0 +1,9 @@ +describe('should not hang when an error is thrown in a before() block', () => { + before(() => { + expect(true).to.be.false + }) + + after(() => {}) + it('has a test') + it('has another test') +}) diff --git a/packages/runner/cypress/integration/issues/issue-9162.js b/packages/runner/cypress/integration/issues/issue-9162.js new file mode 100644 index 000000000000..784944182c21 --- /dev/null +++ b/packages/runner/cypress/integration/issues/issue-9162.js @@ -0,0 +1,15 @@ +const helpers = require('../../support/helpers') + +const { createCypress } = helpers +const { runIsolatedCypress } = createCypress() + +// https://github.com/cypress-io/cypress/issues/9162 +describe('issue 9162', () => { + beforeEach(function () { + return runIsolatedCypress(`cypress/fixtures/issues/issue-9162.js`) + }) + + it('tests does not hang even if there is a fail in before().', function () { + cy.contains('expected true to be false') + }) +})