npm i --save-dev jest
package.json
...
"scripts": {
"test": "jest --watchAll --verbose --coverage" // --env node
},
"jest": {
"testEnvironment": "node"
}
...
package.json
"scripts": {
...
"test": "jest --watchAll --coverage --verbose --silent --runInBand"
}
jest.config.js
module.exports = {
setupFilesAfterEnv: ["./jest.setup.js"]
};
jest.setup.js
jest.setTimeout(30000);
run only this test
npm test -- -t "should register a new user"
// -t: the title of the test
strings.js
function sayHelloTo(person) {
return `Hi, ${person}!`;
}
module.exports = sayHelloTo;
strings.spec.js
const sayHelloTo = require("./../strings");
describe("The string package", () => {
describe("the sayHelloTo function", () => {
it("should return 'Hi, Peter!' if the argument is 'Peter'", () => {
const actual = sayHelloTo("Peter");
const expected = "Hi, Peter!";
expect(actual).toBe(expected);
});
});
});
classes.js
class User {
constructor(name, email) {
this.name = name;
this.email = email;
this.errors = [];
}
validateName() {
if (this.name) {
if (this.name.length < 5) {
this.errors.push("the name must be at least 5 chars long");
}
} else {
this.errors.push("the name is required");
}
}
}
module.exports = User;
classes.spec.js
const User = require("./../classes");
describe("The User class", () => {
it("should create a new user", () => {
const user1 = new User("smith", "[email protected]");
expect(user1).toEqual({
name: "smith",
email: "[email protected]",
errors: []
});
});
describe("The validateName function", () => {
it("should create new error message if the user name is less than 5 characters", () => {
const user2 = new User("tom", "[email protected]");
user2.validateName();
expect(user2.errors).toEqual(["the name must be at least 5 chars long"]);
});
});
});
classes.js
class User {
constructor(name, email) {
this.name = name;
this.email = email;
this.errors = [];
}
validateName() {
if (this.name) {
if (this.name.length < 5) {
this.errors.push("the name must be at least 5 chars long");
}
} else {
this.errors.push("the name is required");
}
}
validateEmail() {
console.log("validating email...");
}
validatePassword() {
console.log("validating password...");
}
isValid() {
this.validateName();
this.validateEmail();
this.validatePassword();
}
}
module.exports = User;
classes.spec.js
describe("The isValid function", () => {
it("should call validateName, validateEmail, validatePassword functions when isValid fn is called", () => {
// arrange
const user = new User();
jest.spyOn(user, "validateName");
jest.spyOn(user, "validatePassword");
jest.spyOn(user, "validateEmail");
// action
user.isValid();
// assertion
expect(user.validatePassword).toHaveBeenCalled();
expect(user.validateName).toHaveBeenCalled();
expect(user.validateEmail).toHaveBeenCalled();
});
});
const next = jest.fn();
....
expect(next).toHaveBeenCalled();
- beforeEach
- afterEach
- beforeAll
- afterAll
npm i --save-dev supertest
const supertest = require("supertest");
const app = require("../src/app");
const server = supertest(app)
app.js
if(!module.parent) {
app.listen(3000);
}
This gist is part of:
Additional Info: