Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

303
Views
How we can mock or trigger window.onload method in unit test case using jest

Please find below code, i need to trigger load event so that my test function get execute

const test = () => {
    return true;
};

window.addEventListener('load', () => {
    test();
});
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Make sure the test environment is jsdom for jest, then you can trigger the load event by using dispatchEvent

E.g.

window.addEventListener('load', () => {
  console.log('load event triggered');
});

describe('71220140', () => {
  test('should pass', () => {
    window.dispatchEvent(new Event('load'));
  });
});

Test result:

 PASS  stackoverflow/71220140/index.test.ts
  71220140
    ✓ should pass (13 ms)

  console.log
    load event triggered

      at stackoverflow/71220140/index.test.ts:2:11

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        2.301 s
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!