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

Import a function throws type error exception #7036

Closed
Rumpelstinsk opened this issue Apr 16, 2020 · 6 comments · Fixed by #7072
Closed

Import a function throws type error exception #7036

Rumpelstinsk opened this issue Apr 16, 2020 · 6 comments · Fixed by #7072
Assignees
Labels
type: regression A bug that didn't appear until a specific Cy version release v4.4.0 🐛 Issue present since 4.4.0

Comments

@Rumpelstinsk
Copy link

Rumpelstinsk commented Apr 16, 2020

Current behavior:

Importing a function from a diferent file throws a type error

Test code to reproduce

I want to test a page wich uses basic auth. So I write this line on a test:

cy.visit("/my_url", { auth:{
      username: Cypress.env("BASICAUTH_USERNAME"),
      password: Cypress.env("BASICAUTH_USERNAME")
    } });

This line works with no problem. However, I want to test other pages with basic auth too. So, in order to avoid repeating this object I made a function to retrieve it:

//Auth.js"
export default {
  basicAuth: () => {
    return {
      username: Cypress.env("BASICAUTH_USERNAME"),
      password: Cypress.env("BASICAUTH_USERNAME")
    };
  }
};

Then I import it on my original test file:

import { basicAuth } from "./Auth";

//other lines here:

cy.visit("/my_url", { auth:basicAuth() });

Then when I run the code, I get a Type error on console:

> TypeError: Auth_1.basicAuth is not a function
      at xxxxxxx (#URL-Here#/__cypress/tests?p=test/app.spec.js-193:74:26)
      at Context.<anonymous> (#URL-Here#/__cypress/tests?p=test/app.spec.js-193:29:13)

Versions

Cypress/included:4.4.0 from https://github.com/cypress-io/cypress-docker-images

@jennifer-shehane
Copy link
Member

jennifer-shehane commented Apr 17, 2020

This is a regression introduced in 4.4.0. I'm able to replicate this with the code below:

cypress/integration/spec.js

import { getUrl } from "./url"

it('tests', () => {
  console.log(getUrl())        // prints fine
  cy.visit({ url: getUrl() })  // errors
})

cypress/integration/url.js

export default {
  getUrl: () => {
    return "https://example.cypress.io"
  }
}

4.3.0

Screen Shot 2020-04-17 at 4 57 57 PM

4.4.0

The console.log() prints the correct string after calling the function. But Cypress throws an error, notice the attribution to url_1.js, which is...not the name of the file?

Screen Shot 2020-04-17 at 4 55 41 PM

@jennifer-shehane jennifer-shehane added v4.4.0 🐛 Issue present since 4.4.0 type: regression A bug that didn't appear until a specific Cy version release labels Apr 17, 2020
@cypress-bot cypress-bot bot added the stage: needs investigating Someone from Cypress needs to look at this label Apr 17, 2020
@jennifer-shehane
Copy link
Member

This fails with function being called in the options object, may be related to cloning the options? #6459

The only other big change in 4.4.0 was the TypeScript support. #5906

Any of these PRs could effect this @sainthkh?

@sainthkh
Copy link
Contributor

I'll check it out.

@sainthkh sainthkh self-assigned this Apr 17, 2020
@cypress-bot cypress-bot bot added stage: work in progress There is an open PR for this issue [WIP] stage: needs review The PR code is done & tested, needs review and removed stage: needs investigating Someone from Cypress needs to look at this stage: work in progress There is an open PR for this issue [WIP] labels Apr 20, 2020
@sainthkh
Copy link
Contributor

sainthkh commented Apr 20, 2020

The cause was the typo in the code I wrote. Because of that, typescript was used to parse your file. To make it work, the code had to be written like import { default as { getUrl } } from './url'. It's not intuitive.

#7072 fixes the problem and babel parser will be used when you didn't include typescript inside your project like before.

If you're trying to adopt TypeScript, your code won't work. It is recommended to use named export instead like below:

export const basicAuth = () => {
    return {
      username: Cypress.env("BASICAUTH_USERNAME"),
      password: Cypress.env("BASICAUTH_USERNAME")
    };
  }

@cypress-bot cypress-bot bot added the stage: pending release There is a closed PR for this issue label Apr 20, 2020
@cypress-bot
Copy link
Contributor

cypress-bot bot commented Apr 20, 2020

The code for this is done in cypress-io/cypress#7072, but has yet to be released.
We'll update this issue and reference the changelog when it's released.

@cypress-bot cypress-bot bot removed the stage: needs review The PR code is done & tested, needs review label Apr 20, 2020
@cypress-bot
Copy link
Contributor

cypress-bot bot commented Apr 20, 2020

Released in 4.4.1.

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

@cypress-bot cypress-bot bot removed the stage: pending release There is a closed PR for this issue label Apr 20, 2020
@cypress-bot cypress-bot bot locked as resolved and limited conversation to collaborators Apr 20, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type: regression A bug that didn't appear until a specific Cy version release v4.4.0 🐛 Issue present since 4.4.0
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants