Skip to content

Commit

Permalink
Make jest-mock return an object (jestjs#2410)
Browse files Browse the repository at this point in the history
Right now it returns a class that you've got to instantiate which is not the API we want. The _ hack is ugly but I think we can live with it.

Test Plan:
Make sure tests are passing

Also make sure that we can write this :)
```js
var mock = require('jest-mock');
const fn = mock.getMockFn();
```
  • Loading branch information
vjeux authored and cpojer committed Jan 8, 2017
1 parent b2b2039 commit fc96580
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 13 deletions.
5 changes: 3 additions & 2 deletions packages/jest-environment-jsdom/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
import type {Config} from 'types/Config';
import type {Global} from 'types/Global';
import type {Script} from 'vm';
import type {ModuleMocker} from 'jest-mock';

const FakeTimers = require('jest-util').FakeTimers;
const installCommonGlobals = require('jest-util').installCommonGlobals;
const ModuleMocker = require('jest-mock');
const mock = require('jest-mock');

class JSDOMEnvironment {

Expand All @@ -34,7 +35,7 @@ class JSDOMEnvironment {
this.global.Error.stackTraceLimit = 100;
installCommonGlobals(global, config.globals);

this.moduleMocker = new ModuleMocker(global);
this.moduleMocker = new mock.ModuleMocker(global);
this.fakeTimers = new FakeTimers(global, this.moduleMocker, config);
}

Expand Down
5 changes: 3 additions & 2 deletions packages/jest-environment-node/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
import type {Config} from 'types/Config';
import type {Global} from 'types/Global';
import type {Script} from 'vm';
import type {ModuleMocker} from 'jest-mock';

const FakeTimers = require('jest-util').FakeTimers;
const installCommonGlobals = require('jest-util').installCommonGlobals;
const ModuleMocker = require('jest-mock');
const mock = require('jest-mock');
const vm = require('vm');

class NodeEnvironment {
Expand All @@ -35,7 +36,7 @@ class NodeEnvironment {
global.setInterval = setInterval;
global.setTimeout = setTimeout;
installCommonGlobals(global, config.globals);
this.moduleMocker = new ModuleMocker(global);
this.moduleMocker = new mock.ModuleMocker(global);
this.fakeTimers = new FakeTimers(global, this.moduleMocker, config);
}

Expand Down
4 changes: 2 additions & 2 deletions packages/jest-mock/src/__tests__/jest-mock-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ describe('moduleMocker', () => {
let moduleMocker;

beforeEach(() => {
const ModuleMocker = require('../');
const mock = require('../');
const global = vm.runInNewContext('this');
moduleMocker = new ModuleMocker(global);
moduleMocker = new mock.ModuleMocker(global);
});

describe('getMetadata', () => {
Expand Down
7 changes: 5 additions & 2 deletions packages/jest-mock/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,10 @@ function getSlots(object?: Object): Array<string> {
}


class ModuleMocker {
class ModuleMockerClass {
_environmentGlobal: Global;
_mockRegistry: WeakMap<Function, MockFunctionState>;
ModuleMocker: Class<ModuleMockerClass>;

/**
* @see README.md
Expand All @@ -185,6 +186,7 @@ class ModuleMocker {
constructor(global: Global) {
this._environmentGlobal = global;
this._mockRegistry = new WeakMap();
this.ModuleMocker = ModuleMockerClass;
}

_ensureMock(f: Mock): MockFunctionState {
Expand Down Expand Up @@ -564,4 +566,5 @@ class ModuleMocker {
}
}

module.exports = ModuleMocker;
export type ModuleMocker = ModuleMockerClass;
module.exports = new ModuleMockerClass(global);
2 changes: 1 addition & 1 deletion packages/jest-runtime/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type {Console} from 'console';
import type {Environment} from 'types/Environment';
import type {HasteContext} from 'types/HasteMap';
import type {ModuleMap} from 'jest-haste-map';
import type ModuleMocker, {MockFunctionMetadata} from 'jest-mock';
import type {MockFunctionMetadata, ModuleMocker} from 'jest-mock';

const HasteMap = require('jest-haste-map');
const Resolver = require('jest-resolve');
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-util/src/FakeTimers.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import type {Config} from 'types/Config';
import type {Global} from 'types/Global';
import type ModuleMocker from 'jest-mock';
import type {ModuleMocker} from 'jest-mock';

const {formatStackTrace} = require('./messages');
const setGlobal = require('./setGlobal');
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-util/src/__tests__/FakeTimers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ describe('FakeTimers', () => {

beforeEach(() => {
FakeTimers = require('../FakeTimers');
const ModuleMocker = require('jest-mock');
const mock = require('jest-mock');
const global = vm.runInNewContext('this');
moduleMocker = new ModuleMocker(global);
moduleMocker = new mock.ModuleMocker(global);
});

describe('construction', () => {
Expand Down
2 changes: 1 addition & 1 deletion types/Environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import type {Config} from './Config';
import type {Global} from './Global';
import type {Script} from 'vm';
import type ModuleMocker from 'jest-mock';
import type {ModuleMocker} from 'jest-mock';

export type Environment = {|
constructor(config: Config): void;
Expand Down

0 comments on commit fc96580

Please sign in to comment.