Skip to content
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

[Bug] Testing: TypeError: getComputedStyle(...).getPropertyValue is not a function #1505

Closed
mcblum opened this issue May 1, 2018 · 9 comments
Labels

Comments

@mcblum
Copy link

mcblum commented May 1, 2018

I'm submitting a...

  • Bug report

Current behavior

I'm using Jest to run our tests and we're seeing an error when the InputsModule is included for testing. Here's the error:

 TypeError: getComputedStyle(...).getPropertyValue is not a function

      80 |         fixture = TestBed.createComponent(SchemaFieldPropertiesEditorComponent);
      81 |         component = fixture.componentInstance;
    > 82 |         fixture.detectChanges();
      83 |     });
      84 |
      85 |     it("should create", () => {

and a bit of the stack trace:

at computedProp (node_modules/@progress/kendo-angular-resize-sensor/dist/npm/resize-sensor.component.js:12:41)
      at ResizeSensorComponent.Object.<anonymous>.ResizeSensorComponent.ngAfterViewChecked (node_modules/@progress/kendo-angular-resize-sensor/dist/npm/resize-sensor.component.js:69:13)
      at callProviderLifecycles (node_modules/packages/core/esm5/src/view/provider.js:597:2)
      at callElementProvidersLifecycles (node_modules/packages/core/esm5/src/view/provider.js:561:12)
      at callLifecycleHooksChildrenFirst (node_modules/packages/core/esm5/src/view/provider.js:544:2)

Here's the testbed we're using:

 TestBed.configureTestingModule({
                declarations: [
                    SchemaFieldPropertiesEditorComponent,
                    NmSwitchComponent
                ],
                imports: [
                    FormsModule,
                    BrowserModule,
                    BrowserAnimationsModule,
                    MatDialogModule,
                    RouterTestingModule,
                    MatMenuModule,
                    ReactiveFormsModule,
                    ConfirmationDialogsModule,
                    InputsModule
                ],

System:

  • TypeScript version: 2.4
  • Node version: 9
  • Platform: Mac
@mcblum
Copy link
Author

mcblum commented May 1, 2018

Just a quick update, if I remove fixture.detectChanges(); from the test, it passes.

@tsvetomir
Copy link
Member

The error can be worked around by mocking getPropertyValue in your setup file:

Object.defineProperty(window, 'getComputedStyle', {
    value: () => ({
        getPropertyValue: (prop) => {
            return '';
        }
    })
});

We may add feature detection if this is a common problem in customers tests. For now, we prefer to keep the code free of JSDOM-specific workarounds.

@mcblum
Copy link
Author

mcblum commented May 2, 2018

@tsvetomir thank you! I added it to setupJest.ts and all is working. I Googled for days on this one - hopefully someone else will find this if they need it.

@mcblum mcblum closed this as completed May 2, 2018
@jamesmart77
Copy link

Thank you @tsvetomir! This just made it really easy!

@brandonpittman
Copy link

AFAICT, the mock isn't even necessary anymore.

@tsvetomir
Copy link
Member

@brandonpittman thanks for pointing this out. The default JSDOM implementation seems to work fine in the current version.

@murilodevkapaz
Copy link

The error can be worked around by mocking getPropertyValue in your setup file:

Object.defineProperty(window, 'getComputedStyle', {
    value: () => ({
        getPropertyValue: (prop) => {
            return '';
        }
    })
});

We may add feature detection if this is a common problem in customers tests. For now, we prefer to keep the code free of JSDOM-specific workarounds.

Worked for me! But I have a question: How do I mock more than once, with multiples values?

@tsvetomir
Copy link
Member

@murilodevkapaz you won't be able to identify the element with the property syntax. Maybe overriding the function would help:

window.getComputedStyle = (element) => {
    // test for element here
    
    return {
        getPropertyValue: (prop) => {
            // and for prop here
            return '';
        }
    };
};

@wizarion
Copy link

The error can be worked around by mocking getPropertyValue in your setup file:

Object.defineProperty(window, 'getComputedStyle', {
    value: () => ({
        getPropertyValue: (prop) => {
            return '';
        }
    })
});

We may add feature detection if this is a common problem in customers tests. For now, we prefer to keep the code free of JSDOM-specific workarounds.

Thank you very much, sir! Helped a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants