1

I have the following two cypress fixtures.

cypress/fixtures/myFixture.js

import Something from "./somewhere";

...

cypress/fixtures/somewhere.ts

export default function Something(): number {
   return 1;
}

It's importing a Typescript file from JS, but it gives this error when trying to run the Cypress tests.

Error: Can't walk dependency graph: Cannot find module './somewhere' from '/path/myFixture.js'

I've tried adding tsconfig.json to the cypress folder like I've seen in many Stackoverflow answers, but the error is always that Cypress cannot find somewhere.js when it should be looking at somewhere.ts. I've also tried ts-loader to no avail and Cypress docs say it's supposed to support typescript OOTB (Cypress v10).

1 Answer 1

2

It looks like this question Import Typescript file in Javascript addresses the same issue (more generally than the Cypress context).

TypeScript cannot be executed from a browser / node.js environment directly. At first you have to transpile it into native javascript. To do this you will need to execute tsc.

So I guess you have to take the hard path to get your JS-based Cypress project set up for Typescript. Ultimately, Cypress does not transpile by default. You must add typescript to get anywhere.

If I were you, I would start clean with

  • install Typescript
  • install Cypress, it recognizes Typescript is installed and does the TS setup for you on first start-up - yarn cypress open or npx cypress open.
  • EXCEPT it fails to create a tsconfig, so will have to grab that from somewhere - probably the Cypress Real World App is an viable example.

Now you are working in typescript, copy across all the tests from the previous project.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.