{"id":19133282,"url":"https://github.com/kkgthb/web-site-nodejs-02-tests","last_synced_at":"2026-04-09T08:12:03.133Z","repository":{"id":164101949,"uuid":"615053323","full_name":"kkgthb/web-site-nodejs-02-tests","owner":"kkgthb","description":"Code for a teeny-tiny Node.js web server with Jest unit tests to ensure it really says Hello World!","archived":false,"fork":false,"pushed_at":"2023-03-16T21:39:51.000Z","size":7,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-22T17:45:19.271Z","etag":null,"topics":["express-js","expressjs","jest","jest-tests","node-js","nodejs","unit-testing","web-server","webserver"],"latest_commit_sha":null,"homepage":"https://katiekodes.com/node-unit-test-jest/","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/kkgthb.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,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-03-16T21:23:06.000Z","updated_at":"2023-10-07T08:42:52.000Z","dependencies_parsed_at":null,"dependency_job_id":"d646442e-8f39-4588-a2f8-e50131aacfad","html_url":"https://github.com/kkgthb/web-site-nodejs-02-tests","commit_stats":null,"previous_names":[],"tags_count":0,"template":true,"template_full_name":null,"purl":"pkg:github/kkgthb/web-site-nodejs-02-tests","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kkgthb%2Fweb-site-nodejs-02-tests","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kkgthb%2Fweb-site-nodejs-02-tests/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kkgthb%2Fweb-site-nodejs-02-tests/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kkgthb%2Fweb-site-nodejs-02-tests/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kkgthb","download_url":"https://codeload.github.com/kkgthb/web-site-nodejs-02-tests/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kkgthb%2Fweb-site-nodejs-02-tests/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269458164,"owners_count":24420369,"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","status":"online","status_checked_at":"2025-08-08T02:00:09.200Z","response_time":72,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["express-js","expressjs","jest","jest-tests","node-js","nodejs","unit-testing","web-server","webserver"],"created_at":"2024-11-09T06:21:51.472Z","updated_at":"2026-04-09T08:12:03.084Z","avatar_url":"https://github.com/kkgthb.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# A tiny _testable_ Node.js website\r\n\r\n(Accompanies associated blog post on Katie Kodes \"[Locally unit-testing source code for a Node.js Hello World webapp](https://katiekodes.com/node-unit-test-jest/)\".)\r\n\r\n---\r\n\r\n## Prerequisites\r\n\r\n1. Please work your way through my entire exercise \"[Source code that builds locally into a Node.js Hello World webapp](https://katiekodes.com/node-hello-world/)\" before beginning this exercise so that you're familiar with \"building\" and \"running\" a webserver _(although in this case, we won't do either of those things -- instead we'll be \"testing\" it)_ from source code.\r\n1. As mentioned there, you need to have Node.js installed and NPM installed onto your local computer.\r\n1. Download a copy of **this** codebase onto your local computer, being sure to give it a completely _different_ folder from the one you used for the previous exercise.\r\n1. Using a command line interface -- ensuring first that its prompt indicates that your commands will be running within the context of the folder into which you downloaded a copy of **this** -- you must run the following command:\r\n    ```sh\r\n    npm install\r\n    ```\r\n\r\nThe `npm install` command will take about a minute to execute.\r\n\r\nIt will add a new subfolder and a new file to the folder on your computer containing a copy of this codebase:\r\n\r\n1. A `/node_modules/` folder _(important, and note that its contents will be different than in the last exercise)_.\r\n2. A `/package-lock.json` file _(unnecessary for this exercise but not hurting anything)_.\r\n\r\n---\r\n\r\n## Running tests that pass\r\n\r\nOpen up a command line interface.\r\n\r\nEnsure that its prompt indicates that your commands will be running within the context of the folder into which you downloaded a copy of this codebase.\r\n\r\nRun the following command:\r\n\r\n```sh\r\nnpm run test\r\n```\r\n\r\nThe output you will see will say something like:\r\n\r\n```\r\n\u003e test\r\n\u003e cross-env NODE_ENV=test jest --testTimeout=10000 --detectOpenHandles\r\n\r\n PASS  src/__tests__/my-first-test.js\r\n  Homepage Hello\r\n    √ GET / return hello world (135 ms)\r\n    √ GET /index.html return hello world (16 ms)\r\n\r\nTest Suites: 1 passed, 1 total\r\nTests:       2 passed, 2 total\r\nSnapshots:   0 total\r\nTime:        1.261 s, estimated 2 s\r\nRan all test suites.\r\n```\r\n\r\n---\r\n\r\n## Running tests that fail\r\n\r\nEdit the contents of line 12 of the file at `/src/__tests__/my-first-test.js` and replace the word \"**World**\" with the word \"**Goodbye**\" so that instead of looking for `\"Hello World!\"` your test is looking for `\"Hello Goodbye!\"`.\r\n\r\nOpen up a command line interface.\r\n\r\nEnsure that its prompt indicates that your commands will be running within the context of the folder into which you downloaded a copy of this codebase.\r\n\r\nRun the following command:\r\n\r\n```sh\r\nnpm run test\r\n```\r\n\r\nThe output you will see will say something like this, complaining that the homepage, both when accessed as `http://localhost:3000/` and as `http://localhost:3000/index.html`, says \"**Hello World**\" instead of saying \"**Hello Goodbye** as expected by the unit test _(after all, you just finished turning your unit test into a [Beatles fan](https://en.wikipedia.org/wiki/Hello,_Goodbye))_:\r\n\r\n```\r\n\u003e test\r\n\u003e cross-env NODE_ENV=test jest --testTimeout=10000 --detectOpenHandles\r\n\r\n FAIL  src/__tests__/my-first-test.js\r\n  Homepage Hello\r\n    × GET / return hello world (146 ms)\r\n    × GET /index.html return hello world (20 ms)\r\n\r\n  ● Homepage Hello › GET / return hello world\r\n\r\n    expect(received).toEqual(expected) // deep equality\r\n\r\n    Expected: \"Hello Goodbye!\"\r\n    Received: \"Hello World!\"\r\n\r\n      10 |         expect(supertest_response.status).toEqual(200);\r\n      11 |         expect(supertest_response.type).toEqual('text/html');\r\n    \u003e 12 |         expect(supertest_response.text).toEqual(\"Hello Goodbye!\");\r\n         |                                         ^\r\n      13 |     }\r\n      14 |     it(\"GET / return hello world\", async () =\u003e {\r\n      15 |         const res = await supertest(my_express_server).get(\"/\");\r\n\r\n      at toEqual (src/__tests__/my-first-test.js:12:41)\r\n      at Object.check_homepage_expectations (src/__tests__/my-first-test.js:16:9)\r\n\r\n  ● Homepage Hello › GET /index.html return hello world\r\n\r\n    expect(received).toEqual(expected) // deep equality\r\n\r\n    Expected: \"Hello Goodbye!\"\r\n    Received: \"Hello World!\"\r\n\r\n      10 |         expect(supertest_response.status).toEqual(200);\r\n      11 |         expect(supertest_response.type).toEqual('text/html');\r\n    \u003e 12 |         expect(supertest_response.text).toEqual(\"Hello Goodbye!\");\r\n         |                                         ^\r\n      13 |     }\r\n      14 |     it(\"GET / return hello world\", async () =\u003e {\r\n      15 |         const res = await supertest(my_express_server).get(\"/\");\r\n\r\n      at toEqual (src/__tests__/my-first-test.js:12:41)\r\n      at Object.check_homepage_expectations (src/__tests__/my-first-test.js:20:9)\r\n\r\nTest Suites: 1 failed, 1 total\r\nTests:       2 failed, 2 total\r\nSnapshots:   0 total\r\nTime:        1.495 s, estimated 2 s\r\nRan all test suites.\r\n```\r\n\r\n---\r\n\r\n## Anxious?  Go ahead and let the test pass again\r\n\r\nP.S.  If leaving a failing test lying around is really bothering you, feel free to either:\r\n\r\n1. Change \"`Goodbye`\" back to \"`World`,\" or\r\n2. Engage in some \"[test-driven-development](https://en.wikipedia.org/wiki/Test-driven_development)\" _(\"TDD\")_ by updating line 6 of `/src/web/server.js` from \"`World`\" to \"`Goodbye`.\"\r\n\r\nThen run `npm run test` one more time to set your heart at ease.\r\n\r\n---\r\n\r\n## Extra credit -- build and run\r\n\r\nYou're not _forbidden_ from building and running this server just because it's not the focus of this exercise.\r\n\r\nGo ahead -- try `npm run build` and then `node ./dist/server.js` against this codebase just like you did against the last one.\r\n\r\nIn fact, try it with the server saying \"`World`\" and the test expecting \"`Goodbye`.\"\r\n\r\nDid you notice that nothing at all goes wrong with the build process or with visiting your website on `localhost`?\r\n\r\n_(Although when you visit it, it will say \"`World`,\" not \"`Goodbye`\" -- remember, that's what you told the server to do.)_\r\n\r\nBy **deliberately** running `npm run build` and `node ./dist/server.js` even when you _knew_ that `npm run test` would be angry with you, you've arguably \"built\" a runtime out of \"bad code\" and chosen to run \"bad code\" on a webserver.\r\n\r\nLuckily, you've only done so on your local machine, but as we work our way through this series, we'll make sure to build codebases and habits that prevent us from running `npm run build` if `npm run test` failed.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkkgthb%2Fweb-site-nodejs-02-tests","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkkgthb%2Fweb-site-nodejs-02-tests","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkkgthb%2Fweb-site-nodejs-02-tests/lists"}