Skip to content

Instantly share code, notes, and snippets.

@vzaidman
Last active November 21, 2021 16:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vzaidman/0900f1eadf5aa980d82fcd9912eadb69 to your computer and use it in GitHub Desktop.
Save vzaidman/0900f1eadf5aa980d82fcd9912eadb69 to your computer and use it in GitHub Desktop.
import user from './user'
describe('user', () => {
// before each test
beforeEach(() => {
// spy on the "isValid" function on the "user" object
jest.spyOn(user, 'isValid');
})
// after each test
afterEach(() => {
// remove the mock from the "isValid" function
user.isValid.mockRestore();
})
it('is validated for valid users', () => {
user.isValid.mockReturnValue(true)
// Testing someFn with user where user.isValid() returns true
assert(validate(user))
})
it('does not validated for invalid users', () => {
user.isValid.mockReturnValue(false)
// Testing someFn with user where user.isValid() returns true
assert(!validate(user))
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment