Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

154
Views
Any example related to function testing and edge cases in javascript

Can anyone share any example related to edge case testing in javascript and also any example related to function testing in javascript using jest.

const activityFunction: AzureFunction = async function (context: Context): Promise<string> {
    try {
        let mappingArr = [] as any;
        mapCategoryNameToNameOfNetwork(mappingArr, context);
        return
    } catch (err) {
        context.log.error("Error while mapping category name to name of networks", err)
        throw err;
    }
};



I want to test this function as this is giving blank response. I am not able to test it like i was testing for normal functions. Do anyone have any solution that how i should move ahead with this?



Thanks in advance for help.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

  1. Create an Azure JavaScript Function in VS Code and test it locally.

enter image description here

Here, a folder for HTTP Trigger1 is created.

  1. Create another folder named as testing under the root of the project in VS Code and run these commands in the terminal of same order:
npm init -y
npm i jest

It adds the required packages to the project for testing the function with jest.

  1. Update the package.json to replace the existing test command:
    "scripts": {
        "test": "jest"
    }

It looks like: enter image description here

  1. In a testing folder, create a file by naming it as defaultContext.js and add this code:
    module.exports = {
        log: jest.fn()
    };

It mocks the log function in default context.

  1. In the Azure Function Folder (i.e., HTTP Trigger1 Folder), add a new file index.test.js by adding this test code:
    const httpFunction = require('./index');
    const context = require('../testing/defaultContext')
    
    test('Http trigger should return known text', async () => {
    
        const request = {
            query: { name: 'Bill' }
        };
    
        await httpFunction(context, request);
    
        expect(context.log.mock.calls.length).toBe(1);
        expect(context.res.body).toEqual('Hello Bill');
    });

These are steps and code format of running the JavaScript Azure Function using Jest.

  1. To Run the test, use this code in the VS Code Terminal: npm test

If the test failed, it shows like below:

enter image description here

And Here the test is failed because in the test script, the result string should be like Hello {name} where as in the boilerplate code of Azure Function Http Trigger, the result string is Hello, {name}. This function executed successfully.

So both doesn't match, the test failed. Modified the HTTP Trigger Function to output the result string same as test script result i.e., Hello {name}

enter image description here

The test is passed as the function's output is Hello Bill which is same as the Test Script expected output.

Here are the references for the Azure JavaScript Function Testing using Jest and the edge cases testing:

  1. Microsoft Documentation of Azure JavaScript Functions Testing & Debugging.
  2. Edge Cases Testing Code in the Functions
  3. Testing JavaScript with Jest
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!