ngneat / svg-icon

👻 A lightweight library that makes it easier to use SVG icons in your Angular Application
https://netbasal.com
MIT License
257 stars 35 forks source link

Unit tests fail after updating to v5 and v6 #114

Closed tspmelo closed 1 year ago

tspmelo commented 1 year ago

I'm submitting a...


[x] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report  
[ ] Performance issue
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request
[ ] Other... Please describe:

Current behavior

The app works fine and svg icons are displayed when I serve it or build it, but when I try to run unit tests of components that use svg-icon it always fails

Expected behavior

Tests should work as before.

Minimal reproduction of the problem with instructions

  1. Create an empty Nx angular project
  2. configure svg-icon
  3. Add a new angular library
  4. add a new component that uses svg-icon
  5. run unit tests for that component

You can check an example here: https://github.com/tspmelo/ngneat-svg-icon-issue/pull/1

I noticed that unit tests are using fesm2015/ngneat-svg-icon.mjs and if I move the code from line 83 to line 65, tests start working again:

const SVG_ICONS_CONFIG = new InjectionToken('SVG_ICONS_CONFIG', {
    providedIn: 'root',
    factory() {
        return {};
    },
});

SvgIconRegistry.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.2.8", ngImport: i0, type: SvgIconRegistry, deps: [{ token: SVG_ICONS_CONFIG }], target: i0.ɵɵFactoryTarget.Injectable });
SvgIconRegistry.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "14.2.8", ngImport: i0, type: SvgIconRegistry, providedIn: 'root' });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.2.8", ngImport: i0, type: SvgIconRegistry, decorators: [{
            type: Injectable,
            args: [{ providedIn: 'root' }]
        }], ctorParameters: function () {
        return [{ type: undefined, decorators: [{
                        type: Inject,
                        args: [SVG_ICONS_CONFIG]
                    }] }];
    } });

Environment


Angular version: 15.0.0 (also happens with latest 14.x.x)


For Tooling issues:
- Node version: 16.17.1 and  18.12.0 
- Platform:  Linux 

Others:
- Jest: 28.1.1

Biffyn commented 1 year ago

+1 for this

NetanelBasal commented 1 year ago

I'm not sure what's the issue. We're using ng-packager to build it. Try to ask in the angular cli repo, please.

sabo-marius commented 1 year ago

I can also confirm this behaviour, unit tests are failing becuase if accesing SVG_ICONS_CONFIG before initialization:

ReferenceError: Cannot access 'SVG_ICONS_CONFIG' before initialization

@tspmelo 's seems to fix the problem (thanks for that).