Skip to content
This repository has been archived by the owner on Jun 17, 2024. It is now read-only.

Azure-Samples/azure-functions-unit-testing-mocha

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

page_type languages products description urlFragment
sample
nodejs
functions
This repository includes examples demonstrating how to run tests against an HTTP-triggered and timer-triggered function in JavaScript using Mocha, chai and Sinon
azure-functions-unit-testing-using-mocha

Testing Azure Functions using Mocha

This repository includes examples demonstrating how to run tests against an HTTP-triggered and timer-triggered function in JavaScript using Mocha and works best with Visual Studio Code.

Testing all code is recommended, however you may get the best results by wrapping up a Function's logic and creating tests outside the Function. Abstracting logic away limits a Function's lines of code and allows the Function to be solely responsible for calling other classes or modules. There are two functions with associated tests available, these examples include:

  • HTTP-triggered function: The example demonstrates how to pass in query string parameters to the function.
  • Timer-triggered function: A non-HTTP triggered function (in the form of a timer-triggered function) is included to demonstrate how to test a function that is not callable via a standard HTTP request.

To read more about the testing Azure functions, read Strategies for testing your code in Azure Functions

Setup

Open the folder src in VS Code. Run npm install to install the dependencies and then run npm test to run the tests. The output from the tests should look something like the following:

Testing Azure Functions with JavaScript in VS Code

Debug tests

To debug your tests, add the following configuration if not present to your launch.json file:

{
    "type": "node",
    "request": "launch",
    "name": "Mocha Tests",
    "program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
    "args": 
    [
        "--timeout",
        "999999",
        "--colors",
        "'${workspaceFolder}/{,!(node_modules)/}*/*.test.js'"
    ],
  "internalConsoleOptions": "openOnSessionStart"
}

Next, set a breakpoint in your test and press F5.

To learn more on how to setup & debug from scratch, please read this document

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.

When you submit a pull request, a CLA bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.

About

Azure functions unit testing using Mocha

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •