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

Error on XHR stub if resonse json only contains a number or boolean #4899

Closed
ambervdberg opened this issue Aug 1, 2019 · 5 comments · Fixed by #9107
Closed

Error on XHR stub if resonse json only contains a number or boolean #4899

ambervdberg opened this issue Aug 1, 2019 · 5 comments · Fixed by #9107
Labels
good first issue Good for newcomers pkg/server This is due to an issue in the packages/server directory type: bug

Comments

@ambervdberg
Copy link

ambervdberg commented Aug 1, 2019

Current behavior:

When a fixture.json ONLY contains a number or a boolean, cypress throws the following error:

CypressError: TypeError: First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object.
    at Function.Buffer.from (buffer.js:202:9)
    at Object.<anonymous> 

Desired behavior:

For Cypress to accept fixtures that contain a number or a boolean only.

Steps to reproduce: (app code and test code)

Fixtures:

content of my-number.json:

26

content of my-bool.json:

true

Tests:

 // fails:
    it('accepts a number', () => {
        cy.server();
        cy.route({method: 'GET', url: '/getnumber', response: 'fixture:my-number.json'}).as('mynumber');
        cy.visit('/');
        cy.wait('@mynumber').its('responseBody').should('be', 26);
    });

    // fails:
    it('accepts a boolean', () => {
        cy.server();
        cy.route({method: 'GET', url: '/getbool', response: 'fixture:my-bool.json'}).as('mybool');
        cy.visit('/');
        cy.wait('@mybool').its('responseBody').should('be', true);
    });

repo: https://github.com/amberjs/cypress-test-tiny

Versions

Cypress 3.2.0 - 3.4.1
Chrome

@ambervdberg
Copy link
Author

Any update on this?

@jennifer-shehane
Copy link
Member

I can verify this behavior. This is a bug. This error only occurs when you use the fixtures: shorthand within the cy.route() property.

num.json

26

spec.js

it('accepts a number in cy.fixture', () => {
  cy.server()
  cy.fixture('num.json').as('number')
  cy.visit('https://example.cypress.io/commands/files')
  cy.route('GET', 'comments/*', '@number').as('getNumber')
  cy.get('.fixture-btn').click()

  cy.wait('@getNumber').its('responseBody')
    .should('eq', 26) // passes
});

it('fails when a number in fixture:', () => {
  cy.server()
  cy.visit('https://example.cypress.io/commands/files')
  cy.route('GET', 'comments/*', 'fixture:num.json').as('getNumber')
  cy.get('.fixture-btn').click() // fails after click

  cy.wait('@getNumber').its('responseBody')
    .should('eq', 26)
});

Screen Shot 2020-01-06 at 5 29 03 PM

This error used to look slightly different: in 3.0.0 for example.

Screen Shot 2020-01-06 at 5 31 53 PM

Cause / Fix

This is being thrown from this line of code:

https://github.com/cypress-io/cypress/blob/issue-2869-config-changes/packages/server/lib/controllers/xhrs.coffee#L58:L58

Buffer.from only accepts a String when encoding is passed as explained here: https://nodejs.org/api/buffer.html#buffer_class_method_buffer_from_string_encoding

@sainthkh added a fix for if the JSON file is null, but this does not handle cases where the JSON file is valid and a non-string (like being a Number of Boolean). #5562

Workaround

Switch to using cy.fixture() syntax with .as() alias as shown above.

@cypress-bot cypress-bot bot added the stage: ready for work The issue is reproducible and in scope label Jan 6, 2020
@jennifer-shehane jennifer-shehane added good first issue Good for newcomers pkg/server This is due to an issue in the packages/server directory type: bug labels Jan 6, 2020
@mrmodise
Copy link
Contributor

mrmodise commented Nov 5, 2020

PR on the way @jennifer-shehane

@cypress-bot cypress-bot bot added stage: work in progress stage: needs review The PR code is done & tested, needs review and removed stage: ready for work The issue is reproducible and in scope stage: work in progress labels Nov 5, 2020
@cypress-bot
Copy link
Contributor

cypress-bot bot commented Nov 9, 2020

The code for this is done in cypress-io/cypress#9107, 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 9, 2020
@cypress-bot
Copy link
Contributor

cypress-bot bot commented Nov 9, 2020

Released in 5.6.0.

This comment thread has been locked. If you are still experiencing this issue after upgrading to
Cypress v5.6.0, please open a new issue.

@cypress-bot cypress-bot bot locked as resolved and limited conversation to collaborators Nov 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
good first issue Good for newcomers pkg/server This is due to an issue in the packages/server directory type: bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants