lundi 22 juillet 2019

jest test says "ReferenceError: Headers is not defined"

I'm trying to test a function which interacts with the Headers class. Not sure if my test technically falls into a category of a unit-test but that should not be relevant.

When I run the test, I see the infamous ReferenceError: Headers is not defined error. I'm sure it's because the test is executed in a non-browser environment.

My question is: how do I execute the test in a browser environment, because I don't want to mock the Headers class at all?

$ yarn test                                                                         
yarn run v1.12.3                                                                    
$ jest                                                                              
 FAIL  __tests__/request-init-spec.ts                                     
  requestInit                                                               
    × should work (9ms)                                                             

  ● requestInit › should work                                               

    ReferenceError: Headers is not defined                                          

      13 | describe(requestInit.name, () => {                               
      14 |   it('should work', () => {                                              
    > 15 |     const headers = new Headers();                                       
         |                     ^                                                    
      16 |     headers.append('H1', 'V1');                                          
      17 |                                                                          
      18 |     const requestInit: RequestInit = {                                   

      at Object.it (__tests__/request-init-spec.ts:15:21)                 

Test Suites: 1 failed, 1 total                                                      
Tests:       1 failed, 1 total                                                      
Snapshots:   0 total                                                                
Time:        4.574s                                                                 
Ran all test suites.                                                                
error Command failed with exit code 1.                                              
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Aucun commentaire:

Enregistrer un commentaire