swc-node icon indicating copy to clipboard operation
swc-node copied to clipboard

Support angular

Open splincode opened this issue 3 years ago • 25 comments

Support angular

splincode avatar Oct 16 '21 22:10 splincode

+1 and for now we have this image

infodusha avatar Oct 17 '21 21:10 infodusha

Try this config:

// jest.config.js

module.exports = {
  transform: {
    '^.+\\.(t|j)sx?$': [
      '@swc-node/jest',

      // configuration
      {
        dynamicImport: true,
        experimentalDecorators: true,
        emitDecoratorMetadata: true,
      },
    ],
  },
}

Brooooooklyn avatar Oct 18 '21 03:10 Brooooooklyn

Sure, that fixes decorator issues but here is the next one image

infodusha avatar Oct 19 '21 18:10 infodusha

After some investigation, I found that Angular need custom transform for Angular template , which is not supported by SWC now.

Brooooooklyn avatar Oct 27 '21 03:10 Brooooooklyn

Angular doesn't support lazy loading/resolving templates at runtime in node.js. Thats why https://yarnpkg.com/package/jest-preset-angular replaces templateUrl: './foo.html with template: '<!-- foo.html contents -->' at transform time.

sod avatar Nov 05 '21 12:11 sod

Same for NestJS.

rottmann avatar Nov 18 '21 15:11 rottmann

Is there any news on this?

DasGandlaf avatar Dec 10 '22 06:12 DasGandlaf

Any plans on supporting angular?

thais-molica avatar Feb 13 '23 19:02 thais-molica

@mgechev Hi! Do you (or your team) have a plan?

splincode avatar Feb 13 '23 19:02 splincode

You already can do component testing with jest and swc. I wrote the https://gist.github.com/sod/07078bb93a292f0a9ebf5a246b34828d file. Import it in your entry file you configured in jest via setupFilesAfterEnv.

The file overwrites the Component annotation of the @angular/core module and rewrites styleUrls: string[] into styles: string[] and templateUrl: string into template: string at runtime, and uses require(...) to resolve those urls from the context of the component.

Be sure to add jest transformers for html and css (or e.g. scss if you use sass) so jest can handle those requires. The html transformer can return the html as plaintext, angular JIT will compile it at runtime. The sass transformer though has to compile in jest as angular JIT only understands css.

sod avatar Feb 14 '23 15:02 sod

@sod Do you have project example on github?)

splincode avatar Feb 14 '23 15:02 splincode

sure, here you go @splincode - https://github.com/sod/angular-jest-swc - follow instructions in readme

CleanShot 2023-02-14 at 17 24 53@2x

sod avatar Feb 14 '23 16:02 sod

In our project with your setup

image image image

splincode avatar Feb 14 '23 19:02 splincode

@splincode - which Angular version are you running? Not sure if that's the case, but @sod's example uses Angular 15.

hudsontavares avatar Jun 02 '23 12:06 hudsontavares

So @sod's example actually works, but the thing with mocking component is sometimes you can get an error:

Error: NG0202: This constructor is not compatible with Angular Dependency Injection because its dependency at index 0 of the parameter list is invalid.
This can happen if the dependency type is a primitive like a string or if an ancestor of this class is missing an Angular decorator.

Please check that 1) the type for the parameter at index 0 is correct and 2) the correct Angular decorators are defined for this class and its ancestors.

My guess here is that is not actually a proper way - to mock @Component directive. Would be great if there were a thing that does this templateUrl to template transform not in the runtime but somewhere in transformations.

infodusha avatar Jul 10 '23 19:07 infodusha

+1

Henry-Brinkman avatar Jul 24 '23 14:07 Henry-Brinkman

+1

koraxos avatar Jul 31 '23 10:07 koraxos

+1

haskelcurry avatar Dec 19 '23 18:12 haskelcurry

+1

vojtesaak avatar Jan 13 '24 20:01 vojtesaak

@sod's example seems to be working. Only thing wanted to know how do i pass tsconfig.json path which i pass like below while using jest-preset-angular

transform: { '^.+.(ts|mjs|js|html)$': [ 'jest-preset-angular', { tsconfig: '<rootDir>/tsconfig.spec.json', stringifyContentPathRegex: '\.(html|svg)$', isolatedModules: true } ] },

hardikpatel043 avatar Jan 30 '24 11:01 hardikpatel043

+1

michal-materowski avatar Feb 06 '24 07:02 michal-materowski

+1

Trojan13 avatar May 14 '24 17:05 Trojan13