question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't run Typescript tests in Cypress when I have @cypress/webpack-batteries-included-preprocessor enabled

See original GitHub issue

Current behavior

Because of this issue we have to use @cypress/webpack-batteries-included-preprocessor enabled in our setup. This looks like this: // cypress/plugins/

import webpackPreprocessor from '@cypress/webpack-batteries-included-preprocessor'
module.exports = (on: (arg0: string, arg1: any) => void, config: { env: { ENVIRONMENT: any } }) => {
    // We need to enable webpack preprocessing because Webpack doesn't play nice with
    // Swagger generated clients: https://github.com/cypress-io/cypress/issues/8373
    const options = webpackPreprocessor.defaultOptions

    options.webpackOptions.module.rules.push({
        parser: { amd: false },
    })

    on('file:preprocessor', webpackPreprocessor(options))
    // other unrelated stuff

}

We have 2 tsconfig.json files in the project. If we don’t set tsconfig.json in the root folder, then we get :

TypeError: Cannot read property 'defaultOptions' of undefined
    at module.exports (/Users/andrew/Work/New10/Testing/cypress/cypress/plugins/index.ts:43:41)

Root tsconfig.json looks like this :

{
  "extends": "@new10com/new10-standard/tsconfig.base.json",
  "include": [
    "cypress/**/*.ts",
    "cypress/**/.*.ts",
    "index.d.ts"
  ],
  "exclude": [".vscode", "deploy", "node_modules/**"],
  "compilerOptions": {
    "baseUrl": ".",
    "allowJs": true,
    "esModuleInterop": true,
    "types": ["cypress"],
    "typeRoots": ["./typings", "./node_modules/@types"],
    "rootDir": ".",
    "skipLibCheck": true,
    "noEmit": false,
    "lib": ["esnext.asynciterable", "esnext", "dom", "es5"]
  }
}

Cypress tsconfig.json looks like this:

// cypress/tsconfig.json

{
  "extends": "../tsconfig.json",
  "include": ["./**/*.ts", "./**/*.*.ts", "@cypress/webpack-batteries-included-preprocessor"],
  "exclude": [],
  "compilerOptions": {
    "rootDir": "../",
    "types": ["cypress"],
    "lib": ["es2015", "dom"],
    "isolatedModules": false,
    "allowJs": true,
    "noEmit": true
  }
}

If we have webpack preprocessor enabled, then at the run of typescript spec we get this error:

Error: You are attempting to run a TypeScript file, but do not have TypeScript installed. Ensure you have 'typescript' installed to enable TypeScript support.

The file: /Users/andrew/Work/New10/Testing/cypress/cypress/integration/regular/smokeTests/signup-user.spec.ts
    at Object.handler (/Users/andrew/Work/New10/Testing/cypress/node_modules/@cypress/webpack-batteries-included-preprocessor/index.js:110:29)
    at invoke (/Users/andrew/Library/Caches/Cypress/6.8.0/Cypress.app/Contents/Resources/app/packages/server/lib/plugins/child/run_plugins.js:22:16)
    at /Users/andrew/Library/Caches/Cypress/6.8.0/Cypress.app/Contents/Resources/app/packages/server/lib/plugins/util.js:41:14
    at tryCatcher (/Users/andrew/Library/Caches/Cypress/6.8.0/Cypress.app/Contents/Resources/app/packages/server/node_modules/bluebird/js/release/util.js:16:23)
    at Function.Promise.attempt.Promise.try (/Users/andrew/Library/Caches/Cypress/6.8.0/Cypress.app/Contents/Resources/app/packages/server/node_modules/bluebird/js/release/method.js:39:29)
    at Object.wrapChildPromise (/Users/andrew/Library/Caches/Cypress/6.8.0/Cypress.app/Contents/Resources/app/packages/server/lib/plugins/util.js:40:23)
    at Object.wrap (/Users/andrew/Library/Caches/Cypress/6.8.0/Cypress.app/Contents/Resources/app/packages/server/lib/plugins/child/preprocessor.js:28:8)
    at execute (/Users/andrew/Library/Caches/Cypress/6.8.0/Cypress.app/Contents/Resources/app/packages/server/lib/plugins/child/run_plugins.js:119:27)
    at EventEmitter.<anonymous> (/Users/andrew/Library/Caches/Cypress/6.8.0/Cypress.app/Contents/Resources/app/packages/server/lib/plugins/child/run_plugins.js:202:5)
    at EventEmitter.emit (events.js:315:20)
    at process.<anonymous> (/Users/andrew/Library/Caches/Cypress/6.8.0/Cypress.app/Contents/Resources/app/packages/server/lib/plugins/util.js:19:22)
    at process.emit (events.js:315:20)
    at process.emit (/Users/andrew/Library/Caches/Cypress/6.8.0/Cypress.app/Contents/Resources/app/packages/server/node_modules/source-map-support/source-map-support.js:495:21)
    at emit (internal/child_process.js:876:12)
    at processTicksAndRejections (internal/process/task_queues.js:85:21)

But js specs that use our generated API client work just fine.

If will disable preprocessing, then typescript spec runs just fine, but specs with generated API client will fail just like described in issue

Desired behavior

We would love convert our specs into typescript and use our generated client along with it.

Test code to reproduce

I will try to create some small demo project, but meanwhile wanted to ask if you know where at least to look for a problem ? I couldn’t find anywhere in the google this kind of error.

Versions

Cypress 6.8.0 Mac OS Chrome 89

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
chrisbreidingcommented, Apr 13, 2021

Trying this out on the repro, I found there’s a bug in the batteries-included preprocessor that prevents TypeScript support from working when passing in the default options, so what I posted in the above comment won’t work. I’ll work on a fix and publish a new version of @cypress/webpack-batteries-included-preprocessor.

0reactions
azakordonetscommented, Apr 28, 2021

That actually worked, thanks you 🙇‍♂️

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript - Cypress Documentation
Cypress ships with official type declarations for TypeScript. This allows you to write your tests in TypeScript. Install TypeScript To use TypeScript.
Read more >
Use TypeScript With Cypress - Gleb Bahmutov
Step by step tutorial how to set up TypeScript support in Cypress using WebPack bundler. How to write Cypress.io end-to-end tests in TypeScript...
Read more >
Testing Web Components with Cypress and TypeScript
Since we have a TypeScript-based project, we can consider the new cypress directory as the root of a new sub-project for End-to-End testing....
Read more >
Cypress causing type errors in jest assertions - Stack Overflow
I installed Cypress yesterday, and now I'm getting Typescript errors on all my jest matchers: Property 'toEqual' doesn't exist on type ' ...
Read more >
Starting with TypeScript in Cypress - Filip Hric
So before even running my test, I get a feedback that the way I'm using this function is not valid. This helps tremendously...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found