{"id":14967385,"url":"https://github.com/nordcloud/serverless-mocha-plugin","last_synced_at":"2025-04-04T19:08:02.281Z","repository":{"id":6976867,"uuid":"55841009","full_name":"nordcloud/serverless-mocha-plugin","owner":"nordcloud","description":"Plugin for Serverless Framework which adds support for test-driven development using Mocha","archived":false,"fork":false,"pushed_at":"2023-09-27T20:49:34.000Z","size":752,"stargazers_count":159,"open_issues_count":54,"forks_count":50,"subscribers_count":13,"default_branch":"master","last_synced_at":"2025-03-28T18:16:05.402Z","etag":null,"topics":["mocha","serverless-framework","serverless-plugin","test-driven-development"],"latest_commit_sha":null,"homepage":"","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":"2016-04-09T11:35:34.000Z","updated_at":"2024-09-27T10:55:59.000Z","dependencies_parsed_at":"2024-06-18T15:16:04.936Z","dependency_job_id":"83eb3a4e-3319-433d-ae06-f95954baf667","html_url":"https://github.com/nordcloud/serverless-mocha-plugin","commit_stats":{"total_commits":221,"total_committers":26,"mean_commits":8.5,"dds":0.4570135746606335,"last_synced_commit":"7aace4054ea2007f9d0d321b119bfa446eb7f500"},"previous_names":["sc5/serverless-mocha-plugin"],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nordcloud%2Fserverless-mocha-plugin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nordcloud%2Fserverless-mocha-plugin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nordcloud%2Fserverless-mocha-plugin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nordcloud%2Fserverless-mocha-plugin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nordcloud","download_url":"https://codeload.github.com/nordcloud/serverless-mocha-plugin/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247234921,"owners_count":20905854,"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":["mocha","serverless-framework","serverless-plugin","test-driven-development"],"created_at":"2024-09-24T13:37:58.691Z","updated_at":"2025-04-04T19:08:02.263Z","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 plugin 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 `initLiveModule()` instead of `getWrapper()`. You can also use `--live` flag and then you don't need to change your tests.\n\n```\n  let wrapped = mochaPlugin.initLiveModule('myLambdaFunctionName');\n```\n\n`initLiveModule()` and `getWrapper()` are helper methods to initialize [lambda-wrapper](https://github.com/nordcloud/lambda-wrapper), which is used under the hood. Both methods return wrapped function, which can be invoked with `.run({})` method and takes event object as an argument.\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. If you're running the tests locally (rather than on live Lambdas, as described below), it will also set the `IS_LOCAL` to `'true'` to match the behavior of [`sls invoke local`](https://serverless.com/framework/docs/providers/aws/cli-reference/invoke-local#environment).\n\n```\nsls invoke test [--stage stage] [--region region] [-t timeout] [-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 --root 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 \"server ready\" $TMPFILE\ndo sleep 1; done\n\nrm $TMPFILE\n```\n\n**Note:** The example relies on the output of the `serverless offline` command. If the start script is not working for you, replace `\"server ready\"` with the string `serverless offline` prints as soon as the server is ready and listening.\n\nSample stopOffline.sh\n```\nkill `cat .offline.pid`\nrm .offline.pid\n```\n\n### Usage with [babel register](https://babeljs.io/docs/en/babel-register)\n\nIf you use mocha with [babel compiler](https://github.com/mochajs/mocha/wiki/compilers-deprecation) e.g. `sls invoke test --compilers js:@babel/register` \\\nBabel configuration can be determined with the custom `babelOptions` configuration in serverless.yml\n\n```\ncustom:\n  serverless-mocha-plugin:\n    babelOptions:\n      presets: [[\"@babel/env\", { \"targets\": { \"node\": \"8.10\" }, \"shippedProposals\": true, \"useBuiltIns\": \"usage\" }]]\n      plugins:\n        - [\"@babel/plugin-transform-runtime\"]\n```\n## Release History (1.x)\n\n* 2019/11/xx - v1.12.0 - support for node12\n                         fix --compiler option parsing\n* 2019/07/25 - v1.11.0 - support for node10\n                         deprecated node6\n* 2019/04/02 - v1.10.0 - add timeout parameter\n                         add babel options\n* 2018/12/15 - v1.9.1 - fix to work with serverless 1.33 and later\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-mocha-plugin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnordcloud%2Fserverless-mocha-plugin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnordcloud%2Fserverless-mocha-plugin/lists"}