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
przemuh committed Jul 27, 2020
1 parent 3ab9643 commit b919f68
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 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
13 changes: 13 additions & 0 deletions packages/driver/cypress/integration/commands/xhr_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2150,6 +2150,19 @@ describe('src/cy/commands/xhr', () => {
.wrap({ foo: 'bar' }).as('foo')
.route(/foo/, '@bar')
})

// https://github.com/cypress-io/cypress/issues/7818
it('throws when fixture cannot be found', (done) => {
cy.on('fail', (err) => {
expect(err.message).to.contains('A fixture file could not be found at any of the following paths:')
done()
})

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

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

if (err) {
return $errUtils.throwErr(err)
if (response && response.__error) {
return $errUtils.throwErr(response.__error)
}

// add the fixture to the cache
Expand Down
11 changes: 10 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,15 @@ module.exports = (Commands, Cypress, cy, state, config) => {
})
}

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

if (hasResponse && fixturesRe.test(options.response)) {
const fixtureName = options.response.replace(fixturesRe, '')

return cy.now('fixture', fixtureName).then(() => route())
}

return route()
}

Expand Down

0 comments on commit b919f68

Please sign in to comment.