Last active
April 24, 2019 13:07
-
-
Save owans/66cedb88ebc600df53eaf0f595bf1683 to your computer and use it in GitHub Desktop.
Write tests using Jest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const getCatFact = require('./apitest'); | |
test(`get catFacts`, () => { | |
getCatFact('facts', () => { | |
expect(getCatFact.resolve('facts')).resolves.toBe('facts') | |
}) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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