From 035678ba5e3c480c8fde5e860629a0c76e4d34d0 Mon Sep 17 00:00:00 2001 From: Przemek Suchodolski Date: Wed, 15 Jul 2020 21:55:52 +0200 Subject: [PATCH] Fix for #7818 - cy.route swallows error message when fixture does not exist --- .../cypress/integration/commands/fixtures_spec.js | 4 ++++ .../driver/cypress/integration/commands/xhr_spec.js | 11 +++++++++++ packages/driver/src/cy/commands/fixtures.js | 2 +- packages/driver/src/cy/commands/xhr.js | 9 ++++++++- 4 files changed, 24 insertions(+), 2 deletions(-) diff --git a/packages/driver/cypress/integration/commands/fixtures_spec.js b/packages/driver/cypress/integration/commands/fixtures_spec.js index f8a547ed411f..254afb7879b9 100644 --- a/packages/driver/cypress/integration/commands/fixtures_spec.js +++ b/packages/driver/cypress/integration/commands/fixtures_spec.js @@ -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' }) }) diff --git a/packages/driver/cypress/integration/commands/xhr_spec.js b/packages/driver/cypress/integration/commands/xhr_spec.js index 1c7cd99a12ca..c89508f8ac50 100644 --- a/packages/driver/cypress/integration/commands/xhr_spec.js +++ b/packages/driver/cypress/integration/commands/xhr_spec.js @@ -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', () => { diff --git a/packages/driver/src/cy/commands/fixtures.js b/packages/driver/src/cy/commands/fixtures.js index 6e088516f3e1..048b4d9faa92 100644 --- a/packages/driver/src/cy/commands/fixtures.js +++ b/packages/driver/src/cy/commands/fixtures.js @@ -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) diff --git a/packages/driver/src/cy/commands/xhr.js b/packages/driver/src/cy/commands/xhr.js index 7b00ed70a695..e022f31df594 100644 --- a/packages/driver/src/cy/commands/xhr.js +++ b/packages/driver/src/cy/commands/xhr.js @@ -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) }, @@ -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() }