components icon indicating copy to clipboard operation
components copied to clipboard

A "Testing with Material Components" guide to demonstrate including core theme in test suite

Open willshowell opened this issue 9 years ago • 30 comments

Bug, feature request, or proposal:

Bug or missing docs

What is the expected behavior?

Tests shouldn't log warnings after following the Getting Started guide on a fresh cli generated app.

Current behavior / steps to reproduce

  1. Fresh cli app
  2. Follow the getting started guide
  3. Add an md-card component to AppComponent
  4. Run unit tests
  5. Resulting warning:
WARN: 'Could not find Angular Material core theme. Most Material components may not work as expected. For more info refer to the theming guide: https://material.angular.io/guide/theming'
Chrome 57.0.2987 (Mac OS X 10.12.4): Executed 0 of 3 SUCCESS (0 secs / 0 secs)
Chrome 57.0.2987 (Mac OS X 10.12.4): Executed 3 of 3 SUCCESS (0.226 secs / 0.199 secs)

Is there anything else we should know?

Modifying karma.conf.js to include a theme silenced the warning. This approach was borrowed from Material's karma config:

files: [
  { pattern: './src/test.ts', watched: false },
  { pattern: './node_modules/@angular/material/prebuilt-themes/indigo-pink.css' }
],

willshowell avatar Apr 12 '17 17:04 willshowell

@willshowell i have the same issue, i am with angular 4 and the newset beta 3 of angular material with a custom theme

ChenReuven avatar Apr 13 '17 04:04 ChenReuven

I am using the custom theme with ng v4, material beta.3 and all good. Got my custom theme in file assets/theme.scss:

@import '~@angular/material/_theming';
@import './my-palette.scss';
@include mat-core();
$primary: mat-palette($mat-saphire, 500);
$accent:  mat-palette($mat-saphire, A100);
$warn:    mat-palette($mat-orange, A200);
$theme: mat-light-theme($primary, $accent, $warn);
@include angular-material-theme($theme);

Importing that in file styles.scss: (if you using rebuild just import that instead)

@import '/assets/theme';

As I am using Angular CLI then in .angular-cli.json just doing so:

 "styles": [
        "styles.scss"
      ], 

And I am on Windows 7 x64, running node 7.8.0, karma 1.4.1 if it is making any difference.

From package.json:

    "karma": "~1.4.1",
    "karma-chrome-launcher": "~2.0.0",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^1.1.0",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "karma-remap-istanbul": "0.2.2", 

kuncevic avatar Apr 13 '17 04:04 kuncevic

I have this configuration:

app-theme.scss @include mat-core(); $app-primary: mat-palette($mat-blue); $app-accent: mat-palette($mat-amber); $app-warn: mat-palette($mat-red); $app-theme: mat-light-theme($app-primary, $app-accent, $app-warn); @include angular-material-theme($app-theme);

angular-cli.json "styles": [ "styles.css", "app-theme.scss" ],

style.css i have all fonts application configuration

ChenReuven avatar Apr 13 '17 04:04 ChenReuven

you are missing @import '~@angular/material/_theming'; in your app-theme.scss it is where mat-core(), and .mat-theme-loaded-marker, etc. is coming from

// Marker that is used to determine whether the user has added a theme to their page.
  .mat-theme-loaded-marker {
    display: none;
  }

kuncevic avatar Apr 13 '17 04:04 kuncevic

@kuncevic oops :), i just forgot to wrote it here in comment but it exist in code: @import '~@angular/material/theming'; on the top of the app-theme.scss

BTW i am not use the '_theming' but the 'theming'

Thanks 4 your help :)

ChenReuven avatar Apr 13 '17 05:04 ChenReuven

actually I just run ng t and I notice same warning, whoops...

kuncevic avatar Apr 13 '17 05:04 kuncevic

From what I understand, karma does not inject any css into your tests by default, but including a material component without a theme will trigger the warning.

In my original comment I showed how to inject one of the prebuilt themes into the test suite. What I don't know is if this is the ideal solution. I figure there should either be something in the docs about it, or the warning should stay silent during unit tests that import material modules.

Maybe @DevVersion or @crisbeto can weigh in?

willshowell avatar Apr 13 '17 13:04 willshowell

Another issue with receiving warnings- I've also noticed that the ".css.map" files seem to be missing in beta.3, so I keep getting thrown the following in Chrome:

DevTools failed to parse SourceMap: https://localhost:3000/@angular/material/indigo-pink.css.map

