Codementor Events

WRITING TESTS FOR IMAGE/FILE UPLOADS IN NODEJS.

Published Nov 11, 2020
WRITING TESTS FOR IMAGE/FILE UPLOADS IN NODEJS.

Writing tests generally for code written is part of quality assurance. Tests are a set of written code against business logic to see how the logic renders against certain inputs and conditions.

In this tutorial, we will make use of mocha, chai and chai-http to run tests. The code for this project can be found here. In the tests folder inside src, there are a couple of tests cases/suites in it:

import chai from 'chai';
import fs from 'fs';
import chaiHttp from 'chai-http';
import app from '../app';


const { expect } = chai;


chai.use(chaiHttp);

describe('Test for user endpoint', () => {
    describe('Test for POST route', () => {
        it('should return 201 and create user with single image upload', async() => {
            const res = await chai.request(app)
                .post('/users')
                .set('content-type', 'multipart/form-data')
                .field('email', 'myemail@gmail.com')
                .field('firstname', 'slim')
                .field('lastname', 'shady')
                .attach('image', fs.readFileSync(`${__dirname}/file.png`), 'tests/file.png')
                expect(res.status).to.equal(201);
        })

        //anoter post test
        it('should return 201 and create user with multiple image upload', async() => {
            const res = await chai.request(app)
                .post('/users')
                .set('content-type', 'multipart/form-data')
                .field('email', 'busybody@gmail.com')
                .field('firstname', 'busy')
                .field('lastname', 'body')
                .attach('image', fs.readFileSync(`${__dirname}/file.png`), 'tests/file.png')
                .attach('image', fs.readFileSync(`${__dirname}/chris1.pdf`), 'tests/chris1.pdf')
                expect(res.status).to.equal(201);
        })
    })
})

In the above, we have imported a couple of packages, chai for assertion, fs(node's file sysytem), chai-http for integration testing and the app file where all imports are consolidated in a nodejs app, we then went futher to use chai-http via chai.use(). It is worthy to note that the first it block caters for a single file upload while the second one caters to multiple file uploads. Notice the set, field and attach properties of the chai.request(app) object.

  • multipart/form-data is being used because it is supported by all browsers(and of course the files will be uploaded from the client/frontend).
  • In the field() method, we set key-value pairs as arguments.
  • attach() method accepts two arguments, the name of the file field and the file path itself embedded in fs.readFileSync() method.

Multiple files like in the second it block can be uploaded using attach() on different lines.

Below is the result when the tests in the project are ran:Screen Shot 2020-08-06 at 1.10.34 PM.png
Screen Shot 2020-08-06 at 1.10.43 PM.png

The extra results are log statements added to the tests to ascertain what the tests return against the code.

It's advisable to write tests (unit, integration and e2e) for all projects to guarantee quality and ensure that addition of further features do not break existing funstionality.

Discover and read more posts from Seun Somefun
get started