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?
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With