3

I'm currently facing a strange behavior on my Angular (14) shallow rendering snapshots in unit tests (Jest 28).

Sometimes my snapshot tests are failing, depending if I run them individually or as bundle. The reason of failure is the difference between __ngContext__={[Function Number]} and __ngContext__="0".

Besides the fact, that I'm using the default snapshot-serializer shipped by "jest-preset-angular", "ng-mocks" is used to provide the module in a mocked way to render only shallow DOM snapshots.

Does anyone know how to remove the __ngContext__ from the mock-render, to avoid the differences while running the test standalone or in a bundle.

My test looks as follow:


import { MockBuilder, MockRender } from 'ng-mocks';


  describe('Snapshot Tests', () => {
    beforeEach(() =>
      MockBuilder(MyComponent, [
        MyModule
      ])
    );

    it('should create', () => {
      const fixture = MockRender(MyComponent, {});
      expect(fixture).toMatchSnapshot();
    });
  });
// Jest Snapshot v1

exports[`MyComponent Snapshot Tests should create`] = `
<mock-render
  __ngContext__={[Function Number]}
>
  <my-component>
    <h1>Test</h1>
  </my-component>
</mock-render>

and sometimes (if I run the test individually)

// Jest Snapshot v1

exports[`MyComponent Snapshot Tests should create`] = `
<mock-render
  __ngContext__="0"
>
  <my-component>
    <h1>Test</h1>
  </my-component>
</mock-render>

Package information

> npx envinfo --npmPackages 
@angular/core: ^14.0.2 => 14.0.2  
ng-mocks: ^14.0.1 => 14.0.1
jest: ^28.1.1 => 28.1.1 
jest-preset-angular: ^12.1.0 => 12.1.0 
ts-jest: ^28.0.5 => 28.0.5
...
N33D
  • 135
  • 1
  • 7
  • I have the same problem now, were you able to figure out an answer to this problem? – Triguna Sep 15 '22 at 13:29
  • @Triguna Not yet :/ – N33D Sep 16 '22 at 18:08
  • 1
    Hi all, can you create a min repo with the problem and submit an issue on github? https://github.com/help-me-mom/ng-mocks/issues/new/choose – satanTime Sep 18 '22 at 14:32
  • @N33D what I understood is, this happens due to an upgrade to angular versions. What fixed my problem was to revert any changed snapshots committed from the repo in the PR and then run `npm ci` locally, which should get newer versions of angular updates to node_modules, which fixed the issue. Post this it never asks to update snapshots. – Triguna Sep 30 '22 at 12:48
  • @satanTime: I created an issue here with a reproduction repository: https://github.com/help-me-mom/ng-mocks/issues/3811; repository: https://github.com/jones1008/ng-mocks-snapshot-test-bug – jones1008 Oct 12 '22 at 17:52

1 Answers1

2

It looks like a bug in either Angular or jest.

ngContext is an internal property for angular needs. it can be an index (number) or an object. The number support was added to improve performance and to reduce the number of objects for the same context on different nodes.

The index of a context starts from 0, and it looks like there is code in either Angular or jest, which considers 0 as false, and 1+ as true. In case of true, there is conversion from a primitive to an object of its type.

And this is why the very first test in the suite has __ngContext__="0", whereas all others (1+) have __ngContext__={[Function Number]}

The right way is to report an issue to the related framework to fix it. But it might take quite a while, especially for jest.

That's why I've implemented a permanent fix in ng-mocks: https://github.com/help-me-mom/ng-mocks/pull/3830, it will be released today.

satanTime
  • 12,631
  • 1
  • 25
  • 73