Weeknotes 4 - Learning AWS, debugging Node.js, and stubbing with Jest

☁️

This week I started studying for the AWS Certified Developer - Associate certification. I’m following the Ultimate AWS Certified Developer Associate 2021 course on Udemy. Other than that, I’m using the test quizzes from the AWS Certified Developer course on A Cloud Guru. I’m testing the water, and I don’t have a solid deadline set yet. So far, I finished the sections about IAM and EC2, and there is a lot of overlap with the AWS Certified Cloud Practitioner certification, which I passed in 2019.

🐛🔫

I’m using VS Code at the moment, and we are writing Node.js microservices predominantly. Some of my colleagues use more heavyweight IDEs, such as IntelliJ IDEA, which provides better debugging tools(breakpoints, highlighting weird formats/missing variables etc.). Although I never felt the need for an IDE when working with dynamically typed languages, I started considering other tools or getting out more of VS Code.

I trialled the Node.js debugging tool:

To enable, open up the command palette

⇧⌘P

and select > Toggle Auto Attach

Then select the mode for auto attach. I use the always mode, which allows me to use breakpoint anywhere. There is a smart mode that ignores scripts in the node_modules folder. Unfortunately, smart mode did not work on test code. Apart from occasionally being slow, breakpoints can be helpful, although you can avoid using them with good tests and logs.

🧪

As said, driving your development with tests can significantly reduce the amount of time spent on debugging. I always have to look up the documentation for test doubles when I am testing with Jest, so I’ll close my notes with an example of creating a Stub.

// in this example retrieveUser is calling the getUserRecord function to get records from the db

const { getUserRecord } = require("../module-to-stub")
const { retrieveUser } = require("../retrieve-user")

jest.mock("../module-to-stub", () => ({
  getUserRecord: jest.fn(),
}))

describe("retrieve user", () => {
  it("can retrieve a user", async () => {
    getUserRecord.mockImplementation(() => ({
      userName: "Batman",
      favouriteDrink: "lemon tea",
    }))

    const result = await retrieveUser()
    expect(result.statusCode).toBe(200)
  })

  it("can handle when the user is not in the db", async () => {
    getUserRecord.mockImplementation(() => {
      throw new UserNotFoundError()
    })

    const result = await retrieveUser()
    expect(result.statusCode).toBe(404)
  })
})