'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)
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
