4

I a persistent error while using serverless framework with serverless-offline and jest unit testing module.

I am trying to test my serverless function (lambda) using serverless-jest-plugin which is supposed to integrate jest on serverless. Following is my jest configuration in my serverless.yml:

custom:
 default:
  stage: dev
 table:
  dictionary: Dictionary
  rules: Rules
 jest:
  verbose: true
  testResultsProcessor: jest-junit
  modulePaths: ["<rootDir>/node_modules", "<rootDir>/lib"]
  roots: ["<rootDir>/tests"]
  testEnvironment: node
plugins:
   - serverless-jest-plugin
   - serverless-dynamodb-local
   - serverless-offline

I have a lambda function which uses dynamoDB to get some data etc.

Whenever I use sls invoke test I have the following:

$ sls invoke test
 FAIL  tests/file.test.js
  ● Test suite failed to run

TypeError: Path must be a string. Received undefined

  at assertPath (path.js:7:11)
  at Object.relative (path.js:1226:5)
  at process._tickDomainCallback (internal/process/next_tick.js:135:7)
Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        0.386s
Ran all test suites.

I'm running the following package.json:

{
  "dependencies": {
    "ajv": "^6.1.1",
    "async": "^2.6.0",
    "aws-sdk": "^2.188.0",
    "notevil": "^1.1.0"
  },
  "devDependencies": {
    "jest": "^22.2.1",
    "jest-junit": "^3.5.0",
    "serverless-dynamodb-local": "^0.2.27",
    "serverless-jest-plugin": "^0.1.6",
    "serverless-offline": "^3.16.0"
  }
}

Cheers.

Souf
  • 167
  • 1
  • 2
  • 12
  • As a workaround I've used `serverless-jest-plugin` inside each one of my test files then wrap my lambda functions with `lambdaWrapper`. then call test files with `npm test`. – Souf Mar 29 '18 at 10:00
  • Here's an example: ```javascript const jestPlugin = require('serverless-jest-plugin'); const lambdaWrapper = jestPlugin.lambdaWrapper; const myFunction = require('/../services/function1'); const wrappedFunction = lambdaWrapper.wrap(myFunction, { region: 'your_region', handler: 'handler' }); // Here you should prepare your lambda event then call it wrappedFunction.run(event, (err, response) => { ... }); ``` – Souf Mar 29 '18 at 10:11

0 Answers0