GithubHelp home page GithubHelp logo

tommygaessler / express-testing-mocha-knex Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 15 KB

This tutorial looks at how to test an Express CRUD app with Mocha and Chai!

License: MIT License

JavaScript 82.92% CSS 0.78% HTML 16.30%

express-testing-mocha-knex's Introduction

Testing Node and Express

This tutorial looks at how to test an Express CRUD app with Mocha and Chai. Although we'll be writing both unit and integration tests, the focus will be on the latter so that tests run against the database in order to test the full functionality of our app. Postgres will be used, but feel free to use your favorite relational database.

Let's get to it!

Why Test?

Are you currently manually testing your app?

When you push new code do you manually test all features in your app to ensure the new code doesn't break existing functionality? How about when you're fixing a bug? Do you manually test your app? How many times?

Stop wasting time!

If you do any sort of manual testing write an automated test instead. Your future self will thank you.

Getting Started

Project Set Up

To quickly create an app boilerplate install the following generator:

$ npm install -g [email protected]

Make sure you have Mocha, Chai, Gulp, and Yeoman installed globally as well:

Create a new project directory, and then run the generator to scaffold a new app:

$ yo galvanize-express

NOTE: Add your name for the MIT License and do not add Gulp Notify.

Make sure to review the structure.

Install the dependencies:

$ npm install

Finally, let's run the app to make sure all is well:

$ gulp

Navigate to http://localhost:3000/ in your favorite browser. You should see:

Welcome to Express!
The sum is 3

Database Set Up

Make sure the Postgres database server is running, and then create two new databases in psql, for development and testing:

# create database express_tdd;
CREATE DATABASE
# create database express_tdd_testing;
CREATE DATABASE
#

Install Knex and pg:

$ npm install [email protected] [email protected] --save-dev

Run knex init to generate a new knexfile.js file in the project root, which is used to store database config. Update the file like so:

module.exports = {
  development: {
    client: 'postgresql',
    connection: 'postgres://localhost:5432/express_tdd',
    migrations: {
      directory: __dirname + '/src/server/db/migrations'
    },
    seeds: {
      directory: __dirname + '/src/server/db/seeds'
    }
  },
  test: {
    client: 'postgresql',
    connection: 'postgres://localhost:5432/express_tdd_testing',
    migrations: {
      directory: __dirname + '/src/server/db/migrations'
    },
    seeds: {
      directory: __dirname + '/src/server/db/seeds'
    }
  }
};

Here, different database configuration is used based on the app's environment, either development or test. The environment variable NODE_ENV will be used to change the environment. NODE_ENV defaults to development, so when we run test, we'll need to update the variable to test.

Next, let's init the connection. Create a new folder with "sever" called "db" and then add a file called knex.js:

const environment = process.env.NODE_ENV;
const config = require('../../../knexfile.js')[environment];
module.exports = require('knex')(config);

The database connection is establish by passing the proper environment to knexfile.js which returns the associated object that gets passed to the knex library in the third line above.

Now is a great time to initilize a new git repo and commit!

Test Structure

With that complete, let's look at the current test structure. In the "src" directory, you'll notice a "test" directory, which as you probably guessed contains the test specs. Two sample tests have been created, plus there is some basic configuration set up for JSHint and JSCS.

Run the tests:

$ npm test

They all should pass:

jscs
  ✓ should pass for working directory (357ms)

routes : index
  GET /
    ✓ should render the index (88ms)
  GET /404
    ✓ should throw an error

jshint
  ✓ should pass for working directory (247ms)

controllers : index
  sum()
    ✓ should return a total
    ✓ should return an error


6 passing (724ms)

Glance at the sample tests. Notice how we are updating the environment variable at the top of each test:

process.env.NODE_ENV = 'test';

Remember what this does? Now, when we run the tests, knex is intilized with the test config.

Schema Migrations

placeholder

With that, let's start writing some code...

Test Driven Development

Talk about workflow

  1. GET ALL:
    • Write test
    • Watch it fail
    • Write code to get it to pass
  2. GET Single:
    • Write test
    • Watch it fail
    • Write code to get it to pass
  3. POST:
    • Write test
    • Watch it fail
    • Write code to get it to pass
  4. PUT:
    • Write test
    • Watch it fail
    • Write code to get it to pass
  5. DELETE:
    • Write test
    • Watch it fail
    • Write code to get it to pass
  6. Edge Cases?

express-testing-mocha-knex's People

Contributors

tommygaessler avatar

Watchers

 avatar John Kennedy Kalu avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.