ngneat / spectator

🦊 🚀 A Powerful Tool to Simplify Your Angular Tests
https://ngneat.github.io/spectator
MIT License
2.07k stars 178 forks source link

Jest Matchers gets corrupted when I install spectator #227

Closed undsoft closed 4 years ago

undsoft commented 4 years ago

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report  
[ ] Performance issue
[ ] Feature request
[ ] Documentation issue or request
[ x] Support request
[ ] Other... Please describe:

Current behavior

I've recently installed spectator and I've noticed that some methods in expect() stopped working. For example, toMatchObject and toMatchSnapshot are no longer working. I get errors like this:

Property 'toMatchObject' does not exist on type 'Matchers<{}>

They do work once I remove spectator.

I don't have anything jasmine related in my package.json. Here are the relevant parts:

    "@angular/compiler": "^7.2.4",
    "@angular/cli": "~7.2.1",
    "@angular/compiler-cli": "^7.2.4",
    "@angular/language-service": "^7.2.4",
    "@angular-builders/jest": "~7.4.4",
    "@angular-devkit/build-angular": "~0.13.8",
    "@types/jest": "~24.0.22",
    "jest": "~24.9.0",
    "jest-marbles": "~2.5.0",
    "jest-teamcity-reporter": "~0.9.0",
    "ng-mocks": "~8.1.0",
    "typescript": "~3.2.2"

So far I've tried downgrading jest and @types/jest versions and removing other packages. I'm running out of ideas on how to debug it.

undsoft commented 4 years ago

Additional piece of info. Doing:

    const t: JestMatchers<any> = expect(2);

errors with

Error:(5, 11) TS2322: Type 'Matchers' is not assignable to type 'JestMatchersShape<Matchers<void, any>, Matchers<Promise, any>>'. Type 'Matchers' is missing the following properties from type '{ resolves: AndNot<Matchers<Promise, any>>; rejects: AndNot<Matchers<Promise, any>>; }': resolves, rejects

but only if spectator is installed.

undsoft commented 4 years ago

I've traced the problem to @types/jasmine actually being installed by one of the packages. In my case it was ng-cli-pug-loader. I was able to remove this package.

Not sure why the problem didn't manifest before installing spectator.

NetanelBasal commented 4 years ago

It seems like you solved the issue. I'm closing it.

tom10271 commented 4 years ago

https://jestjs.io/docs/en/api

import {describe, expect, it} from '@jest/globals' solves the problem

maxkuzmin commented 4 years ago

@NetanelBasal that is not always possible to get rid of jasmine typings. Protractor requires it, and it worked well with jest specs before installing spectator. The only way to handle this issue is to import @jest/globals in every spec which uses jest specific matchers as tom10271 suggested, but that is not very convenient.