jest-community / jest-extended

Additional Jest matchers 🃏💪
https://jest-extended.jestcommunity.dev/
MIT License
2.32k stars 223 forks source link

Property 'toStrictEqual' does not exist on type 'Matchers<any>' #230

Open maplion opened 4 years ago

maplion commented 4 years ago

Bug

Relevant code or config

module.exports = {
    globals: {
        'ts-jest': {
            tsConfig: './src/tsconfig.spec.json',
            stringifyContentPathRegex: '\\.html$',
            astTransformers: [
                require.resolve('jest-preset-angular/InlineHtmlStripStylesTransformer')
            ]
        },
    },
    setupFilesAfterEnv: [
        '<rootDir>/node_modules/@angular-builders/jest/dist/jest-config/setup.js',
        'jest-extended',
    ],
    transform: {
        '^.+\\.(ts|js|html)$': 'ts-jest'
    },
    testMatch: [
        '**/__tests__/**/*.+(ts|js)?(x)',
        '**/+(*.)+(spec).+(ts|js)?(x)'
    ],
    testEnvironment: 'jest-environment-jsdom-thirteen',
    moduleFileExtensions: ['ts', 'js', 'html'],
    moduleNameMapper: {
        "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/test/mocks/files-mock.js",
        "\\.(css|scss)$": "<rootDir>/test/mocks/styles-mock.js",
        '^core-js/es6/(.*)': '<rootDir>/././node_modules/core-js/es/$1',
        '^core-js/es7/reflect': '<rootDir>/././node_modules/core-js/proposals/reflect-metadata',
        '^@core/(.*)': '<rootDir>/./src/app/core/$1',
        '^@shared/(.*)': '<rootDir>/./src/app/shared/$1',
        '^@feature/(.*)': '<rootDir>/./src/app/feature/$1',
        '^@test/(.*)': '<rootDir>/./src/app/feature/$1'
    },
    transformIgnorePatterns: ['node_modules/(?!@ngrx)'],
    collectCoverageFrom: [
        '{src|projects}/**/{src|lib}/**/*.{ts}',
        '!{src|projects}/*.{ts}',
        '!{src|projects}/**/*.{js}',
        '!{src|projects}/**/environments/*.{ts}',
        '!{src|projects}/**/model/*.{ts}',
        '!{src|projects}/**/*.module.{ts}',
        '!{src|projects}/**/*public_api.{ts}',
        '!{src|projects}/**/main.{ts}',
        '!{src|projects}/**/polyfills.{ts}',
        '!{src|projects}/**/*index.{ts}',
        '!{src|projects}/**/*test.{ts}',
        '!{src|projects}/**/*.enum.{ts}',
        '!{src|projects}/**/*.state.{ts}',
        '!{src|projects}/**/*.entity.{ts}',
        '!**/**e2e/**/*.{ts}',
        '!**/node_modules/**',
        '!**/vendor/**',
    ],
    moduleFileExtensions: ['ts', 'tsx', 'js', 'json'],
    testPathIgnorePatterns: ['/node_modules/', '/dist/', 'src/app/*.{js}'],
    testResultsProcessor: 'jest-sonar-reporter',
    snapshotSerializers: [
        'jest-preset-angular/AngularSnapshotSerializer.js',
        'jest-preset-angular/HTMLCommentSerializer.js'
    ]
};

What you did: I am trying to run a test utilizing a jest-extended matcher function (i.e. toStrictEqual).

What happened (please provide anything you think will help): I cannot get the compiler to recognize jest-extended matchers, no matter what I seem to try; for example: - error TS2339: Property 'toStrictEqual' does not exist on type 'Matchers'.

I have followed everything in the README and here https://github.com/jest-community/jest-extended/issues/172 in order to get the typings to work and they finally seem to work, but I still can't get the compiler to recognize the matcher functions.

Reproduction repository (if possible):

N/A

What am I missing?

Possibly related to https://github.com/jest-community/jest-extended/issues/171 and https://github.com/jest-community/jest-extended/issues/189 which seem to have been ignored.

sebranly commented 4 years ago

Are you sure toStrictEqual comes from jest-extended? I haven't taken a good look at it but it seems to come from jest directly:

An issue was created: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/36285

maplion commented 4 years ago

@sebranly I will feel sheepish if that is the case, but none of my jest-extended matchers seem to work. (I think) all of my Jest functions both give intellisense and are recognized by the compiler.

sebranly commented 4 years ago

Update - I won't confirm it in https://github.com/DefinitelyTyped/DefinitelyTyped/issues/36285 until I get it working but actually I'm pretty confident the 24.0.18 package is updated enough to give typings for toStrictEqual. In my node_modules/@types/jest/index.d.ts file, I literally see:

        /**
         * Use to test that objects have the same types as well as structure.
         */
        toStrictEqual(expected: {}): R;
sebranly commented 4 years ago

I think you have two separate issues:

maplion commented 4 years ago

@sebranly I see what you mean, but I can't even get it to show up in intellisense at all unless I do this at the top of my spec file:

/// <reference path='../../../../test/typings/jest-extended.d.ts' />

That has "import 'jest-extended';" in it and that's when expect('blah').toStrictEqual and other matchers show up; otherwise they are not there and I just have the normal expect('blah').toBe, etc. So I can get the typings in with the janky reference relative path method, but I can't get the typings successfully working in any global way (have tried everything I could find on the web in that regard), but even if I get the typings in, the compiler doesn't recognize them, even though my jest config matches the examples.

jjcv2304 commented 4 years ago

Any updates on this? I'm using "ts-jest": "^26.1.0", and also having same issue

fider commented 4 years ago

@jjcv2304 I just added global to jest config in package.json

{
  "jest": {
    "preset": "ts-jest",
    "testEnvironment": "node",
    "setupFilesAfterEnv": [
      "jest-extended",
      "./tests/tests-setup.ts" // includes import 'jest-extended'
    ],
    "globals": { // this did the job
      "ts-jest": {
        "tsConfig": "tsconfig.json"
      }
    },
    ...
}
tony commented 3 years ago

189

Does anyone here have @types/jasmine in their package.json / or lockfiles?

I have a monorepo and this came up for me and it happened around the time I tried to satisfy a dependency with angular (which uses jasmine by default it appears).

tony commented 3 years ago

If you have any other type declaration file that's competing against jasmine's, even if it's an implicit dependency, please check for it. Use your lockfiles, clear your node_modules and reinstall, etc.

Potentially related: https://github.com/nestjs/nest/issues/4223

Broken with toStrictEqual error: https://github.com/tony/cv/runs/1636818626

Fixed here: https://github.com/tony/cv/runs/1636861741 https://github.com/tony/cv/commit/8b23c802b0c0c1584b930814a552bffecd785b34

I fixed it with:

diff --git a/package.json b/package.json
index 142c0704..079f2b47 100644
--- a/package.json
+++ b/package.json
@@ -14,6 +14,7 @@
     "github": "yarn workspace @tony/cv-scripts run github"
   },
   "workspaces": {
+    "nohoist": ["**/@types/jest", "**/@types/jasmine"],
     "packages": [
       "packages/*"
     ]

Then rerun npm / yarn, and if that doesn't work clear node_modules/

If this does work, please note it.

But this doesn't necessarily solve all issues w/ this. So let's not close this issue until we're sure it's the only manifestation.