forked from jestjs/jest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
moduleMocker-test.js
118 lines (98 loc) · 3.21 KB
/
moduleMocker-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**
* Copyright (c) 2014, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @emails oncall+jsinfra
*/
'use strict';
jest.autoMockOff();
describe('moduleMocker', function() {
var moduleMocker;
beforeEach(function() {
moduleMocker = require('../moduleMocker');
});
describe('getMetadata', function() {
it('returns the function `name` property', function() {
function x() {}
var metadata = moduleMocker.getMetadata(x);
expect(x.name).toBe('x');
expect(metadata.name).toBe('x');
});
});
describe('generateFromMetadata', function() {
it('forwards the function name property', function() {
function foo() {}
var fooMock = moduleMocker.generateFromMetadata(
moduleMocker.getMetadata(foo)
);
expect(fooMock.name).toBe('foo');
});
it('wont interfere with previous mocks on a shared prototype', function() {
var ClassFoo = function() {};
ClassFoo.prototype.x = function() {};
var ClassFooMock = moduleMocker.generateFromMetadata(
moduleMocker.getMetadata(ClassFoo)
);
var foo = new ClassFooMock();
var bar = new ClassFooMock();
foo.x.mockImplementation(function() {
return 'Foo';
});
bar.x.mockImplementation(function() {
return 'Bar';
});
expect(foo.x()).toBe('Foo');
expect(bar.x()).toBe('Bar');
});
it('does not mock non-enumerable getters', function() {
var foo = Object.defineProperties({}, {
nonEnumMethod: {
value: function() {},
},
nonEnumGetter: {
get: function() { throw new Error(); },
},
});
var fooMock = moduleMocker.generateFromMetadata(
moduleMocker.getMetadata(foo)
);
expect(typeof foo.nonEnumMethod).toBe('function');
expect(fooMock.nonEnumMethod.mock).not.toBeUndefined();
expect(fooMock.nonEnumGetter).toBeUndefined();
});
it('does not mock __esModule special property (babel6)', function () {
var ClassFoo = function() {};
var fooModule = {
__esModule: true,
default: ClassFoo
};
var fooModuleMock = moduleMocker.generateFromMetadata(
moduleMocker.getMetadata(fooModule)
);
expect(fooModuleMock.__esModule).toEqual(fooModule.__esModule);
});
it('mocks ES2015 non-enumerable methods', function() {
class ClassFoo {
foo() {}
toString() {
return 'Foo';
}
}
var ClassFooMock = moduleMocker.generateFromMetadata(
moduleMocker.getMetadata(ClassFoo)
);
var foo = new ClassFooMock();
var instanceFoo = new ClassFoo();
var instanceFooMock = moduleMocker.generateFromMetadata(
moduleMocker.getMetadata(instanceFoo)
);
expect(typeof foo.foo).toBe('function');
expect(typeof instanceFooMock.foo).toBe('function');
expect(instanceFooMock.foo.mock).not.toBeUndefined();
expect(instanceFooMock.toString.mock).not.toBeUndefined();
});
});
});