Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
James-E-Adams opened this issue May 3, 2018 · 10 comments · Fixed by #5313
Closed

Comments

@James-E-Adams
Copy link

James-E-Adams commented May 3, 2018

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

@jennifer-shehane
Copy link
Member

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

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

@jennifer-shehane jennifer-shehane added the stage: needs information Not enough info to reproduce the issue label May 3, 2018
@James-E-Adams
Copy link
Author

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.

@James-E-Adams
Copy link
Author

@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

@xiankai
Copy link

xiankai commented May 24, 2018

@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.

@echurilov
Copy link

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

@jennifer-shehane jennifer-shehane changed the title Can't run more than one test without it failing TypeError: Cannot read property 'replace' of undefined" at Object.appendErrMsg Apr 25, 2019
@jennifer-shehane
Copy link
Member

This issue should be fixed as part of this refactor: #3762

@cypress-bot cypress-bot bot added stage: ready for work The issue is reproducible and in scope and removed stage: needs information Not enough info to reproduce the issue labels Apr 25, 2019
@eddif
Copy link

eddif commented Jun 11, 2019

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

@eddif
Copy link

eddif commented Jul 22, 2019

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

@cypress-bot
Copy link
Contributor

cypress-bot bot commented Nov 8, 2019

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.

@cypress-bot cypress-bot bot added stage: pending release and removed stage: needs review The PR code is done & tested, needs review labels Nov 8, 2019
@cypress-bot
Copy link
Contributor

cypress-bot bot commented Nov 8, 2019

Released in 3.6.1.

@cypress-io cypress-io locked as resolved and limited conversation to collaborators Dec 24, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants