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

@MillerSvt
Copy link
Author

MillerSvt commented Feb 21, 2025

@ahnpnl I moved this to src/automocks, but I cant test it, because of getting error:

Jest encountered an unexpected token

    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

    By default "node_modules" folder is ignored by transformers.

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
     • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation, specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

If I add useESM option to jest-src.config.ts, and enable experimental vm modules then I get next error:

    Must use import to load ES Module: /Users/s.v.zaytsev/IdeaProjects/jest-preset-angular/node_modules/@angular/core/fesm2022/core.mjs

    > 1 | import { ɵɵdefineInjectable, ɵgetInjectableDef, InjectableType, Type } from '@angular/core';

But it already is import... If I add extensionsToTreatAsEsm: ['.ts'], I get next error:

    ReferenceError: exports is not defined

      1 | import { stubInjectable } from './stub-injectable';
    > 2 |
        | ^
      3 | describe('stubInjectable', () => {
      4 |     it('should stub root service', () => {

I cant understand whats wrong...

@ahnpnl
Copy link
Collaborator

ahnpnl commented Feb 28, 2025

@ahnpnl I moved this to src/automocks, but I cant test it, because of getting error:

Jest encountered an unexpected token

    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

    By default "node_modules" folder is ignored by transformers.

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
     • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation, specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

If I add useESM option to jest-src.config.ts, and enable experimental vm modules then I get next error:

    Must use import to load ES Module: /Users/s.v.zaytsev/IdeaProjects/jest-preset-angular/node_modules/@angular/core/fesm2022/core.mjs

    > 1 | import { ɵɵdefineInjectable, ɵgetInjectableDef, InjectableType, Type } from '@angular/core';

But it already is import... If I add extensionsToTreatAsEsm: ['.ts'], I get next error:

    ReferenceError: exports is not defined

      1 | import { stubInjectable } from './stub-injectable';
    > 2 |
        | ^
      3 | describe('stubInjectable', () => {
      4 |     it('should stub root service', () => {

I cant understand whats wrong...

Which test command did you run? Did you try with yarn test-cjs?

Copy link

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