ts-mockery icon indicating copy to clipboard operation
ts-mockery copied to clipboard

feat: Reset mock extensions

Open getsaf opened this issue 4 years ago • 1 comments

Currently, using Mock.extend will mutate the thing being extended. eg:

foo.ts

export const FOO = {
  bar: 'bar';
};

example.test.ts

import { FOO } from './foo';

describe('mutations', () => {
  it('can mock FOO.bar', () => {
    Mock.extend(FOO).with({bar: 'fake bar'});
    expect(FOO.bar).toBe('fake bar');
  });
  it('bleeds mock data across tests!!', () => {
    // This will FAIL if tests are run in definition-order because
    // FOO.bar still equals the mock!
    expect(FOO.bar).toBe('bar');
  });

This change will auto-reset mock extensions to their original values in an automatic afterEach.

getsaf avatar Nov 06 '19 15:11 getsaf

Thoughts

Should we make this configurable? As it is, this change could cause breakages in projects that "depend" on data-bleed across tests.

getsaf avatar Nov 07 '19 14:11 getsaf