Howdy! π
- π¬ Ask me about JavaScript, serverless, NoSQL, PM culture, project ops, etc.
- π« How to reach me: DM me on twitter
- π Pronouns: he/him/they
- Fun fact: I've got 3 chonky cat children πΌπ½πΎ
Easily invoke your NodeJS Azure Functions during unit tests
License: MIT License
Howdy! π
Currently if you have the following in your function under test:
context.log.info("Invalid response");
you will get an error:
context.log.info is not a function
Handle for:
Hey,
So, I have this function.
const delay = time => new Promise(resolve => setTimeout(resolve, time))
module.exports = async (context, req) => {
await delay(500)
const { query: { name } } = req
if (name) {
context.res = {
status: 200,
body: `Hello, ${name}`
}
} else {
context.res = {
status: 400,
body: 'Name missing'
}
}
context.done()
}
delay
puts the function to sleep for a bit, before processing. Just trying out async
capabilities of Azure Functions.
And here are my ava tests.
const test = require('ava')
const func = require('azure-functions-node-harness')
test('valid', async t => {
const helloWorld = func('HelloWorld')
const name = 'Ali'
const context = await helloWorld.invokeHttpTrigger({
query: {
name
}
})
const { res: { body, status } } = context
t.deepEqual(status, 200)
t.deepEqual(body, `Hello, ${name}`)
})
test('invalid', async t => {
const helloWorld = func('HelloWorld')
const context = await helloWorld.invokeHttpTrigger()
const { res: { body, status } } = context
t.deepEqual(status, 400)
t.deepEqual(body, 'Name missing')
})
When these tests are ran in parallel, which is the default ava behavior, valid
fails claiming that status
is 400
, while the test expects it to be 200
.
But if I run the tests serially with test.serial()
, they both pass just fine.
How can I run them in parallel, and make sure they don't overlap? Or whatever is going on right now.
Thanks!
Use function.json
to figure out trigger type. This would be useful for http context scenarios. One possibility is to load the sample.dat
and pass it to unit test as default request body.
A user currently has to generate all of the following. Instead give them a helper to pass just the body.
Properties on request object (https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-node#http-triggers-and-bindings):
Sample helper would be something like (not complete example):
httpRequest: function(body){
return{
method: "POST",
body:body,
rawBody: JSON.stringify(body),
};
}
Could potentially fill load info like method
from function.json
Input bindings work, but add support for writing to output bindings.
Add functionality and tests for all the different trigger bindings where it makes sense. Here is a list from the functions docs:
It is possible to set the response in three different ways:
A declarative, efficient, and flexible JavaScript library for building user interfaces.
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. πππ
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google β€οΈ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.