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

Type 'Promise<any>' is not assignable to type 'CanReturnChainable' in Cypress #7807

Closed
stevenvachon opened this issue Jun 25, 2020 · 8 comments · Fixed by #8501
Closed

Type 'Promise<any>' is not assignable to type 'CanReturnChainable' in Cypress #7807

stevenvachon opened this issue Jun 25, 2020 · 8 comments · Fixed by #8501
Labels
topic: typescript type: regression A bug that didn't appear until a specific Cy version release v4.9.0 🐛 Issue present since 4.9.0

Comments

@stevenvachon
Copy link

stevenvachon commented Jun 25, 2020

Current behavior:

Having updated from Cypress 4.8, TypeScript will now not compile my support code. #435 was not insightful.

Desired behavior:

Compile my TypeScript. Either I'm doing something wrong or there is a bug.

Test code to reproduce

Cypress.Commands.overwrite(command, (originalFn, ...args) => {
  const origVal = originalFn(...args);
  return new Promise(resolve => {
    setTimeout(() => resolve(origVal), 1000);
  });
});

It fails similarly with Cypress.Promise as well. I'd like to avoid cy.wrap() unless necessary.

Versions

Cypress 4.9.0
macOS 10.15.5
Chrome 83

@jennifer-shehane
Copy link
Member

@stevenvachon Hey, can you provide some more info?

  • You're adding this code to a support/index.ts file?
  • How are you running your tests? (cypress open, cypress run)?
  • Do you have any preprocessors in your plugins?
  • Do you have a tsconfig file defind?
  • What is the error you are seeing?

I can't recreate this just adding this code to a support/index.ts file as you've described.

@jennifer-shehane jennifer-shehane added the stage: needs information Not enough info to reproduce the issue label Jun 26, 2020
@0x80
Copy link

0x80 commented Jun 29, 2020

@jennifer-shehane I'm seeing similar behavior.

  • Yes, it's code added to support/index.ts
  • It's the compiler that complains. Weirdly enough the tests still run.
  • No preprocessor in plugins
  • Tsconfig file below
Cypress.Commands.add(
  "createReview",
  (payload: Partial<CreateReviewPayload>) => {
    const review = cleanObject<CreateReviewPayload>({
      title: payload.title || `[cy-test]`,
      description: "This is a review created for test purposes",
      tagIds: [],
      industryId: "__no_industry",
      type: "self-review",
      accountId: testAccountId,
      ...payload,
    });

    return functions
      .httpsCallable("createReview")(review)
      .then((result) => {
        const reviewId = result.data.reviewId as string;
        return firestore
          .collection("reviews")
          .doc(reviewId)
          .get()
          .then((snap) => ({
            id: reviewId,
            data: snap.data() as Review,
          }));
      });
  },
);

Error:

Argument of type '(payload: Partial<CreateReviewPayload>) => Promise<{ id: string; data: Review; }>' is not assignable to parameter of type '(...args: any[]) => CanReturnChainable'.
  Type 'Promise<{ id: string; data: Review; }>' is not assignable to type 'CanReturnChainable'.
    Type 'Promise<{ id: string; data: Review; }>' is missing the following properties from type 'Chainable<any>': and, as, blur, check, and 89 more.ts(2345)
{
  "compilerOptions": {
    "esModuleInterop": true,
    "jsx": "preserve",
    "lib": ["dom", "esnext"],
    "types": ["cypress", "node"],
    "module": "esnext",
    "moduleResolution": "node",
    "noEmit": true,
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "preserveConstEnums": true,
    "removeComments": false,
    "skipLibCheck": true,
    "checkJs": false,
    "sourceMap": true,
    "strict": true,
    "target": "es2018",
    "allowJs": false,
    "forceConsistentCasingInFileNames": true,
    "resolveJsonModule": true,
    "baseUrl": ".",
    "paths": {
      "/*": ["./src/*"]
    }
  }
}

For me this issue also started when upgrading from 4.8 to 4.9

@araujobarret
Copy link

araujobarret commented Aug 11, 2020

@jennifer-shehane @0x80 I faced the same situation here, but with MailSlurp service:
// command.ts

import { MailSlurp } from 'mailslurp-client';

const getMailSlurpInstance = (apiKey: string) => {
  return new MailSlurp({ apiKey });
};

Cypress.Commands.add('createInbox', (apiKey: string) => {
  return getMailSlurpInstance(apiKey).createInbox();
});

This expected type from the command CanReturnChainable it is more a blocker than a helper in my case, just downgraded to version 4.8.0 as a workaround from now.

@jennifer-shehane jennifer-shehane added topic: typescript type: regression A bug that didn't appear until a specific Cy version release v4.9.0 🐛 Issue present since 4.9.0 labels Aug 12, 2020
@lansana
Copy link

lansana commented Aug 20, 2020

This issue also appears in 5.0.0

@sainthkh
Copy link
Contributor

sainthkh commented Sep 4, 2020

Confirmed. Trying to find the fix.

@cypress-bot cypress-bot bot added stage: work in progress There is an open PR for this issue [WIP] and removed stage: needs information Not enough info to reproduce the issue labels Sep 4, 2020
@0x80
Copy link

0x80 commented Sep 4, 2020

I worked around it by returning my promise prefixed with cy.window().then(() => myAsyncFunction()). I'm not using the window result but the return value will therefor get the chainable type.

@cypress-bot cypress-bot bot added stage: pending release There is a closed PR for this issue and removed stage: work in progress There is an open PR for this issue [WIP] labels Sep 9, 2020
@cypress-bot
Copy link
Contributor

cypress-bot bot commented Sep 9, 2020

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

@cypress-bot
Copy link
Contributor

cypress-bot bot commented Sep 15, 2020

Released in 5.2.0.

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

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

Successfully merging a pull request may close this issue.

6 participants