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

207
Views
How to cover async function by unit test?

I'm trying to cover code below by unit test, but in code coverage report lines are still red. And I see warning that property debug is undefined. How I can successfully cover this function?

import sinon from "sinon"
import policyTesterTypeDefs from "./typeDefinitions/policyTesterTypeDefinitions"
import { typeDefs, testPolicyOnDatapoint } from "./policyTester"

describe("policyTester.js", () => {
    let log
    beforeEach(() => {
        log = {
            debug: sinon.stub(),
        }
    })

    afterEach(() => {
        log.debug.restore()
    })

    it("Is policy test schema valid", () => {
        expect(typeDefs).to.equal(policyTesterTypeDefs)
    })

    it("returns valid datapoint for policy tester", async () => {
        const input = {
            policy: {
                ingest: {
                    type: "ingest",
                    name: "zorro"
                }
            }
        }
        expect(await testPolicyOnDatapoint(input, {})).to.equal(true)
    })
})

code which I'm trying to cover

import * as log from "../../logger"
import { getGrpcRequestContext } from "../../clients"
import policyTesterTypeDefs from "./typeDefinitions/policyTesterTypeDefinitions"

export const typeDefs = policyTesterTypeDefs

export async function testPolicyOnDatapoint(input, ctx) {
    const { type, ...validInput } = input
    log.contextLogger(ctx).debug({ validInput }, "Testing policy on datapoint")
    const requestContext = getGrpcRequestContext(ctx)
    try {
        validInput.datapoint = inputDataToScalars(validInput.datapoint, "metadata")
        if (validInput.datapoint.event !== null && validInput.datapoint.event !== undefined) {
            validInput.datapoint = inputDataToScalars(validInput.datapoint, "dimensions")
        }
        const response = await ctx.dataSources.policyTesterSvc
            .TestPolicyOnDatapoint(validInput, requestContext)
        response.datapoint = response.datapoint ?
            convertDatapointScalarsToArrays(response.datapoint) :
            null
        return response
    } catch (e) {
        log.contextLogger(ctx).warn(e, "Failed to test policy on datapoint")
        throw (e)
    }
}

code coverage report

about 4 years ago · Juan Pablo Isaza
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!