问题描述
给出被测模块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
范围内比较干净,因为在其他地方不需要它。这是怎么回事?开玩笑的东西使用什么范围?
解决方法
我看起来setMock
只考虑了给定模块的第一个模拟,而没有在第二个调用中覆盖它。或更确切地说,我相信是require
进行了缓存-笑话在运行整个测试套件之前仅清空模块缓存一次(预计the imports will be at the top of the suite在声明要模拟的模块之后)。
您的模拟实现随后从第一个beforeEachScope
调用开始对beforeEach
进行了关闭。
为什么您可能会想,为什么describeScope
上也没有关闭?实际上,确实如此,可能会使您的代码感到困惑的是,beforeEach
确实运行了describeScope = false
,这总是将其重置为false
,然后再记录到任何地方。如果删除该语句,而仅在let describeScope = false
范围内初始化describe
,您将看到它在第一次调用true
之后也将更改为sut()
。
如果我们手动解析范围并从执行中删除所有垃圾包装,这就是发生的情况:
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,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
内创建一个新的闭包,当我们第二次调用varInRunClosure
时使用一个新的run
,但是缓存的函数仍在使用生成的闭包run
第一次运行时,现在在缓存的函数范围之外无法访问。