Skip to content

Instantly share code, notes, and snippets.

@owans
Last active April 24, 2019 13:07
Show Gist options
  • Save owans/66cedb88ebc600df53eaf0f595bf1683 to your computer and use it in GitHub Desktop.
Save owans/66cedb88ebc600df53eaf0f595bf1683 to your computer and use it in GitHub Desktop.
Write tests using Jest
const getCatFact = require('./apitest');
test(`get catFacts`, () => {
getCatFact('facts', () => {
expect(getCatFact.resolve('facts')).resolves.toBe('facts')
})
})
const axios = require("axios")
const getCatFact = async (req, res) => {
try{
const url = "https://cat-fact.herokuapp.com/facts/random?animal"
const response = await axios.get(url)
const facts = response.data
console.log(facts)
res.json(
facts.map((facts) => {
return({facts})
})
)
}catch(err){
console.log(err)
res.status(503).json({status: `error`, message: `Service Unavailable`})
}
}
module.exports = getCatFact;
function multipleArray(arr){
let myArr = [];
for (let i = 0; i < arr.length; i++){
if(Array.isArray(arr)){
myArr[i] = arr[i] * 2;
}else{
console.log ("Parameter isn't a number");
}
}
return myArr;
}
console.log(multipleArray([1, 2, 4, 8]));
module.exports = multipleArray;
const multipleArray = require('./arraytest');
test("mutiply each array element by 2", () => {
expect (multipleArray([1, 2, 4, 8])).toEqual([2, 4, 8, 16]);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment