devrafalko / jasmine-dom-custom-matchers

jasmine 2.0 DOM custom matchers comparing relations, attributes, styles and states of html element and text nodes
https://devrafalko.github.io/jasmine-dom-custom-matchers/index.html
MIT License
18 stars 4 forks source link

addMatchers method not adding jasmine-dom-custom-matchers package in Angular 8 app #8

Open jamesbarrett95 opened 5 years ago

jamesbarrett95 commented 5 years ago

Hi there,

I'm currently using this package in my Angular 8 project with Jasmine + Karma as dependancies. When I add the matchers and run an assertion, I receive an Property [nameOfProperty] does not exist on type Matchers<typeOfElement> when running the tests.

package.json (relevant packages):

    "@types/karma-viewport": "^0.4.0",
    "karma": "^4.1.0",
    "karma-chrome-launcher": "^2.2.0",
    "karma-coverage-istanbul-reporter": "^2.0.4",
    "karma-ie-launcher": "^1.0.0",
    "karma-jasmine": "^2.0.1",
    "karma-jasmine-dom": "^1.0.1",
    "karma-jasmine-html-reporter": "^1.4.0",
    "karma-junit-reporter": "^1.2.0",
    "karma-phantomjs-launcher": "^1.0.4",
    "karma-viewport": "^1.0.4",
    "jasmine-core": "^3.3.0",
    "jasmine-dom-custom-matchers": "^1.0.5",
    "jasmine-spec-reporter": "^4.2.1",
    "karma-jasmine": "^2.0.1",
    "karma-jasmine-dom": "^1.0.1",
    "karma-jasmine-html-reporter": "^1.4.0"

karma.conf.js:

  config.set({
    basePath: '',
    frameworks: ['jasmine-dom', 'jasmine', '@angular-devkit/build-angular', 'viewport'],
    plugins: [
      require('karma-jasmine'),
      require('karma-chrome-launcher'),
      require('karma-jasmine-html-reporter'),
      require('karma-coverage-istanbul-reporter'),
      require('@angular-devkit/build-angular/plugins/karma'),
      require('karma-viewport'),
      require('karma-phantomjs-launcher'),
      require('karma-jasmine-dom')
    ],
...[restOfConfig]

app.component.spec.ts:

  beforeAll(async(() => {
    const matchers = require('jasmine-dom-custom-matchers');
    jasmine.addMatchers(matchers);
  }))

  it('should call the getStyleSheets() with config', async () => {
    let headTagDOM = document.querySelector('head');
    let linkTags;
    spyOn(component, 'getStyleSheets');
    component.getStyleSheets(config);
    linkTag = document.querySelectorAll('link')[0];
    expect(component.getStyleSheets).toHaveBeenCalled();
    // Error occurs here: Can't recognise the toContainHTMLElement method
    expect(headTagDOM).toContainHTMLElement(linkTags)
  });

I'm unsure if this is a config error or a bug whereby using the package with Angular isn't possible.

Any help would be much appreciated.

elliot-nelson commented 5 years ago

Note that matchers (and pretty much all other jasmine configuration calls) are wiped after every spec. If you intend to install them globally, you should use beforeEach, not beforeAll. That might fix it for you.