0

Code is working properly there is no issue on that, but While I test my angular code I got this following error.

what i have to add to get rid of this error. anyone please help me on this. i know array size get exceeded but how to fix that? RangeError: Invalid array length

enter image description here

component.ts

 this.columns = Array.from(Array(this.nbColumn - 1 > 0 ? this.nbColumn - 1 : 0 ).keys())

spec.ts

describe('HomeComponent', () => {
      let component: HomeComponent;
      let fixture: ComponentFixture<HomeComponent>;
    
      beforeEach(waitForAsync(() => {
        TestBed.configureTestingModule({
          imports:[HttpClientModule,RouterTestingModule,MatSnackBarModule],
          declarations: [ HomeComponent ],
          providers:[ ]
        })
        .compileComponents();
      }));
    
      beforeEach(() => {
        fixture = TestBed.createComponent(HomeComponent);
        component = fixture.componentInstance;
        fixture.detectChanges();
      });
    
      it('should create', () => {
        expect(component).toBeTruthy();
      });
    });

Edited: I have added my spec file

5
  • can you show your unit test (spec) file ? As suggested, you may have mismocked some values. Commented Mar 15, 2023 at 9:49
  • @Random I have added my spec file. can you please check Commented Mar 15, 2023 at 9:58
  • Indeed, your unit test is empty. detectChanges will run your ngOnInit. You have to mock all your services so their method return a specific value, which would allow the ngOnInit() method to run without errors. Commented Mar 15, 2023 at 10:01
  • @Random , thanks for your feedback .As i am new to angular , can you please help me with add some example Commented Mar 15, 2023 at 10:05
  • Sorry, but if you are new to Angular, this code is too complex to help you easily. You should follow a unit test course. Commented Mar 15, 2023 at 10:10

1 Answer 1

0

The JavaScript exception "Invalid array length" occurs when specifying an array length that is either negative, a floating number or exceeds the maximum supported by the platform (i.e. when creating an Array or ArrayBuffer, or when setting the length property).

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Invalid_array_length

I don't know exactly where is home.component.ts line 52 char 23 on your image, because you haven't provided code lines, but the trace probably points to this part (just an educated guess):

Observe when variable nbColumn in the snippet changes:

    this.columns = Array(this.nbColumn - 1)
      .fill(0)
      .map((x, i) => i);

nbColumn = 1

columns = [];
nbColumn = 1;

this.columns = Array(this.nbColumn - 1).fill(0).map((x, i) => i);

console.log(this.columns)

nbColumn = 0

columns = [];
nbColumn = 0;

this.columns = Array(this.nbColumn - 1).fill(0).map((x, i) => i);

console.log(this.columns)

Probably your test doesn't populate some properties as it does in the actual application. Find out why it is so or fix the likely problematic area Array(this.nbColumn - 1).

Maybe assert that an array is always produced?

this.columns = Array.from(Array(this.nbColumn - 1 > 0 ? this.nbColumn - 1 : 0 ).keys())

More on how to use Array.from() here: https://stackoverflow.com/a/33352604/15439733

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.