{"id":18866416,"url":"https://github.com/nordcloud/serverless-tdd-plugin","last_synced_at":"2026-02-13T03:30:17.413Z","repository":{"id":40950601,"uuid":"157545358","full_name":"nordcloud/serverless-tdd-plugin","owner":"nordcloud","description":"serverless-mocha-plugin reborn.","archived":false,"fork":false,"pushed_at":"2024-05-01T10:20:29.000Z","size":1559,"stargazers_count":1,"open_issues_count":14,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-12-30T22:22:33.961Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nordcloud.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-11-14T12:26:55.000Z","updated_at":"2020-07-20T09:21:31.000Z","dependencies_parsed_at":"2024-05-01T12:19:10.549Z","dependency_job_id":null,"html_url":"https://github.com/nordcloud/serverless-tdd-plugin","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nordcloud%2Fserverless-tdd-plugin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nordcloud%2Fserverless-tdd-plugin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nordcloud%2Fserverless-tdd-plugin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nordcloud%2Fserverless-tdd-plugin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nordcloud","download_url":"https://codeload.github.com/nordcloud/serverless-tdd-plugin/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239816356,"owners_count":19701752,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-08T05:06:29.089Z","updated_at":"2026-02-13T03:30:17.365Z","avatar_url":"https://github.com/nordcloud.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Serverless Mocha Plugin\n\n[![Build Status](https://travis-ci.org/nordcloud/serverless-mocha-plugin.svg?branch=master)](https://travis-ci.org/nordcloud/serverless-mocha-plugin)\n\nA Serverless Plugin for the [Serverless Framework](http://www.serverless.com) which\nadds support for test driven development using [mocha](https://mochajs.org/)\n\n**THIS PLUGIN REQUIRES SERVERLESS V1!**\n\nMore familiar with Jest? Use [serverless-jest-plugin](https://github.com/sc5/serverless-jest-plugin).\n\n## Introduction\n\nThis plugins does the following:\n\n* It provides commands to create and run tests manually\n* It provides a command to create a function, which automatically also creates a test\n\n## Installation\n\nIn your service root, run:\n\n```bash\nnpm install --save-dev serverless-mocha-plugin\n```\n\nAdd the plugin to `serverless.yml`:\n\n```yml\nplugins:\n  - serverless-mocha-plugin\n```\n\n## Usage\n\n### Creating functions\n\nFunctions (and associated tests) can be created using the command\n\n```\nsls create function -f functionName --handler handler\n```\n \ne.g.\n\n```\nsls create function -f myFunction --handler functions/myFunction/index.handler\n```\n\ncreates a new function `myFunction` into `serverless.yml` with a code template for\nthe handler in `functions/myFunction/index.js` and a Javascript function `module.exports.handler` as the entrypoint for the Lambda function. A test template is also created into `test/myFunction.js`. Optionally tests can be created to specific folder using `--path` or `-p` switch, e.g. \n\n```\nsls create function -f myFunction --handler functions/myFunction/index.handler --path tests\n```\n\nTo create an http event for the lambda, add the --httpEvent parameter, i.e.\n\n```\nsls create function -f myFunction --handler functions/myFunction/index.handler --httpEvent \"[httpVerb] [relativePath]\"\n```\n\ne.g.\n\n```\nsls create function -f myFunction --handler functions/myFunction/index.handler --httpEvent \"post myResource\" --httpEvent \"get myResource\"\n```\n\n### Creating tests\n\nFunctions can also be added manually using the mocha-create command\n\n```\nsls create test -f functionName\n```\n\nIf you want to run the tests against the real Lambda functions, you can pass the liveFunction object to wrapper.init().\n\n```\n  wrapper.init(liveFunction);\n```\n\nNOTE: Live running does not currently work. Need to finalize / have required env variables available\n\n### Running tests\n\nTests can be run directly using the \"invoke test\" command. This also initializes the environment variables based on your serverless.yml file and the SERVERLESS_TEST_ROOT variable that defines the root for the code to be tested.\n\n```\nsls invoke test [--stage stage] [--region region] [-f function1] [-f function2] [...]\n```\n\nTo use a mocha reporter (e.g. json), use the -R switch. Reporter options can be passed with the -O switch.\n\nIf no function names are passed to \"invoke test\", all tests are run from the test/ directory and subdirectories.\n\nThe default timeout for tests is 6 seconds. In case you need to apply a different timeout, that can be done in the test file \nusing using .timeout(milliseconds) with the define, after, before or it -blocks. e.g.\n```\n  it('implement tests here', () =\u003e {\n    ...\n  }).timeout(xxx);\n```\n\nTo run test in specific folder use `--path` or `-p` switch.\n\nTo run tests live against the actual deployed Lambdas, use the '--live' or '-l' switch. Please note that this will work only for tests created with module version 1.4 or higher.\n\nTo run tests e.g. against built artefacts that reside in some other directory, use the '--root' or '-r' switch. e.g.\n```\n  sls webpack -o testBuild\n  sls invoke test -r testBuild\n  rm -rf testBuild\n```\n\n\n### Using own template for a test file\n\nThe templates to use for new function Files can be determined with the custom `testTemplate` configuration in `serverless.yml`\n\n```yaml\ncustom:\n  serverless-mocha-plugin:\n    testTemplate: templates/myTest.js\n```\n\nCurrently, there are three variables available for use in the template:\n\n- functionName - name of the function\n- functionPath - path to the function\n- handlerName - the name of the handler function\n\nIf you'd like to get more information on the template engine, you check documentation of the [EJS project](http://ejs.co/).\n\n### Using own template for function file\n\nThe templates to use for new function Files can be determined with the custom `functionTemplate` configuration in `serverless.yml`\n\n```yaml\ncustom:\n  serverless-mocha-plugin:\n    functionTemplate: templates/myFunction.js\n```\n\n### Running commands before / after tests\n\nThe plugin can be configured to run commands before / after the tests. This is done by setting preTestCommands and postTestCommands in the plugin configuration.\n\nFor example, start serverless-offline before tests and stop it after tests using the following configuration:\n\n```yaml\ncustom:\n  serverless-mocha-plugin:\n    preTestCommands: \n      - bash startOffline.sh\n    postTestCommands:\n      - bash stopOffline.sh\n```\n\nSample startOffline.sh:\n```\nTMPFILE=/var/tmp/offline$$.log\nif [ -f .offline.pid ]; then\n    echo \"Found file .offline.pid. Not starting.\"\n    exit 1\nfi\n\nserverless offline 2\u003e1 \u003e $TMPFILE \u0026\nPID=$!\necho $PID \u003e .offline.pid\n\nwhile ! grep \"Offline listening\" $TMPFILE\ndo sleep 1; done\n\nrm $TMPFILE\n```\n\nSample stopOffline.sh\n```\nkill `cat .offline.pid`\nrm .offline.pid\n```\n## Release History (1.x)\n\n* 2018/09/16 - v1.9.0 - add support for --exit option\n* 2018/04/03 - v1.8.0 - add support for Node 8\n* 2017/09/10 - v1.7.0 - ability to run scripts before / after tests\n* 2017/09/09 - v1.6.0 - also run tests from subfolders of test\n* 2017/07/11 - v1.4.1 - Add option --root for running tests on e.g. webpack build results residing in other directories,\n                        add option --httpEvent to create http events when creating functions\n* 2017/07/09 - v1.4.0 - Add --live switch, \n                        add --grep switch, \n                        verify that the test runtime matches the service runtime,\n                        upgrade lambda-wrapper (returns exceptions as errors)\n* 2016/12/21 - v1.3.2 - Fix population of environment variables\n* 2016/11/28 - v1.3.1 - Added support for environment variables in Serverless 1.2\n* 2016/11/09 - v1.2.0 - Added ability to add function / test templates\n* 2016/11/09 - v1.1.0 - Added function create command.\n* 2016/09/23 - v1.0.2 - Bugfixes, configurable test timeouts\n* 2016/08/15 - v1.0.0 - Preliminary version for Serverless 1.0\n\n## License\n\nCopyright (c) 2017 [Nordcloud](https://nordcloud.com/), licensed for users and contributors under MIT license.\nhttps://github.com/nordcloud/serverless-mocha-plugin/blob/master/LICENSE\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnordcloud%2Fserverless-tdd-plugin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnordcloud%2Fserverless-tdd-plugin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnordcloud%2Fserverless-tdd-plugin/lists"}