{"id":20610744,"url":"https://github.com/anjasfedo/jestjs","last_synced_at":"2026-04-18T22:02:54.688Z","repository":{"id":212838236,"uuid":"732420003","full_name":"Anjasfedo/JestJS","owner":"Anjasfedo","description":"JavaScript Unit Testing with JestJS.","archived":false,"fork":false,"pushed_at":"2023-12-20T16:33:23.000Z","size":47,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-17T03:28:18.108Z","etag":null,"topics":["jestjs","testing"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Anjasfedo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2023-12-16T16:06:01.000Z","updated_at":"2024-01-11T12:18:58.000Z","dependencies_parsed_at":"2023-12-19T18:45:38.783Z","dependency_job_id":"c3bf46ec-ba70-4246-b327-89da69ec1515","html_url":"https://github.com/Anjasfedo/JestJS","commit_stats":{"total_commits":9,"total_committers":1,"mean_commits":9.0,"dds":0.0,"last_synced_commit":"1f311cf408101797579029babab7ebbcc2210ff1"},"previous_names":["g1a021037-anjasfedo/jestjs","anjasfedo/jestjs"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Anjasfedo%2FJestJS","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Anjasfedo%2FJestJS/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Anjasfedo%2FJestJS/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Anjasfedo%2FJestJS/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Anjasfedo","download_url":"https://codeload.github.com/Anjasfedo/JestJS/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242258070,"owners_count":20098263,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["jestjs","testing"],"created_at":"2024-11-16T10:17:45.140Z","updated_at":"2026-04-18T22:02:54.611Z","avatar_url":"https://github.com/Anjasfedo.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Intro\npowerfull testing for javascript framework\n\n===\n\n## Setting Up Jest\n1. make new folder to store our project, give folder name JestJS\n2. open terminal on that directory\n3. then do npm init -y to initialize node.js project\n4. then we can open the code in vscode by type code .\n5. to install jestjs we can use: npm install --save-dev jest\n6. so we successfully install jest on our node.js, we can see on package.json\n7. next we will configure npm script\n8. on package.json, on scripts there is test\n\n===\n\n## Basic Concepts In Jest\n1. unit test is process of checking small piece of code to deliver information early\n2. we need unit test to easly identify error or bug, instead of check the whole code\n3. now we gonna make a unit test with jestjs\n4. we start by make new file name sum.js, we gonna type our code in this file\n5. then also create new file name sum.test.js. this file is the test file\n6. back to sum.js. we make new function name sum that take 2 parameter a and b. and return a + b. then we export the sum function with module like:\n7. then we need to use sum funcion on sum.test.js\n8. once we make name.test.js, javascript automaticly know that this file is test file\n9. now open sum.test.js, we need import the sum function from sum.js with:\n10. now we can run the test, with test() method, the first parameter is description and followed by testFunction as callback\n11. we gonna make desription that adds 1 + 2 to equal 3\n12. with callback second parameter, that use expect(function).toBe(expect output) like: \n13. then open package.json, change jest to test, and the value to jest like: \n13. now run the test, we can open terminal run npm test\n14. then we can see information about the test is pass\n\n===\n\n## Matchers\n1. use to test different type of data. on last code, the matcher is toBe method\n2. there is various type of matcher, like toBe toEqual toTruthy toFalsy toThrow\n3. first we well learn about toBe\n4. to is use to primitive value (number, string, boolean)\n5. start with make test with description two plus two is four\n6. and with call back function with expect 2 + 2 toBe 4 like: \n7. so we can try npm test to run the test\n8. next is about toEqual, is use when comparing object or array\n9. we can make test with description object assignment\n10. with callback function that have const variable of data with key and value of one: 1\n11. follow with assign data on that object with key and value two and 2\n12. then add expect data toEqual({one: 1, two:2}), the code will be like: \n13. and we run npm test to run the test\n14. so next matcher is toTruthy and toFalsy matcher, we use \n15. start by create a test with description null is falsy\n16. with callback function that have const variable name n with value null\n17. the add expect n toBeFalsy()\n18. lets run with npm test\n19. because falsy not only null, but also all falsy state, so we can use it like: \n20. next we gonna try toBeTruthy, the value of truthy is  the exception of falsy state\n21. we can change value of n to 1, and toBeFalsy to toBeTruthy\n22. and also change the description to one is truthy like: \n23. and run the test with npm run\n24. the last thing is toThrow to error handling\n25. we use it for detailing when the function is throw error, we expect the function throw the error when its executed. useful to validate input and throw the error\n26. first create new test with description throws on invalid input\n27. with callback function that expect with function myFunction(invalidInput) and toThrow()\n28. the back to sum.js, and add mynFunction with input as parameter\n29. and create a conditional that if typeof input is not a number we gonna throw new Error('invalid input')\n30. the we need to export the function like:\n31. now back to sum.test.js, and create const variable name myFunction with value require of the file of myFunction that sum\n32. so on expect callback function we change the argument to some string like:\n33. now try the test with npm test\n\n===\n\n## Testing Asynchronous Code\n1. test asynchronus code is quite complex, not such straighforward\n2. asyncronus code allow to execute some code on background, not blocking the main execution\n3. web need asynchronus because keep ui to responsive while do request or timer\n4. there is some method of async such as callback, promise, and async function\n5. first the callback, lets start by write new function called fetchData with parameter callback\n6. and do setTimeout with callback function callback('peanut butter') and time 1000\n7. new we gonna test this function, be sure to export the function, the code be like: \n8. create test with description the data is peanut butter\n9. then for second parameter make done =\u003e {}\n10. done contain function callback(data)\n11. and do try with expect(data) toBe('peanut butter') and add done()\n12. on catch error, add done(error)\n13. then after the callback function, add fetchData(callback). the code gonna be like: \n14. and we can try the test with npm test\n15. next we gonna try promise\n16. first we need to create a function with promise\n17. create function name fetchPromise with return new Promise\n18. with parameter resolve and reject, then use setTimeout to\nresolve('peanut butter') in 1000\n19. now make sure to export this function\n20. and create test for the promise, we need to import the function with require method\n21. then write test with description the data is peanut butter\n22. with second parameter a callback that return expect the function\n23. follow by resolves.toBe('peanut butter')\n24. then we can do npm test to run the test\n25. now also create the test on reject\n22. with desctipiton the fetch fails with an error \n23. then callback function that return expect(fetchPromise()).rejects.toThrow('error')\n24. now we talk about async await function\n25. now to do async await test, we create new test with description the data is peanut butter\n26. follow by async function, that have const variable call data with value await fetchPromise()\n27. and expect data toBe('peanut butter')\n28. the async will significe the callback funtion will be an asynchronus\n29. the await is keyword of async, this wait the function to return the result\n30. now we gonna run the test with npm test\n\n===\n\n## Mock Functions and Spies\n1. mock is fake implementation of real function\n2. and spies are tools that use to track the behavior of those function \n3. such the function is called or how many it has called, and with what argument of the function\n4. mocking used to isolate unit of code being tested, this prevent test from being affected by external factors\n5. now lets try mock function in jest\n6. we gonna use jest.fn the way to create mock function\n7. first we create new const variable name mockCallback with value jest.fn \n8. and have argument of a function x =\u003e 42 + x\n9. and we can call mockCallback(0) or mockCallback(1)\n10. that gonna make jest.fn contain 43\n11. now to test the mock function with jest\n12. write test with descripiton mock implementation of basic function\n13. with function the before function of mockCallback\n14. now expect mock(1) toBe(43)\n15. now try the test with npm test\n16. then add expect mock toHaveBeenCalledWith(1)\n17. this statement check if the mock call by correct argument\n18. then run test too\n19. next is spies, we gonna make new test with desctiption spying on a method of an object\n20. with function that have variable called video\n21. video have value of object then a method play() { return true }\n22. then add const spy with value jest.spyOn(video, 'play')\n23. then add video.play()\n24. and expect spy. toHaveBeenCalled()\n25. and do sply.mockRestore()\n26. so we can run the test","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanjasfedo%2Fjestjs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fanjasfedo%2Fjestjs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanjasfedo%2Fjestjs/lists"}