Skip to content

Instantly share code, notes, and snippets.

@ezequieltejada
Last active July 21, 2022 08:13
Show Gist options
  • Save ezequieltejada/bd93050e3a10f122b86da5a7c253ec15 to your computer and use it in GitHub Desktop.
Save ezequieltejada/bd93050e3a10f122b86da5a7c253ec15 to your computer and use it in GitHub Desktop.
Testing Steps
  1. Install Jest (yarn add -D jest babel-jest @babel/core @babel/preset-env | npm install -D jest babel-jest @babel/core @babel/preset-env)
  2. Create babel.config.js file in the root of the project with this content
    module.exports = {
        presets: [
            ['@babel/preset-env', { targets: { node: 'current' } }],
            '@babel/preset-typescript'
        ]
    };
    
  3. Install type definitions (yarn add -D @types/jest | npm install -D @types/jest)
  4. Add jest env to package.json
    {
        ...rest of package.json
        "jest": {
            "testEnvironment": "node"
        }
    }
    
  5. Add jest in esconfig.rc to avoid linting problems. StackOverflow
    "env": {
        "jest/globals": true
    }
    
  6. Modify npm run test to run jest.
    scripts:{
        "test": "jest --verbose ./my-directory"
    }
    
    • We can add "--silent" to avoid displaying console.logs

Unexpected token error

When I got this error I've solved it creating a "jest.config.js" file in the root of the project with this content:

module.exports = {
  roots: [
    '<rootDir>/test'
  ],
  testMatch: [
    '**/__tests__/**/*.+(ts|tsx|js)',
    '**/?(*.)+(spec|test).+(ts|tsx|js)'
  ],
  transform: {
    '^.+\\.(ts|tsx)$': 'ts-jest'
  }
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment