Cypress: TypeError: Cannot read property 'replace' of undefined" at Object.appendErrMsg

Created on 3 May 2018  路  10Comments  路  Source: cypress-io/cypress

Current behavior:

This is a weird issue that is isolated to a single machine.

If I try run a test file with more than one or two tests, I get the following:
screen shot 2018-05-03 at 10 59 12 am

The stack trace:

TypeError: Cannot read property 'replace' of undefined

Because this error occurred during a 'before each' hook we are skipping the remaining tests in the current suite: 'bulk actions'
    at Object.appendErrMsg (http://localhost:3000/__cypress/runner/cypress_runner.js:66723:23)
    at Runner.<anonymous> (http://localhost:3000/__cypress/runner/cypress_runner.js:65768:20)
    at Runner.EventEmitter.emit (http://localhost:3000/__cypress/runner/cypress_runner.js:28665:13)
    at Runner.fail (http://localhost:3000/__cypress/runner/cypress_runner.js:65066:17)
    at Runner.failHook (http://localhost:3000/__cypress/runner/cypress_runner.js:33083:8)
    at http://localhost:3000/__cypress/runner/cypress_runner.js:33128:16
    at http://localhost:3000/__cypress/runner/cypress_runner.js:65918:11
From previous event:
    at onNext (http://localhost:3000/__cypress/runner/cypress_runner.js:65917:57)
    at done (http://localhost:3000/__cypress/runner/cypress_runner.js:32741:5)
    at Hook.Runnable.run (http://localhost:3000/__cypress/runner/cypress_runner.js:32776:5)
    at http://localhost:3000/__cypress/runner/cypress_runner.js:65936:28
From previous event:
    at Object.onRunnableRun (http://localhost:3000/__cypress/runner/cypress_runner.js:65935:20)
    at $Cypress.action (http://localhost:3000/__cypress/runner/cypress_runner.js:61399:51)
    at Hook.Runnable.run (http://localhost:3000/__cypress/runner/cypress_runner.js:65075:20)
    at next (http://localhost:3000/__cypress/runner/cypress_runner.js:33119:10)
    at http://localhost:3000/__cypress/runner/cypress_runner.js:33141:5
    at timeslice (http://localhost:3000/__cypress/runner/cypress_runner.js:28382:27)

The test spec file like:

describe('tests', () => {
  beforeEach(() => {
    cy.authenticatedVisit(
      'some_url'
    )
  })
  it('draws a box', () => {})
  it('potato', () => {})
  it('draws a circle', () => {})
  it('draws nothing', () => {})
})

and the authenticatedVisit command is:

Cypress.Commands.add('authenticatedVisit', url => {
  cy.visit(url, {
    onBeforeLoad() {
      window.localStorage.setItem(
        'authRefreshToken',
        userJames.authRefreshToken
      )
      window.localStorage.setItem('authAccessToken', null) //it needs this field to have an entry
    },
  })
})

It works perfectly fine on other machines with the exact same set up.

It works fine running a single test at a time - but I'm wondering if anyone else has experienced this before?

Happy to provide any extra info if I can!

Versions

Both cypress 2.1.0 and the previous version. Mac OS 10.13.4, chrome 66.0.3359.139

Most helpful comment

I'm getting a similar error in 3.1.5 when trying to cy.setCookie in a before: TypeError: Cannot read property 'replace' of undefined" at Object.appendErrMsg

Inserting that on-fail debugger as you showed yields err = Error and runnable.trace.message = "Error: done() called multiple times" at Hook.Runnable

All 10 comments

Can you try adding this to your suite and seeing what the err is here?

Cypress.on('fail', (err, runnable) => {
  debugger
})

Hi @jennifer-shehane!
Sorry for the late response. I couldn't work out where that event listener should go?
I tried it in the spec file at the top, in the describe, in the beforeEach and inside a test.
I also tried it in the console, as well as using cy instead of Cypress.

@Bkucera I'm not sure if you noticed, but I'm using onBeforeLoad as an option for cy.visit(). If I try to use your suggestion of cy.window() to yield the window inside the onBeforeLoad, I get the attached error, which makes sense, I guess.

screen shot 2018-05-15 at 3 58 32 pm

@James-E-Adams I believe you are supposed to put the event listener outside of the describe.

Cypress.on('fail', (err, runnable) => {
  debugger
})

describe('tests', () => {
  beforeEach(() => {
    cy.authenticatedVisit(
    'some_url'
    )
  })
  it('draws a box', () => {})
  it('potato', () => {})
  it('draws a circle', () => {})
  it('draws nothing', () => {})
})

Also Bkucera's suggestion's was to call cy.window() after cy.visit(), not inside onBeforeLoad(). Which means that your authenticatedVisit command would look like this:

Cypress.Commands.add('authenticatedVisit', url => {
  cy.visit(url);
  cy.window().then(window => {
    window.localStorage.setItem(
      'authRefreshToken',
      userJames.authRefreshToken
    );
    window.localStorage.setItem('authAccessToken', null) //it needs this field to have an entry
  });
});

I'm not sure if that would solve your problem but I hope that gives you a working alternative.

I'm getting a similar error in 3.1.5 when trying to cy.setCookie in a before: TypeError: Cannot read property 'replace' of undefined" at Object.appendErrMsg

Inserting that on-fail debugger as you showed yields err = Error and runnable.trace.message = "Error: done() called multiple times" at Hook.Runnable

This issue should be fixed as part of this refactor: https://github.com/cypress-io/cypress/issues/3762

Similar to @echurilov I'm unable to call cy.setCookie() within a beforeEach:

image

Within the Cypress window I see the error TypeError: Cannot read property 'replace' of undefined -

image

Resolved my issue by trimming the response. Whitespace was being included in the body.

The code for this is done in cypress-io/cypress#5313, but has yet to be released.
We'll update this issue and reference the changelog when it's released.

Released in 3.6.1.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jennifer-shehane picture jennifer-shehane  路  3Comments

simonhaenisch picture simonhaenisch  路  3Comments

brian-mann picture brian-mann  路  3Comments

scottcrowe picture scottcrowe  路  3Comments

carloscheddar picture carloscheddar  路  3Comments