getsaf / shallow-render

Angular testing made easy with shallow rendering and easy mocking. https://getsaf.github.io/shallow-render
MIT License
273 stars 25 forks source link

Shallow does not resolve in a new Angular 14.2 project #230

Closed victoragung closed 1 year ago

victoragung commented 1 year ago

I created a new Angular 14.2 project and replaced karma with jest v28. I am able to get tests running using TestBed, however when I installed shallow-render@14 just running a simple test the Shallow object fails to resolve.

Here's a snapshot of the error

Cannot find module 'shallow-render' from 'src/app/app.component.spec.ts'

      1 | import { TestBed, async } from '@angular/core/testing';
      2 | import { AppComponent } from './app.component';
    > 3 | import {Shallow} from 'shallow-render'
        | ^
      4 | import { AppModule } from './app.module';
      5 | describe('AppComponent', () => {
      6 |   let shallow: Shallow<AppComponent>;

      at Resolver._throwModNotFoundError (node_modules/jest-resolve/build/resolver.js:425:11)
      at Object.<anonymous> (src/app/app.component.spec.ts:3:1)

We have another project on Angular 13 with jest28 and that seems fine.

victoragung commented 1 year ago

The fix for this is in an outstanding PR here: https://github.com/getsaf/shallow-render/pull/223

Timebutt commented 1 year ago

I'm seeing the same problem in an Angular 15 application, although the error message looks a bit different. Hoping this helps other people find this issue if they're experiencing the same problem.

/usr/src/app/node_modules/shallow-render/dist/index.d.ts:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export { Shallow } from './lib/shallow';
                                                                                      ^^^^^^
    SyntaxError: Unexpected token 'export'
       9 | import { MockSharedFeatureOrganisationModule } from '../libs/shared/feature-organisation/src/testing.index';
      10 | import { SharedFeatureOrganisationModule } from '../libs/shared/feature-organisation/src/index';
    > 11 | import { Shallow } from 'shallow-render';
         | ^
      12 |
      13 | import '@angular/localize/init';
      14 |
      at Runtime.createScriptFromCode (../../../node_modules/jest-runtime/build/index.js:1796:14)
      at Object.<anonymous> (../../../jest/setupJest.ts:11:1)

Thanks for your suggested fix @victoragung, I've patch-packaged it into my repo for now, until this fix gets merged.

getsaf commented 1 year ago

This should be fixed in v14.2.0 or v14.2.1, thanks for the PR @victoragung!

victoragung commented 1 year ago

Thank you! Credit should go to @develobrix :)