Skip to content

Instantly share code, notes, and snippets.

@ddialar
Created January 27, 2020 11:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ddialar/91dba1d18aac633d72b558696e50aff6 to your computer and use it in GitHub Desktop.
Save ddialar/91dba1d18aac633d72b558696e50aff6 to your computer and use it in GitHub Desktop.
import { orm } from '@orm';
// Other needed imports and operations...
describe('Block description', () => {
// Type error restoring the mock (mocking strategy #1)...
test('whatever test description.', async (done) => {
// Creating the spyOn...
jest.spyOn(orm.controllers, 'enableUserAccount')
// Mocking the function...
orm.controllers.enableUserAccount = jest
.fn()
.mockImplementation(() => {
throw new Error('Enabling user account Testing Error Message.')
});
// ...
// Test content...
// ...
// Restoring the function...
orm.controllers.enableUserAccount.mockRestore();
// Error: The property 'mockRestore' doesn't exist for type '(userId: number) => Promise<void>'.
done();
});
// Type error restoring the mock (mocking strategy #2)...
test('whatever test description.', async (done) => {
// Creating the spyOn and mocking the function at the same time...
jest
.spyOn(orm.controllers, 'enableUserAccount')
.mockImplementation(() => {
throw new Error('Enabling user account Testing Error Message.')
});
// ...
// Test content...
// ...
// Restoring the function...
orm.controllers.enableUserAccount.mockRestore();
// Error: The property 'mockRestore' doesn't exist for type '(userId: number) => Promise<void>'.
done();
});
// Without type error restoring the mocked function...
test('whatever test description.', async (done) => {
// Creating the spyOn and mocking the function at the same time...
jest
.spyOn(orm.controllers, 'enableUserAccount')
.mockImplementation(() => {
throw new Error('Enabling user account Testing Error Message.')
});
// ...
// Test content...
// ...
// Restoring the function...
jest.spyOn(orm.controllers, 'enableUserAccount').mockRestore();
// No type error
done();
});
)};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment