Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

toMatchSnapshot fails to work #14

Closed
ronlawrence3 opened this issue Aug 1, 2019 · 6 comments · Fixed by #41
Closed

toMatchSnapshot fails to work #14

ronlawrence3 opened this issue Aug 1, 2019 · 6 comments · Fixed by #41

Comments

@ronlawrence3
Copy link

Describe the bug
Adding a toMatchSnapshot() assertion in a test on a fresh angular 6 project gets an error.

To Reproduce
Steps to reproduce the behavior:
With @angular/cli@6:

ng new testproj
cd testproj
ng add @briebug/jest-schematic
# edit app.component.spec.ts and add a toMatchSnapshot() assertion
npm test

Error message is:

$ npm test

> [email protected] test /tmp/fooa
> jest

 FAIL  src/app/app.component.spec.ts
  ● Test suite failed to run

    TypeScript diagnostics (customize using `[jest-config].globals.ts-jest.diagnostics` option):
    src/app/app.component.spec.ts:22:23 - error TS2339: Property 'toMatchSnapshot' does not exist on type 'Matchers<any>'.

    22     expect(app.title).toMatchSnapshot();
                             ~~~~~~~~~~~~~~~

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        4.592s
Ran all test suites.
npm ERR! Test failed.  See above for more details.

Expected behavior
I expected these to work once project is converted to jest

Desktop (please complete the following information):

  • OS: Linux
  • Node version: 8.11.3
  • NPM or Yarn version NPM
  • App type Angular CLI
  • Angular version ^6.1.0
@ronlawrence3
Copy link
Author

ronlawrence3 commented Aug 1, 2019

I think the key to fixing this is to replace jasmine with jest in the tsconfig.spec.json file, and add @types/jest. (and possibly remove @types/jasmine from the package.json) I will see if I have time to submit a patch if this works..

@rgunczer
Copy link

rgunczer commented Oct 9, 2019

@ronlawrence3 replacing jasmine with jest seems to work. However in VSCode TS still complains. Eg.
expect(service.getPersons).toBeInstanceOf(Function);
Property 'toBeInstanceOf' does not exist on type 'Matchers'.

@ronlawrence3
Copy link
Author

It looks like with a fresh project from the latest cli, and the latest schematic from this project, things seem to work for me now.

But as @rgunczer says, VScode still doesn't like it. if I add jest to the main tsconfig.json, it is ok with it, so vscode must be using that tsconfig.

@alcaidio
Copy link

alcaidio commented Jun 26, 2020

A quick fix :

const fixture = TestBed.createComponent(TodoComponent);
(<any>expect(fixture)).toMatchSnapshot();

you can create an helper const snap = (fixture: any): any => (<any>expect(fixture)).toMatchSnapshot();
And use it : snap(fixture);

schuchard added a commit that referenced this issue Sep 2, 2020
Add support single application projects and workspaces with apps and libraries. 

## Jest runner

Replace the direct reliance on [`jest-preset-angular`](https://github.com/thymikee/jest-preset-angular) and package json scripts in favor of [`angular-builders/jest`](https://github.com/just-jeb/angular-builders/tree/master/packages/jest). This provides the ability to run tests with `ng test`

This should address #30 #28 #22 #11 
It better positions us to support #1 

## Jest types

Update each projects `tsconfig.spec.json` to configure the `jest` types while removing `jasmine`

This should address #24 #14

## Other
Non-user facing changes 

- refactor development scripts from `package.json` to TS file
- create 2 sandboxes (single app and workspace with app and lib)
- test both sandboxes during CI
- add MIT license
- add prettier
@schuchard
Copy link
Collaborator

Please see if ng add @briebug/[email protected] solves the issue

@schuchard
Copy link
Collaborator

This should be fixed with the latest 3.0.0 release. Please reopen if not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants