为什么 jest.fn().mockImplementation 在 beforeEach 作用域中声明时看不到重置变量?
Why does jest.fn().mockImplementation not see the reset variable when it's declared in the beforeEach scope?
给定一个被测模块sut.js
const { dependencyFunc } = require('./dep')
module.exports = () => {
return dependencyFunc()
}
具有依赖性dep.js
module.exports = {
dependencyFunc: () => 'we have hit the dependency'
}
和一些测试:
describe('mocking in beforeEach', () => {
let sut
let describeScope
beforeEach(() => {
let beforeEachScope = false
describeScope = false
console.log('running before each', { beforeEachScope, describeScope })
jest.setMock('./dep', {
dependencyFunc: jest.fn().mockImplementation(() => {
const returnable = { beforeEachScope, describeScope }
beforeEachScope = true
describeScope = true
return returnable
})
})
sut = require('./sut')
})
it('first test', () => {
console.log(sut())
})
it('second test', () => {
console.log(sut())
})
})
我得到以下输出:
me$ yarn test test.js
yarn run v1.22.5
$ jest test.js
PASS ./test.js
mocking in beforeEach
✓ first test (17 ms)
✓ second test (2 ms)
console.log
running before each { beforeEachScope: false, describeScope: false }
at Object.<anonymous> (test.js:9:13)
console.log
{ beforeEachScope: false, describeScope: false }
at Object.<anonymous> (test.js:22:13)
console.log
running before each { beforeEachScope: false, describeScope: false }
at Object.<anonymous> (test.js:9:13)
console.log
{ beforeEachScope: true, describeScope: false }
at Object.<anonymous> (test.js:26:13)
Test Suites: 1 passed, 1 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 1.248 s, estimated 2 s
Ran all test suites matching /test.js/i.
✨ Done in 3.56s.
我希望两个测试的输出都是 { beforeEachScope: false, describeScope: false }
。也就是说,我希望 beforeEachScope
和 describeScope
变量都被重置为 false
,而不管它们是在 beforeEach
范围内还是在 describe
范围内声明的。在我的实际测试中,我认为将它放在 beforeEach
范围内会更干净,因为其他地方不需要它。这是怎么回事? Jest 使用的范围是什么?
我看起来 setMock
只考虑给定模块的第一个模拟,并且不会在第二次调用时覆盖它。或者更确切地说,我相信是 require
执行缓存 - 在 运行 整个测试套件之前开玩笑只清空模块缓存一次(预计 ,在声明 which要模拟的模块)。
然后您的模拟实现在第一个 beforeEach
调用的 beforeEachScope
上有一个闭包。
您可能想知道,为什么它似乎也没有关闭 describeScope
?事实上,确实如此,您的代码中可能令人困惑的是 beforeEach
执行 运行 describeScope = false
,它总是在将其记录到任何地方之前将其重置为 false
。如果您删除该语句,而仅在 describe
范围内初始化 let describeScope = false
,您将看到它在第一次 sut()
调用后也会更改为 true
。
如果我们手动解析范围并从执行中删除所有的笑话包装器,将会发生以下情况:
let sut
let describeScope
// first test, beforeEach:
let beforeEachScope1 = false
describeScope = false
console.log('running before each 1', { beforeEachScope1, describeScope }) // false, false as expected
jest.setMock('./dep', {
dependencyFunc(n) {
console.log('sut call '+n, { beforeEachScope1, describeScope });
beforeEachScope1 = true
describeScope = true
})
})
sut = require('./sut') // will call the function we just created
// first test
sut(1) // still logs false, false
// second test, beforeEach:
let beforeEachScope2 = false // a new variable
describeScope = false // reset from true to false, you shouldn't do this
console.log('running before each 2', { beforeEachScope2, describeScope }) // logs false, false
jest.setMock('./dep', {
dependencyFunc(n) {
// this function is never called
})
})
sut = require('./sut') // a no-op, sut doesn't change (still calling the first mock)
// second test:
sut(2) // logs true (beforeEachScope1) and false
使用以下内容:
const dependencyFunc = jest.fn();
jest.setMock('./dep', {
dependencyFunc,
})
const sut = require('./sut')
describe('mocking in beforeEach', () => {
let describeScope = false
beforeEach(() => {
let beforeEachScope = false
console.log('running before each', { beforeEachScope, describeScope })
dependencyFunc.mockImplementation(() => {
const returnable = { beforeEachScope, describeScope }
beforeEachScope = true
describeScope = true
return returnable
})
})
it('first test', () => {
console.log(sut())
})
it('second test', () => {
console.log(sut())
})
})
下面演示了组合的缓存和范围/闭包行为。
let cachedFunction
let varInGlobalClosure
const run = () => {
varInGlobalClosure = false
let varInRunClosure = false
// next line is what jest.mock is doing - caching the function
cachedFunction = cachedFunction || (() => {
const returnable = { varInRunClosure, varInGlobalClosure }
varInRunClosure = true
varInGlobalClosure = true
return returnable
})
return cachedFunction
}
console.log('first run', run()()) // outputs { varInRunClosure: false, varInGlobalClosure: false }
console.log('second run', run()()) // outputs { varInRunClosure: true, varInGlobalClosure: false }
这是因为我们在run
里面做了一个新的闭包,在我们第二次调用run
的时候用了一个新的varInRunClosure
,但是缓存的函数还在使用第一次 run
运行 生成的闭包,现在在缓存函数范围之外无法访问。
给定一个被测模块sut.js
const { dependencyFunc } = require('./dep')
module.exports = () => {
return dependencyFunc()
}
具有依赖性dep.js
module.exports = {
dependencyFunc: () => 'we have hit the dependency'
}
和一些测试:
describe('mocking in beforeEach', () => {
let sut
let describeScope
beforeEach(() => {
let beforeEachScope = false
describeScope = false
console.log('running before each', { beforeEachScope, describeScope })
jest.setMock('./dep', {
dependencyFunc: jest.fn().mockImplementation(() => {
const returnable = { beforeEachScope, describeScope }
beforeEachScope = true
describeScope = true
return returnable
})
})
sut = require('./sut')
})
it('first test', () => {
console.log(sut())
})
it('second test', () => {
console.log(sut())
})
})
我得到以下输出:
me$ yarn test test.js
yarn run v1.22.5
$ jest test.js
PASS ./test.js
mocking in beforeEach
✓ first test (17 ms)
✓ second test (2 ms)
console.log
running before each { beforeEachScope: false, describeScope: false }
at Object.<anonymous> (test.js:9:13)
console.log
{ beforeEachScope: false, describeScope: false }
at Object.<anonymous> (test.js:22:13)
console.log
running before each { beforeEachScope: false, describeScope: false }
at Object.<anonymous> (test.js:9:13)
console.log
{ beforeEachScope: true, describeScope: false }
at Object.<anonymous> (test.js:26:13)
Test Suites: 1 passed, 1 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 1.248 s, estimated 2 s
Ran all test suites matching /test.js/i.
✨ Done in 3.56s.
我希望两个测试的输出都是 { beforeEachScope: false, describeScope: false }
。也就是说,我希望 beforeEachScope
和 describeScope
变量都被重置为 false
,而不管它们是在 beforeEach
范围内还是在 describe
范围内声明的。在我的实际测试中,我认为将它放在 beforeEach
范围内会更干净,因为其他地方不需要它。这是怎么回事? Jest 使用的范围是什么?
我看起来 setMock
只考虑给定模块的第一个模拟,并且不会在第二次调用时覆盖它。或者更确切地说,我相信是 require
执行缓存 - 在 运行 整个测试套件之前开玩笑只清空模块缓存一次(预计
然后您的模拟实现在第一个 beforeEach
调用的 beforeEachScope
上有一个闭包。
您可能想知道,为什么它似乎也没有关闭 describeScope
?事实上,确实如此,您的代码中可能令人困惑的是 beforeEach
执行 运行 describeScope = false
,它总是在将其记录到任何地方之前将其重置为 false
。如果您删除该语句,而仅在 describe
范围内初始化 let describeScope = false
,您将看到它在第一次 sut()
调用后也会更改为 true
。
如果我们手动解析范围并从执行中删除所有的笑话包装器,将会发生以下情况:
let sut
let describeScope
// first test, beforeEach:
let beforeEachScope1 = false
describeScope = false
console.log('running before each 1', { beforeEachScope1, describeScope }) // false, false as expected
jest.setMock('./dep', {
dependencyFunc(n) {
console.log('sut call '+n, { beforeEachScope1, describeScope });
beforeEachScope1 = true
describeScope = true
})
})
sut = require('./sut') // will call the function we just created
// first test
sut(1) // still logs false, false
// second test, beforeEach:
let beforeEachScope2 = false // a new variable
describeScope = false // reset from true to false, you shouldn't do this
console.log('running before each 2', { beforeEachScope2, describeScope }) // logs false, false
jest.setMock('./dep', {
dependencyFunc(n) {
// this function is never called
})
})
sut = require('./sut') // a no-op, sut doesn't change (still calling the first mock)
// second test:
sut(2) // logs true (beforeEachScope1) and false
使用以下内容:
const dependencyFunc = jest.fn();
jest.setMock('./dep', {
dependencyFunc,
})
const sut = require('./sut')
describe('mocking in beforeEach', () => {
let describeScope = false
beforeEach(() => {
let beforeEachScope = false
console.log('running before each', { beforeEachScope, describeScope })
dependencyFunc.mockImplementation(() => {
const returnable = { beforeEachScope, describeScope }
beforeEachScope = true
describeScope = true
return returnable
})
})
it('first test', () => {
console.log(sut())
})
it('second test', () => {
console.log(sut())
})
})
下面演示了组合的缓存和范围/闭包行为。
let cachedFunction
let varInGlobalClosure
const run = () => {
varInGlobalClosure = false
let varInRunClosure = false
// next line is what jest.mock is doing - caching the function
cachedFunction = cachedFunction || (() => {
const returnable = { varInRunClosure, varInGlobalClosure }
varInRunClosure = true
varInGlobalClosure = true
return returnable
})
return cachedFunction
}
console.log('first run', run()()) // outputs { varInRunClosure: false, varInGlobalClosure: false }
console.log('second run', run()()) // outputs { varInRunClosure: true, varInGlobalClosure: false }
这是因为我们在run
里面做了一个新的闭包,在我们第二次调用run
的时候用了一个新的varInRunClosure
,但是缓存的函数还在使用第一次 run
运行 生成的闭包,现在在缓存函数范围之外无法访问。