toBeTruthy does not exist on type Assertion

The toBeTruthy does not exist error message is likely occurring in a TypeScript project that uses the Cypress testing framework. The compiler cannot find the toBeTruthy property on the Assertion type because it was renamed.

The exact error you must be getting in your project: Property ‘toBeTruthy’ does not exist on type ‘Assertion’ in the spec files, for example, in the code using .toBeTruthy();

  it('should create', () => {
    expect(component).toBeTruthy();
  });

Solution:

To resolve this problem, add “exclude”: [“cypress”] to your tsconfig.json file. This tells the TypeScript compiler to ignore the cypress folder and its contents when type-checking your code. This will allow you to use non-Typed definitions or custom definitions from the Cypress testing framework without causing errors.

tsconfig.json contains options that the TypeScript compiler uses to transpile TypeScript code into JavaScript. It’s used to configure the behavior of the TypeScript compiler, including how it type-checks your code and how it outputs compiled JavaScript code.

Go to the tsconfig.json file and add the “exclude”: [“cypress”], tag at the end:

 "exclude": ["cypress"]

The tsconfig.json file will look like this:

/* To learn more about this file see: https://angular.io/config/tsconfig. */
{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
......
  },
  "angularCompilerOptions": {
....
  },
    "exclude": ["cypress"], 
}

By excluding the Cypress folder, the TypeScript compiler will not raise type-related errors for any code within that folder. This allows for more flexible testing with Cypress and ensures that type errors do not interfere with the testing process.

Next, modify the cypress/tsconfig.json like this:

{
  "include": ["../node_modules/cypress", "**/*.ts"],
  "compilerOptions": {
    "sourceMap": false,
    "target": "ES2022",
    "module": "es2020",
    "types": ["cypress"]
  }
}

This configuration file configures the TypeScript compiler for your Cypress tests. The types property is set to [“cypress”] to include the type definitions for Cypress, which will allow you to use its testing syntax without encountering errors.

Adding the above code will fix the Property ‘toBeTruthy’ does not exist on the type ‘Assertion’ issue.

The exclude tag in a tsconfig.json file is used to specify a list of directories or files that the TypeScript compiler will ignore. The compiler will not perform type checking or type inference on code in these directories or files, and any type errors will be ignored.

You can opt out of type checking for a directory by adding the exclude property to tsconfig.json. This will allow you to write tests in JavaScript while still taking advantage of TypeScript’s other benefits for the rest of your project.