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

feat: add automatic mock for angular entities (#2908) #2953

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

MillerSvt
Copy link

@MillerSvt MillerSvt commented Jan 30, 2025

Summary

Currently, to mock Angular components, services, pipes, directives, and modules, we need to manually create stubs for them. This PR introduces a new way to automatically mock them. The process can be simplified as follows:

jest.mock('./component');

Test plan

Does this PR introduce a breaking change?

  • Yes
  • No

TODO


Closes: #2908

@MillerSvt MillerSvt force-pushed the add-automock-transformer branch from 26f4a39 to 39f1670 Compare January 30, 2025 12:32
@MillerSvt
Copy link
Author

@ahnpnl Hi! I've been working on it, and I was wondering what the best way to test it would be. Do you think I should test it in the examples projects or somewhere else? I think it should tested for every supported Angular version.

@ahnpnl
Copy link
Collaborator

ahnpnl commented Jan 30, 2025

@ahnpnl Hi! I've been working on it, and I was wondering what the best way to test it would be. Do you think I should test it in the examples projects or somewhere else? I think it should tested for every supported Angular version.

Hi, about this, I think we should have

@@ -1,7 +1,7 @@
semi: true
printWidth: 120
singleQuote: true
tabWidth: 2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you pls revert this?

@@ -111,7 +111,7 @@
"github-files-fetcher": "^1.6.0",
"glob": "^10.4.5",
"husky": "^9.1.7",
"jest": "^29.7.0",
"jest": "^30.0.0-alpha.7",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is only available in Jest 30, would you pls change merge target branch of this repo to next?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Besides, there are other Jest deps which need to be bumped together too. I think making a separate commit or PR to support Jest 30 first is necessary before this change

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I described it in the TODO section.

@@ -1,5 +1,6 @@
require('zone.js');
require('zone.js/testing');
require('../mock-transformer');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not everyone would want this enabled by default, we should provide a flag to enable it via options provided in

const setupZoneTestEnv = (options) => {

Copy link
Collaborator

@ahnpnl ahnpnl Jan 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Besides, this file is only for testbed setup, we shouldn’t add mock here. It’s better that we provide a utility function which can be imported in a Jest setup file which is owned by the users.

For example

// my-setup-test-file.ts
import { setupAutoMockForComp } from ‘jest-preset-angular’

Or any names which fit the purpose :)

@@ -1,3 +1,5 @@
require('../mock-transformer');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same comment like we have in zone

@ahnpnl
Copy link
Collaborator

ahnpnl commented Jan 30, 2025

Another thing is about documentation for this new feature, potentially a new file in https://github.com/thymikee/jest-preset-angular/tree/main/website/docs/guides

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 this pull request may close these issues.

[Feature]: Exclude ɵfac ɵprov properties from auto mock
2 participants