Skip to content

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

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

Open
wants to merge 4 commits 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

This comment was marked as resolved.

@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

.prettierrc Outdated
@@ -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?

Copy link
Author

Choose a reason for hiding this comment

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

Why? Eslint enforces to 4

@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

This comment was marked as resolved.

@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

@MillerSvt

This comment was marked as resolved.

@MillerSvt MillerSvt force-pushed the add-automock-transformer branch 4 times, most recently from cb2c11a to 2df83d3 Compare July 1, 2025 11:04
@MillerSvt
Copy link
Author

@ahnpnl Since we have single cjs bundle, any imports (that replaced to require functions by compiler) from @angular/* packages is fails with error:

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

You can check this by run e2e esm tests. Do you have any idea how to deal with that?

@MillerSvt MillerSvt marked this pull request as ready for review July 1, 2025 11:57
@MillerSvt MillerSvt requested a review from ahnpnl July 1, 2025 11:57
@MillerSvt MillerSvt force-pushed the add-automock-transformer branch from 2df83d3 to af8d680 Compare July 2, 2025 17:24
@MillerSvt MillerSvt force-pushed the add-automock-transformer branch from af8d680 to 010d2a5 Compare July 3, 2025 01:02
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