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

I could not use MaterialModule #52

Closed
luke-emmental opened this issue Jun 20, 2017 · 20 comments
Closed

I could not use MaterialModule #52

luke-emmental opened this issue Jun 20, 2017 · 20 comments

Comments

@luke-emmental
Copy link

luke-emmental commented Jun 20, 2017

 console.warn node_modules/@angular/material/bundles/material.umd.js:146
    Current document does not have a doctype. This may cause some Angular Material components not to behave as expected.

  console.warn node_modules/@angular/material/bundles/material.umd.js:159
    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


ReferenceError: CSS is not defined

What should i do?

@thymikee
Copy link
Owner

You're probably hitting this: jestjs/jest#3299
I'll be implementing new testEnvironment for this preset, so I can keep that in mind and add support for doctype. If you don't have time, you can create your own testEnvironment (copy-paste existing one and adjust it as I commented in mentioned issue)

Use it in your jest config like:

{
  "testEnvironment": "path/to/your/custom_test_env.js"
}

@luke-emmental
Copy link
Author

thanks reply! It is harder than I thought. I'm trying

@thymikee
Copy link
Owner

Yea it's not a regular thing you do with Jest, but it's definitely possible to adjust :)

@luke-emmental
Copy link
Author

luke-emmental commented Jun 21, 2017

I try it. but not working
vmDom.js

const _util = require('jest-util');
const FakeTimers = _util.FakeTimers, installCommonGlobals = _util.installCommonGlobals;
const mock = require('jest-mock');
const jsdom = require('jsdom');
const path = require('path');
const fs = require('fs');
const process = require('process');


const maincss = fs.readFileSync(path.normalize(`${path.parse(__dirname).dir}/node_modules/@angular/material/prebuilt-themes/indigo-pink.css`), 'utf8');
const vm = require('vm');

class JSDOMEnvironment {


  constructor(config) {
    this.browser = new jsdom.JSDOM(`<!DOCTYPE html><head></head><body></body><html>`, {
      features : {
        FetchExternalResources : ['script', 'style', 'link'],
        QuerySelector : true
      },
      url: config.testURL
    });

    const _global = (this.global = this.browser.window);


    const document = _global.document;
    let head = document.getElementsByTagName('head')[0];
    let style = document.createElement("style");
    style.type = 'text/css';
    style.innerHTML = maincss;
    head.appendChild(style);
   

    installCommonGlobals(_global, config.globals);

    this.moduleMocker = new mock.ModuleMocker(_global);
    this.fakeTimers = new FakeTimers(_global, this.moduleMocker, config);
  }

  dispose() {
    if (this.fakeTimers) {
      this.fakeTimers.dispose();
    }
    if (this.global) {
      this.global.close();
    }
    this.global = null;
    this.browser = null;
    this.fakeTimers = null;
  }

  runScript(script) {

    if (this.global) {

      return script.runInContext(vm.createContext(this.global));
    }
    return null;
  }
}
// new JSDOMEnvironment({testURL: 'https://github.com/letsrock-today/mock-local-storage'});

module.exports = JSDOMEnvironment;

@thymikee
Copy link
Owner

See how it's done here: https://github.com/Micromeritics/jest-environment-jsdom-11.0.0. You can also send them a PR and use it.

@luke-emmental
Copy link
Author

luke-emmental commented Jun 21, 2017

Thanks @thymikee . I will try.

@michahell
Copy link

michahell commented Jun 22, 2017

Also hitting this, after first having hit the bug with merging moduleNameMapper from a preset + jest.config :( (seems to get fixed in jestjs/jest#3689 though).

@thymikee
Copy link
Owner

@luke-emmental can you send a PR to example in this repo with usage if Material module, so I can test new environment?

@michahell
Copy link

Hmm. maybe there is a way to mock material components, seems like the material design guys would do that to test their own nested components 🤔

@ollwenjones
Copy link

Same deal here.

@ollwenjones
Copy link

adding

Object.defineProperty(window, 'CSS', {value: mock()});

to my jestGlobalMocks.ts seems to have cleared up the CSS is not defined issue for me.

@michahell
Copy link

michahell commented Jun 28, 2017

nice @ollwenjones ! I can at least get JEST to pass a basic component spec using material design icons. I do still get the following warnings / errors:

console.warn node_modules/@angular/material/bundles/material.umd.js:146
    Current document does not have a doctype. This may cause some Angular Material components not to behave as expected.

  console.warn node_modules/@angular/material/bundles/material.umd.js:159
    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

  console.log node_modules/@angular/material/bundles/material.umd.js:13754
    Loading icon set URL: [object Object] failed: SyntaxError

  console.log node_modules/@angular/material/bundles/material.umd.js:14108
    Error retrieving icon: Unable to find icon with the name "security-outline"

which are probably still related to the Jsdom document type setting

@zack9433
Copy link
Contributor

I add some mock

Object.defineProperty(document, 'doctype', {
  value: '<!DOCTYPE html>'
});
Object.defineProperty(window, 'getComputedStyle', {
  value: () => {
    return {
      display: 'none',
      appearance: ['-webkit-appearance']
    };
  }
});

and warning messages

console.warn node_modules/@angular/material/bundles/material.umd.js:146
    Current document does not have a doctype. This may cause some Angular Material components not to behave as expected.

  console.warn node_modules/@angular/material/bundles/material.umd.js:159
    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

are gone

@thymikee
Copy link
Owner

@zack9433 nice! Thank you for sharing :)

@thymikee
Copy link
Owner

Would you mind adding PR to the example, so it's there by default?
https://github.com/thymikee/jest-preset-angular/blob/master/example/src/jestGlobalMocks.ts

@michahell
Copy link

Awesome! going to try that right now, thanks for sharing @zack9433 !

@michahell
Copy link

michahell commented Jul 28, 2017

But I think this has the same effect as using the
{provide: MATERIAL_SANITY_CHECKS, useValue: false}, provider in your
TestBed.configureTestingModule() setup, no ?

I'm still getting the following warning types though:

console.log node_modules/@angular/material/bundles/material.umd.js:12149
Loading icon set URL: [object Object] failed: SyntaxError
console.log node_modules/@angular/material/bundles/material.umd.js:12485
Error retrieving icon: Unable to find icon with the name "security-outline"

@zack9433
Copy link
Contributor

@thymikee sorry for lately reply. I already created a PR for this issue.
@michahell I think you can try to load a fake icon like this

@thymikee
Copy link
Owner

thymikee commented Aug 4, 2017

Fixed with #61

@denniske
Copy link

I think the following is still missing in jestGlobalMocks.ts:

Object.defineProperty(window, 'CSS', {value: mock()});

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

No branches or pull requests

6 participants