Last active
          December 16, 2021 19:25 
        
      - 
      
 - 
        
Save jbenner-radham/d39c254a6822155e09f6f9a51da27d40 to your computer and use it in GitHub Desktop.  
    Using Jest with ES Modules (Without Babel)
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | export default () => ({}); | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | import { fileURLToPath } from 'url'; | |
| import lib from '../'; | |
| // The `__dirname` psuedo-global is not available when using es-modules. | |
| // The following can be used as a replacement. | |
| const dirname = fileURLToPath(new URL('./', import.meta.url)); | |
| describe('lib', () => { | |
| it('is a function', () => { | |
| expect(lib).toBeFunction(); | |
| }); | |
| }); | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | module.exports = { | |
| setupFilesAfterEnv: ['jest-extended'], | |
| transform: {} // <-- Important because it instructs Jest not to perform any transformations on code | |
| }; | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | { | |
| "name": "jest-with-es-modules", | |
| "type": "module", | |
| "version": "1.0.0", | |
| "description": "", | |
| "main": "index.js", | |
| "scripts": { | |
| "test": "NODE_OPTIONS=--experimental-vm-modules jest --watchAll --verbose" | |
| }, | |
| "devDependencies": { | |
| "jest": "^27.4.5", | |
| "jest-extended": "^1.2.0" | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment