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
  • The Passing Test
  • The Failing Test
Edit on GitHub
  1. Part 3: The Code
  2. Chapter 10: Testing our API

Integration Test

Now comes my favorite part of the test, the high level API integration test. This is where we call the API endpoint directly as if we are posing as a real client.

The idea here is to set the application up with an open port and build a new database along with some fake seed data.

Then, make the API call programmatically with an HTTP library like fetch.

Finally, we make an assertion and check if the response is what we would expect from the client.

Here is how we'll be setting this test up.

File: tests/api/auth/register.test.js

const fetch = require('node-fetch')
const api = require('../../../src/server')

const apiPort = Math.round(Math.random() * 65535)
const baseURL = `http://localhost:${apiPort}/api/v1`

const db = require('../../../src/utils/db')
let dbConnection
const dbTestUtils = require('../../../tests/testUtils/dbTestUtil')

/**
 * 1. Arrange
 *  - setup the world
 */
beforeAll(async () => {
  await api.listen(apiPort) // start the application
  dbConnection = await db() // start the database
})

beforeEach(async () => {
  await dbTestUtils.setUpDatabase()
})

afterEach(async () => {
  await dbTestUtils.clearDatabase()
})

afterAll(async () => {
  await api.close()
  await dbConnection.disconnect()
})

/**
 * 2. Act
 *  - make the http call
 * 3. Assert
 *  - response check
 */
describe('API Test - Register User', () => {
  // Tests go here
})

The Passing Test

The first test is our happy path, so no errors. This is simple, make sure all the input requests are correct and that when we call the API, the user is successfully registered.

test('POST /api/v1/users - user successfully registered', async () => {
  const user = {
    first_name: 'Yichen',
    last_name: 'Zhu',
    email: 'yichen@yichen.com',
    password: 'password123',
    password_confirmation: 'password123',
    phone_number: '1234567890'
  }

  const response = await (
    await fetch(`${baseURL}/users`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify(user)
    })
  ).json()

  delete user.password
  delete user.password_confirmation

  expect(response).toMatchObject({
    status: 'success',
    code: 201,
    message: `The email: ${user.email} has successfully registered.`,
    data: user
  })
})

The Failing Test

For the second test, let's have the API throw some sort of error. One possible scenario is if the user enters passwords do not match.

test('POST /api/v1/users - fail due to invalid form inputs', async () => {
  const user = {
    first_name: 'Yichen',
    last_name: 'Zhu',
    email: 'yichen@yichen.com',
    password: 'password',
    password_confirmation: 'password_not_same',
    phone_number: '1234567890'
  }

  const response = await (
    await fetch(`${baseURL}/users`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify(user)
    })
  ).json()

  expect(response).toMatchObject({
    code: 400,
    data: null,
    message: 'There were errors with the validation.',
    status: 'error',
    errors: ['The password confirmation and password fields must match.']
  })
})
PreviousTesting the ControllerNextSummary

Last updated 3 years ago