Intro To JavaScript Unit Testing

describe(‘calculator’, function() {// describes a module with nested “describe” functionsdescribe(‘add’, function() {// specify the expected behaviorit(‘should add 2 numbers’, function() {//Use assertion functions to test the expected behavior})})})
it(‘should do something’, function() {//arrange…var dummyData = { foo: ‘bar’ };var expected = ‘the result we want’;//act…var result = functionUnderTest(dummyData);//assert…expect(result).to.equal(expected);});
import user from ‘./user’ // using jest for this exampledescribe(‘user’, () => {// before each testbeforeEach(() => {// spy on the “isValid” function on the “user” objectjest.spyOn(user, ‘isValid’);})// after each testafterEach(() => {// remove the mock from the “isValid” functionuser.isValid.mockRestore();})it(‘is validated for valid users’, () => {user.isValid.mockReturnValue(true)// Testing someFn with user where user.isValid() returns trueassert(validate(user))})it(‘does not validated for invalid users’, () => {user.isValid.mockReturnValue(false)// Testing someFn with user where user.isValid() returns trueassert(!validate(user))})})

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store