Skip to content

Commit 163c173

Browse files
committed
Don't try to fake generator functions
testdouble.js does not yet support faking generators, full stop. Just return the actual function and fake any properties tacked onto them instead.
1 parent ecfe230 commit 163c173

File tree

5 files changed

+78
-11
lines changed

5 files changed

+78
-11
lines changed

src/imitate/create-imitation.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
import _ from '../wrap/lodash'
22

33
import tdFunction from '../function'
4+
import isGenerator from './is-generator'
45

56
export default (original, names) => {
67
if (_.isArray(original) || _.isArguments(original)) {
78
return []
89
} else if (_.isFunction(original)) {
9-
return tdFunction(names.join('') || '(anonymous function)')
10+
if (isGenerator(original)) {
11+
return original
12+
} else {
13+
return tdFunction(names.join('') || '(anonymous function)')
14+
}
1015
} else {
1116
return _.clone(original)
1217
}

src/imitate/is-generator.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const generatorsAreSupported = (function () {
2+
try {
3+
eval('(function* () {})') // eslint-disable-line
4+
return true
5+
} catch (e) {
6+
return false
7+
}
8+
})()
9+
10+
const GeneratorFunction = (function () {
11+
if (!generatorsAreSupported) return
12+
const func = eval('(function* () {})') // eslint-disable-line
13+
return Object.getPrototypeOf(func).constructor
14+
})()
15+
16+
export default (func) =>
17+
generatorsAreSupported && func.constructor === GeneratorFunction

test/helper.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,12 @@ module.exports = {
1818
},
1919
afterAll: function () {}
2020
}
21+
22+
global.ES_GENERATOR_SUPPORT = (function () {
23+
try {
24+
eval('(function* () {})')
25+
return true
26+
} catch (e) {
27+
return false
28+
}
29+
})()
Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
let tdFunction, subject
1+
let tdFunction, isGenerator, subject
22
module.exports = {
33
beforeEach: () => {
44
tdFunction = td.replace('../../../src/function').default
5+
isGenerator = td.replace('../../../src/imitate/is-generator').default
56
subject = require('../../../src/imitate/create-imitation').default
67
},
78
'an array type': () => {
@@ -12,36 +13,47 @@ module.exports = {
1213
assert.deepEqual(subject(args, []), [])
1314
},
1415
'a function without names': () => {
16+
const someFunc = () => {}
1517
td.when(tdFunction('(anonymous function)')).thenReturn('fake thing')
18+
td.when(isGenerator(someFunc)).thenReturn(false)
1619

17-
const result = subject(() => {}, [])
20+
const result = subject(someFunc, [])
1821

1922
assert.deepEqual(result, 'fake thing')
2023
},
2124
'a function with names': () => {
25+
const someFunc = () => {}
2226
td.when(tdFunction('AOK')).thenReturn('fake thing')
27+
td.when(isGenerator(someFunc)).thenReturn(false)
2328

24-
const result = subject(() => {}, ['A', 'OK'])
29+
const result = subject(someFunc, ['A', 'OK'])
2530

2631
assert.deepEqual(result, 'fake thing')
2732
},
2833
'other instances': () => {
2934
const original = {a: 'b'}
3035

31-
const result = subject(original)
36+
const result = subject(original, [])
3237

3338
assert.deepEqual(result, original)
3439
assert.notStrictEqual(result, original)
3540
},
3641
'primitives': () => {
37-
assert.strictEqual(subject(true), true)
38-
assert.strictEqual(subject(5), 5)
39-
assert.strictEqual(subject('hi'), 'hi')
40-
assert.strictEqual(subject(null), null)
41-
assert.strictEqual(subject(undefined), undefined)
42+
assert.strictEqual(subject(true, []), true)
43+
assert.strictEqual(subject(5, []), 5)
44+
assert.strictEqual(subject('hi', []), 'hi')
45+
assert.strictEqual(subject(null, []), null)
46+
assert.strictEqual(subject(undefined, []), undefined)
4247
},
4348
'symbols': () => {
4449
if (!global.Symbol) return
45-
assert.strictEqual(subject(Symbol.species), Symbol.species)
50+
assert.strictEqual(subject(Symbol.species, []), Symbol.species)
51+
},
52+
'generators do not blow up and just return themselves i guess': () => {
53+
const generator = () => {}
54+
td.when(tdFunction(), {ignoreExtraArgs: true}).thenReturn('fake thing')
55+
td.when(isGenerator(generator)).thenReturn(true)
56+
57+
assert.strictEqual(subject(generator, []), generator)
4658
}
4759
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import subject from '../../../src/imitate/is-generator'
2+
3+
module.exports = {
4+
'returns false if passed a plain function': () => {
5+
const func = function () {}
6+
7+
const result = subject(func)
8+
9+
assert.strictEqual(result, false)
10+
},
11+
'returns true if passed a generator (when generators supported)': () => {
12+
if (!ES_GENERATOR_SUPPORT) return
13+
const func = eval('(function* () {})') //esline-disable-line
14+
15+
const result = subject(func)
16+
17+
assert.strictEqual(result, true)
18+
},
19+
'returns false when generators are not supported': () => {
20+
if (ES_GENERATOR_SUPPORT) return
21+
// Not much of a test, ¯\_(ツ)_/¯
22+
assert.strictEqual(subject(), false)
23+
}
24+
}

0 commit comments

Comments
 (0)