2024 03 11 - TheEvergreenStateCollege/upper-division-cs-23-24 GitHub Wiki
Week 10 - Monday
11 March 2024
Web Infra - Morning Session
First Hour
Quiz, read the following screenshots and follow their instructions.
Second Hour
We will apply unit tests to our API code for our final project.
We will use the testing frameworks jest
and supertest
for NodeJS / Express servers.
You may use equivalent libraries and metaphors in Python, such as this tutorial.
Installing dependencies
Change to your final project directory. Don't worry, nothing will be changed about your existing code,
only a new test-only directory will be added, and some (dev) dependencies in package.json
npm i -D test supertest
After you install dependencies, your package.json
should look similar to this.
An important line you'll need to add is the test
command in the scripts
key, that runs jest
in the current directory.
"scripts": {
"test": "jest .",
You should also install the jest
command as a global binary, that you can share across all projects.
npm i -g jest
and then check you can run it
which jest
should return the location of the jest
file on your system, not an empty line.
tests
directory
Create a mkdir tests
Try a basic unit test
You'll create two new files, a simple dummy module that exports one simple function to-be-tested, and a unit test for it. They will be named and located as follows
├── src
│ ├── double.js
├── tests
│ ├── double.test.js
First, try putting the first version of this code in a new file in your src
directory, or somewhere you can easily import it,
called double.js
.
// not testable
const value = 100;
const double = () => {
return value * 2;
};
module.exports = {
double
}
It is meant to be simple and unrelated to your code at first.
In your new test directory, write a unit test for it using Jest in a file called double.test.js
const { double } = require('../src/double.js');
describe("double function", () => {
it("should return twice its argument", async () => {
// .,...
expect(double(25)).toBe(50);
});
});
Running and seeing errors
Now try running all tests that are findable, by typing and pressing enter
npm test
You'll see the above error at first.
What causes it? Discuss with a pair programming partner, then iterate:
- discuss the root cause of the problem, suggest a modification.
- make the modifications
- when you both agree there is an 80% chance of success, run
npm test
again - repeat
API Unit Testing
Now type in the following Javascript (not Typescript) file into a new tests/postSignUp.test.js
file
const { app } = require("../src/server");
const { request } from "supertest";
describe("POST /signup", function () {
it("responds with json", async () {
const res = await request(app)
.post("/signup")
.send({ username: "hello", password: "hola" })
.set("Accept", "application/json")
expect(res.headers["Content-Type"]).toMatch(/json/);
expect(res.status).toEqual(200);
});
});
Start your Express server, including any SSH tunnel needed to your database, and in a separate terminal, run the test.
npm test
With a pair programming or team partner, iterate the same procedure to make progress towards the test passing.
- discuss the root cause of the problem, suggest a modification.
- make the modifications
- when you both agree there is an 80% chance of success, run
npm test
again - repeat
You may start a dev diary with screenshots and error stack traces to document your process, in place of having a code interview.