I'm using Angular 8. I used karma-viewport plugin to mock the screen size and it works well to me.
The package is available at https://www.npmjs.com/package/karma-viewport.
In the karma.conf.js I have to add viewport to frameworks and require the package in plugins.
My karma.conf.js:
frameworks: ['jasmine', '@angular-devkit/build-angular', 'viewport'],
plugins: [
require('karma-jasmine'),
require('karma-chrome-launcher'),
require('karma-junit-reporter'),
require('karma-coverage-istanbul-reporter'),
require('@angular-devkit/build-angular/plugins/karma'),
require('karma-viewport')
],
In my .spec.ts file I added the viewport var declaration:
declare const viewport: any;
Changed the size in the "it" function, called my method that changes a variable according to the screen size and after checked the result. In my case the property width of the component should be 240 when the screen size is less than 700px:
it('test xyz', () => {
viewport.set(320, 240);
component.changeHeightByScreenSize();
expect(component.width).toEqual(240);
});