I'm relatively new to programming and have never written tests. I want to write unit tests for a group project (Angular web app with node background environment written in typescript), which you can find here. I picked out an examples which I want to test and I hope with your answers I can transfer what I learn to other parts of the project. Here is the piece of code in authentication.ts:

import {Request, Response} from 'express';

export function authenticatedUser(req: Request, res: Response, next: Function) {
    if (!req.user) {
        res.statusCode = 403;
        res.send('You are not logged in.');

I would like to write a unit test with Jasmine and run it with Karma in the web browser. My file authentication.spec.ts looks like this right now:

import jasmine;
import { authenticatedUser } from './authentication'

describe("authentication", () => {
    it("should authenticate user", () => {

I don't know how to proceed and if there is an if statement, do I need to test for true and false?


if there is an if statement, do I need to test for true and false?



A Test is meant to exercise your code and show how it works.

  • It shows the happy paths where everything works.
  • It shows the unhappy paths where something goes wrong and how that is communicated.

If I want to use this function/object/API/software system, I should be able to look at these tests and understand what will happen when I pass X in. Will it return null, return an object, throw an exception, will it block, is it asynchronous?

Now your code has a branch: if true do X else do Y.

Lets say that you test for branch X, but do not test branch Y. Is Y valid?

Obviously Y must compile fine, but some languages are pretty liberal about the definition of "compiled fine". JavaScript is in that liberal happy go lucky community. That isn't a bad thing, it just does not make "compiled fine" a satisfying statement about it being valid.

If the language has no problem with it, why is it even important that Branch Y is valid?

Flip that on its head, why is it important that Branch X is valid? The answer will probably be something like "Branch X is business behaviour", "Branch X is needed by feature Z", or "Branch X is when everything works". Branch Y represents the contra-case: when the Business Behaviour is different, or the feature does not need X, or something went wrong. In short there is a valid scenario where Branch Y will be picked. Action: Describe that scenario in the test.

That was a simplification, there is one more case: "Branch Y is never used". Action: Delete that branch.

For context consider GoTo Fail. This bug could have easily been identified by a series of tests that exercised each branch. Not doing so opened up that software to misbehaviour.

Further Learning

I would search YouTube for a few videos, and elsewhere for a book:

  • Look for any of Kent Beck's videos on Unit Testing, also find one of his unit testing/extreme programming books.
  • Look for a presentation called GUTS by Kevlin Henney, also look at his other presentations about errors.

As for syntax refer to the Jasmine Documentation.

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.