vscode-jest icon indicating copy to clipboard operation
vscode-jest copied to clipboard

Rejected to value: [TypeError: Cannot read property 'Lambda' of undefined]

Open s1mrankaur opened this issue 4 years ago • 0 comments

My tests are written in JS and code in TS.

babel.config.js

module.exports = {
  presets: [
    [
      '@babel/preset-env',
      {
        targets: {
          esmodules: true,
        },
      },
    ],
  ],
}

jest.config.js


// jest.config.js
module.exports = {
    collectCoverageFrom: ['./services/**/*.{js, ts,tsx}'],
    preset: 'ts-jest',
    transform: {
      '^.+\\.(ts|tsx)?$': 'ts-jest',
      "^.+\\.(js|jsx)$": "babel-jest",
    },
    coverageThreshold: {
      './services/**/*.js': {
        branches: 100,
        functions: 100,
        lines: 100,
        statements: 100,
      },
      './services/**/*.ts': {
        branches: 100,
        functions: 100,
        lines: 100,
        statements: 100,
      },
    },
    moduleDirectories: [
        'node_modules',
    ],

  }

tsconfig.json


{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "baseUrl": ".",
    "moduleResolution": "node",
    "sourceMap": true,
    "target": "ES6"
  },
  "exclude": ["node_modules", "**/*.spec.ts"],
  "composite": true,
  "declarationMap": true,
  "strict": true,
  "noUnusedLocals": true,
  "noImplicitThis": true,
  "strictNullChecks": true,
  "noImplicitReturns": true,
  "preserveConstEnums": true,
  "suppressImplicitAnyIndexErrors": true,
  "forceConsistentCasingInFileNames": true,
  "resolveJsonModule": true,
  "esModuleInterop": true
}

When I run the debugger , it says:


 ● tenant-functions library tests › should find that the Tenant exits        

    expect(received).resolves.toBe()

    Received promise rejected instead of resolved
    Rejected to value: [TypeError: Cannot read property 'Lambda' of undefined]

      40 |     });
      41 |     const accountId = 'EBS-600';
    > 42 |     await expect(tenantFunctions.checkTenantExists(accountId)).resolves.toBe(true);
         |           ^
      43 |   });
      44 |   it('should find that the Tenant does not exits', async () => {
      45 |     // Mock Lambda invoke to return a known payload

      at expect (node_modules/expect/build/index.js:134:15)
      at Object.<anonymous> (services/tenant-registration-service/unit-tests/tenant-functions.test.js:42:11)
      at asyncGeneratorStep (services/tenant-registration-service/unit-tests/tenant-functions.test.js:3:103)
      at _next (services/tenant-registration-service/unit-tests/tenant-functions.test.js:5:194)
      at services/tenant-registration-service/unit-tests/tenant-functions.test.js:5:364
      at Object.<anonymous> (services/tenant-registration-service/unit-tests/tenant-functions.test.js:5:97)

It works okay if I

  1. run npm run test
  2. If I have require aws-sdk instead of import aws-sdk in mock (but then deployment does not work) and we need to stick to the import module format.
  3. Lambda is also not undefined if I say import {Lambda} from aws-sdk but for specific reasons, I need to import AWS from aws-sdk and then use the lambda function.

What seems to be the issue? How can I fix this?

s1mrankaur avatar Feb 12 '21 20:02 s1mrankaur