Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to encapsulate mocha `expect()` code?

I'm trying to test for the presence of some api response properties that I want to require across all tests (a status and data property).

Here's a generic test that asserts the desired properties in a supertest expect() method:

    it('should create a widget', done => {
        let status = 200;
        request(test_url)
            .post('/api/widgets')
            .set('Authorization', `Bearer ${token}`)
            .send({
                sku: my_widget_data.sku,
                name: my_widget_data.name,
                description: ''
            })
            .expect(res => {
                assert(
                    Object.keys(res.body).includes('status'),
                    '`status` is a required field'
                );
                assert(
                    Object.keys(res.body).includes('data'),
                    '`data` is a required field'
                );
                assert.strictEqual(res.body.status, status);
                assert.strictEqual(res.status, status);
            })
            .end((err, res) => {
                if (err) return done(err);
                done();
            });
    });

This expect() behavior is going to be common to almost all my tests.

How can I extract the expect() behavior to DRY up my tests, while still passing arbitrary status numbers?

like image 727
doub1ejack Avatar asked Mar 25 '19 16:03

doub1ejack


1 Answers

You could extrapolate the function that expect() calls into another one which returns a function you pass status into:

export function statusCheck(status) {
  return res => {
    assert(
      Object.keys(res.body).includes("status"),
      "`status` is a required field",
    )
    assert(Object.keys(res.body).includes("data"), "`data` is a required field")
    assert.strictEqual(res.body.status, status)
    assert.strictEqual(res.status, status)
  }
}

Now in your original file, you could call:

.expect(statusCheck(200))

Here's a snippet showing how it works as well:

// Ignored since status is scoped below
const status = 400

// Returns your (res) => {} function, uses status
function statusCheck(status) {
  return res => {
    console.log(`Desired status number is ${status}`)
    if(res.status === status) console.log(`Response: ${res.status}, It worked!`)
    else console.log(`Response: ${res.status}, It failed!`)
  }
}

// Testing if it works with a mockup
const thisGoesInsideExpect = statusCheck(200)
const res = {status: 200}
thisGoesInsideExpect(res)
like image 168
Leander Rodrigues Avatar answered Sep 23 '22 01:09

Leander Rodrigues