-1

I was working on unit testing I am getting below error

Object is possibly 'undefined'

 it('should set the dataSource filter to the provided argument', () => {
  component.applyFilter('filterValue');
   expect(this.dataSource.filter).toEqual('filterValue');
  })

it('should set the dataSource filter to the provided argument', () => {
   component.applyFilter('filterValue');
   expect(this.DMDataSource.filter).toEqual('filterValue');
 })

I am getting error inside expect(this) what is the mistake here.

Please let me know.

Shashank Vivek
  • 13,776
  • 7
  • 48
  • 88
Mr.M
  • 1,217
  • 1
  • 19
  • 51
  • Does this answer your question? [How can I solve the error 'TS2532: Object is possibly 'undefined'?](https://stackoverflow.com/questions/54884488/how-can-i-solve-the-error-ts2532-object-is-possibly-undefined) – Akash Sep 19 '20 at 15:05
  • Mr M : Did my answer helped ? – Shashank Vivek Sep 26 '20 at 13:59
  • I gave tick mark i have one more question which i asked can you please help on that https://stackoverflow.com/questions/64058914/angular-ngif-not-working-as-expected-angular-8 – Mr.M Sep 26 '20 at 14:02

1 Answers1

1

You should use component.dataSource rather than this.dataSource inside expect block

You need to evaluate the dataSource defined inside component instance

it('should set the dataSource filter to the provided argument', () => {
  component.applyFilter('filterValue');
   expect(component.dataSource.filter).toEqual('filterValue');
  })

it('should set the dataSource filter to the provided argument', () => {
   component.applyFilter('filterValue');
   expect(component.DMDataSource.filter).toEqual('filterValue');
 })
Shashank Vivek
  • 13,776
  • 7
  • 48
  • 88