You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
function isInteger(num) {
if (!Number.isInteger(Number(num))) {
throw new Error(`${num} is not an integer`);
}
return true;
}
it("should throw an error if the argument is not an integer", () => {
expect(() => {
isInteger(13.5);
}).toThrow();
});
How to compare objects
it("Should remove object's properties which have a value of null or undefined", () => {
const testObj = {
name: "smith",
age: null,
member: undefined
};
expect(removeEmpty(testObj)).toEqual({ name: "smith" });
});
Asynchronous testing
describe("Async tests", () => {
describe("findUserById fn", () => {
it("should return an user based on the passed id", async () => {
const result = await findUserById(1);
console.log(`@@ ===>`, result);
expect(result.user.name).toBe("smith");
});
});
});
Asynchronous testing: Testing error case
const findUserById = id => {
return new Promise((resolve, reject) => {
setTimeout(() => {
const user = users.find(user => user.id == id);
if (!user) {
return reject(new Error(`User with id: ${id} was not found.`));
}
return resolve({
message: "User found successfully.",
user
});
}, 500);
});
};
it("should throw an error if id is not found", () => {
return findUserById(11).catch(err => {
expect(err.message).toBe(`User with id: 11 was not found.`);
});
});
// alternative to the one above
it("should throw an error if id is not found [ASYNC/AWAIT]", async () => {
try {
await findUserById(11);
} catch (error) {
expect(error.message).toBe(`User with id: 11 was not found.`);
}
});
// just for reference
// an interesting case in which the user actually exists!
it("should throw an error if id is not found [ASYNC/AWAIT]", async () => {
try {
await findUserById(1); // there is an user with id of 1, so no error will be thrown here
throw new Error("Expected Error"); // let me throw that error for you :P
} catch (error) {
if (error === "Expected Error") {
throw error;
}
expect(error.message).toBe(`User with id: 11 was not found.`);
}
});
const request = require("supertest");
const { app } = require("./../server/app");
// use for POST endpoint test
beforeEach(done => {
// pseudo code used
dbAPI.deleteAllUsers().then( () => done() );
})
describe("Server", () => {
describe("GET /", () => {
it(`should return "Index Page"`, done => {
request(app)
.get("/")
.expect(200)
.expect("Index Page")
.end(done);
});
});
describe("POST /user", () => {
it(`should create new user`, done => {
request(app)
.post("/users")
.send({name: "John"})
.expect(201)
.expect(res => {
expect(res.body.name).toBe("John");
})
.end((err, res) => {
if(err) {
return done(err)
}
// check if user is saved in db
// pseudo code used
dbAPI.findUsers().then(users => {
// use beforeEach()
expect(users.length).toBe(1);
expect(users[0].name).toBe("John");
done();
}).catch(err => done(err))
});
});
});
describe("GET /user", () => {
it(`should return the user object`, done => {
request(app)
.get("/user/sjdkf-32983j-sdjhf")
.expect(200)
.expect(res => {
expect(res.body).toBeA("object");
})
.end(done);
});
});
});
Tesing async functions
// simple async fn
module.exports.addNumsAsync = (a, b, cb) => {
setTimeout(() => {
cb(a + b);
}, 1000);
};
// its test
const expect = require("expect");
const { addNumsAsync } = require("./../{path to the async fn}");
describe("Async methods", () => {
it("should add two numbers async", done => {
addNumsAsync(5, 4, sum => {
expect(sum).toBe(9);
done();
});
});
});
Additional info
Solving EADDRINUSE issue v2
/**
* Alternative to:
* if (!module.parent) {
* app.listen(port);
* }
*/
let server;
describe("Server", () => {
beforeEach( () => {
server = require("./../../app");
} )
afterEach( () => {
server.close();
} )
it("should...", () => {
...
});
});
Run a single test
// refer to jest section to check the contents of the test command
npm test -- -t '<title of the <describe || test || it> function of your asserion library>'