Skip to content

Commit

Permalink
Fix for cypress-io#7818 - cy.route swallows error message when fixtur…
Browse files Browse the repository at this point in the history
…e does not exist
  • Loading branch information
Przemek Suchodolski committed Jul 16, 2020
1 parent c2dc757 commit 035678b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
4 changes: 4 additions & 0 deletions packages/driver/cypress/integration/commands/fixtures_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ describe('src/cy/commands/fixtures', () => {
cy.fixture('example').should('deep.eq', { example: true })
})

it('works with null.json', () => {
cy.fixture('null.json').should('equal', null)
})

it('can read a fixture without extension with multiple dots in the name', () => {
cy.fixture('foo.bar.baz').should('deep.eq', { quux: 'quuz' })
})
Expand Down
11 changes: 11 additions & 0 deletions packages/driver/cypress/integration/commands/xhr_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2122,6 +2122,17 @@ describe('src/cy/commands/xhr', () => {
.wrap({ foo: 'bar' }).as('foo')
.route(/foo/, '@bar')
})

it('throws when fixture cannot be found', () => {
cy.on('fail', (err) => {
expect(err.message).to.contains('A fixture file could not be found at any of the following paths:')
})

cy.route(/foo/, 'fx:NOT_EXISTING_FILE_FIXTURE')
cy.window().then((win) => {
win.$.get('/foo')
})
})
})

describe('.log', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/driver/src/cy/commands/fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ module.exports = (Commands, Cypress, cy, state, config) => {
return Cypress.backend('get:fixture', fixture, _.pick(options, 'encoding'))
.timeout(timeout)
.then((response) => {
const err = response.__error
const err = response?.__error

if (err) {
return $errUtils.throwErr(err)
Expand Down
9 changes: 8 additions & 1 deletion packages/driver/src/cy/commands/xhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ const startXhrServer = (cy, state, config) => {
},

onFixtureError (xhr, err) {
err = $errUtils.cypressErr(err)
err = $errUtils.cypressErr({ message: err })

return this.onError(xhr, err)
},
Expand Down Expand Up @@ -471,6 +471,13 @@ module.exports = (Commands, Cypress, cy, state, config) => {
})
}

// look ahead to see if fixture exists
const fixturesRe = /^(fx:|fixture:)/

if (hasResponse && fixturesRe.test(options.response)) {
return cy.fixture(options.response.replace(fixturesRe, '')).then(() => route())
}

return route()
}

Expand Down

0 comments on commit 035678b

Please sign in to comment.