Is this the case, or am I just unable to find it? The ".css.map" files do not exist in the "/prebuilt-themes" directory.

LilSebastian5000 avatar Apr 13 '17 13:04 LilSebastian5000

I think the real solution here is that there should be guide specifically for testing w/ the Material components.

jelbourn avatar Apr 13 '17 23:04 jelbourn

Now in docs we have:

Angular Material comes prepackaged with several pre-built theme css files. These theme files also include all of the styles for core (styles common to all components), so you only have to include a single css file for Angular Material in your app.

I use:

  • @angular/core v4.0.0
  • @angular/angular-cli v1.0.0
  • @angular/material2 v2.0.0-beta.3
  • @angular/animations v4.0.2

When I import css-file in src/app/app.component.css:

@import '~@angular/material/prebuilt-themes/indigo-pink.css';

and run project via ng serve, I have same warn in console:

Could not find Angular Material core theme. Most Material components may not work as expected. For more info refer to the theming guide: https://material.angular.io/guide/theming

When I do

@import '~@angular/material/theming'
/* or @import '~@angular/material/_theming'*/

I have error "file not found".

When I include the style in src/index.html or in src/style.css all works without warn.

KostyaTretyak avatar Apr 15 '17 15:04 KostyaTretyak

Sorry that I haven't been following the discussion here. If you're testing something that contains a Material component, you definitely need to include the theme, otherwise the positioning and a few other things won't be accurate. To do that, you need to add it to your Karma config. You can see how we're doing it for our own tests here.

crisbeto avatar Apr 18 '17 13:04 crisbeto

The problem with the way you do it it's Chinese for somebody who is just started to work with karma testing. If I put just the line you highlighted then it's not working, but the file is quite different to be able to figure out what's the problem.

gajdot avatar Apr 19 '17 08:04 gajdot

thanks @crisbeto , i changed my karma.conf.js

    files: [
      {pattern: './config/karma-test-shim.js', watched: false},
            // Include a Material theme in the test suite.
      {pattern: './node_modules/@angular/material/prebuilt-themes/indigo-pink.css', included: true, watched: true},
    ],

it workd.

TaylorPzreal avatar Apr 20 '17 16:04 TaylorPzreal

How we can add custom themes .scss files with angular-cli sass processing?

elvirdolic avatar Apr 20 '17 18:04 elvirdolic

custom themes being written in sass throw an error when including it in the files array of karma.conf.js

Invalid character: '@'

farley911 avatar May 08 '17 19:05 farley911

Any update on this? I'm not able to properly test any component using material. The error is showing up and no material animation is working in the components.

gajdot avatar May 11 '17 08:05 gajdot

I think this document would be a good place to suggest reading the tests from the source for figuring out how to test different components. It would cut down on a number of issues like "how do I access x in my tests?" and "x works fine in my app, but my tests fail"

willshowell avatar Jun 20 '17 15:06 willshowell

Well it gives me no error, but I still couldn't figure out how to make the animations work in testing. It gives 0 error or warning, but no animation is happening in testing :(

gajdot avatar Jun 21 '17 11:06 gajdot

@crisbeto how do we target the sass file generated by angular-cli instead? I have all my theming files there, so I should use that instead.

herkulano avatar Jun 21 '17 11:06 herkulano

Had the same issue here and manage to fix it by importing any of the themes in the karma.config file.

Though I'm getting those warnings even that my tests are passing: Chrome 59.0.3071 (Mac OS X 10.12.5) LOG: '>>>>>>', '', 'Error: STACKTRACE TRACKING' Chrome 59.0.3071 (Mac OS X 10.12.5) LOG: '>>>>>>', ' at SEP_TAG (http://localhost:9876/karma_webpack/vendor.bundle.js:7538:37) []', ' at captureStackTraces (http://localhost:9876/karma_webpack/vendor.bundle.js:7538:37) []' Chrome 59.0.3071 (Mac OS X 10.12.5) LOG: '>>>>>>', ' at SEP_TAG (http://localhost:9876/karma_webpack/vendor.bundle.js:7538:37) []', ' at captureStackTraces (http://localhost:9876/karma_webpack/vendor.bundle.js:7538:37) []' Chrome 59.0.3071 (Mac OS X 10.12.5): Executed 0 of 28 SUCCESS (0 secs / 0 secs) Chrome 59.0.3071 (Mac OS X 10.12.5): Executed 28 of 28 SUCCESS (2.446 secs / 2.525 secs)

vapits avatar Jul 03 '17 08:07 vapits