RESTful Node.js: A Structured Approach
  • Book Cover
  • About the Author
  • Links and Resources
  • Part I: The Why
    • Foreword
    • Preface
    • Chapter 1: Introduction
      • The Rise of REST and Distributed Systems
      • Problem #1: Structureless Design, Structureless REST
      • The Emergence of JavaScript and Node.js
      • Problem #2: Structureless JavaScript, Structureless Node.js
      • Behold, the Solution: A Structured Approach
      • Summary
  • Part 2: The Theory
    • Chapter 2: REST Origins
      • A Brief History of the Web and the Birth of REST
      • REST vs. HTTP
      • REST - The Abstract Web Architecture
      • HTTP - A Peak at REST's Concrete Implementation
      • What does it mean for an API to be RESTful?
      • Measuring "RESTfulness" with Richardson Maturity Model
      • Pragmatic REST vs Dogmatic REST
      • Summary
    • Chapter 3: RESTful API Design Guidelines and "Best Practices"
      • Theories vs. Principles vs. Guidelines
      • URI Design
      • Method Verbs
      • Status Codes
      • Representational Design
      • Metadata Design
      • Versioning Strategies
      • Security Considerations
      • Documentation
      • Case Study: GitHub
      • Summary
    • Chapter 4: Structured JavaScript Architecture
      • The Monstrous Monolith and Its Downfall
      • Layered/N-Tier Architecture: The Unpopular Proven Way
      • Microservices and Distributed Computing: A Popular Misdirection
      • Summary
    • Chapter 5: The 8 Step Recipe
      • Route Name (URI)
      • Input Request
      • Middleware
      • Validation
      • Domain
      • Events
      • Output Response
      • Test, Refactor, Document
      • Summary
  • Part 3: The Code
    • Chapter 6: Introduction to the Bookstore API
      • The Bookstore API Endpoint Specifications
      • API Design and Code Structure
      • Project Setup
      • Summary
    • Chapter 7: Retrieving Books from our API
      • Retrieving All Books - Planning
      • Retrieving All Books - Implementation
      • Retrieving A Book By ID - Planning
      • Retrieving A Book By ID - Implementation
      • Summary
    • Chapter 8: Adding Authentication to our API
      • Registering the User - Planning
      • Registering the User - Implementation
      • Logging the User In - Planning
      • Logging the User In - Implementation
      • Getting Authenticated User - Planning
      • Getting Authenticated User - Implementation
      • Summary
    • Chapter 9: Adding the Create, Update, and Delete Operations to our API
      • Creating A Book Listing - Planning
      • Creating A Book Listing - Implementation
      • Updating A Book Listing By ID - Planning
      • Updating A Book Listing By ID - Implementation
      • Deleting A Book Listing By ID - Planning
      • Deleting A Book Listing By ID - Implementation
      • Summary
    • Chapter 10: Testing our API
      • Testing the Request
      • Testing the Middleware
      • Testing the Validation
      • Testing the Domain
      • Testing the Event
      • Testing the Response
      • Testing the Controller
      • Integration Test
      • Summary
  • Conclusion
    • Final Words
  • Bonus!
    • Refactoring to HATEOAS
  • Appendix
    • Sources & References
Powered by GitBook
On this page
Edit on GitHub
  1. Part 3: The Code
  2. Chapter 10: Testing our API

Testing the Controller

Recall how we implemented the registerUser controller.

File: src/controllers/auth/registerUser.js

const catchExceptions = require('../../utils/catchExceptions')
const globalResponseDto = require('../../responses/globalResponseDto')
const userResponseDto = require('../../responses/userResponseDto')
const registerUserRequestDto = require('../../requests/registerUserRequestDto')
const registerUserValidator = require('../../validators/registerUserValidator')
const authService = require('../../domain/services/auth.service')
const EventEmitter = require('events')
const eventEmitter = new EventEmitter()

/**
 * Inserts the user into the database and fires off an email notification to that user's email if successful.
 */
const registerUser = catchExceptions(async (req, res) => {
  const registerUserRequest = registerUserRequestDto(req.body)

  registerUserValidator(registerUserRequest)

  const user = await authService.registerUser(registerUserRequest)

  eventEmitter.emit('userHasRegistered', user)

  res.json(
    globalResponseDto({
      status: 'success',
      code: 200,
      message: `The email: ${registerUserRequest.email} has successfully registered.`,
      data: userResponseDto(user),
      errors: null
    })
  )
})

module.exports = registerUser

The Test

Now because our controller has been kept nice and thin up until this point and have barely any logical statements other than the functions and services we've created, there's actually very little to test. If you've done your job correctly, then you should not have to test the controller what so ever because all of your tests would have been done in those other layers.

However, since this is an educational book, we will do it for demonstration purposes.

In order to test the controller, the key is to mock every service that its using and see if they've been called or not.

Check out the following test case as we mock the registerUserRequestDto, the registerUserValidator, the authService.register function, and the express req and res.

File: src/controllers/auth/__tests__/registerUser.test.js

const registerUserController = require('../registerUser')

const registerUserRequestDto = require('../../../requests/registerUserRequestDto')
const registerUserValidator = require('../../../validators/registerUserValidator')
const authService = require('../../../domain/services/auth.service')

jest.mock('../../../requests/registerUserRequestDto', () =>
  jest.fn((data) => data)
)
jest.mock('../../../validators/registerUserValidator', () =>
  jest.fn((data) => data)
)
jest.mock('../../../domain/services/auth.service', () => {
  return {
    registerUser: jest.fn((data) => data)
  }
})
const mockRequest = () => ({
  body: {
    first_name: 'john',
    last_name: 'doe'
  }
})

const mockResponse = () => {
  const res = {}

  res.status = jest.fn().mockReturnValue(res)
  res.json = jest.fn().mockReturnValue(res)

  return res
}

describe('Controler - Register User', () => {
  test('User should be registered successfully', async () => {
    const req = mockRequest()
    const res = mockResponse()

    await registerUserController(req, res)

    const randomUserFromBodyRequest = {
      first_name: 'john',
      last_name: 'doe'
    }

    expect(registerUserRequestDto).toHaveBeenCalledWith(
      randomUserFromBodyRequest
    )

    expect(registerUserValidator).toHaveBeenCalledWith(
      randomUserFromBodyRequest
    )

    expect(authService.registerUser).toHaveBeenCalledWith(
      randomUserFromBodyRequest
    )

    expect(res.status).toHaveBeenCalledWith(201)
    expect(res.json).toHaveBeenCalled()
  })
})
PreviousTesting the ResponseNextIntegration Test

Last updated 3 years ago