Javascript 为什么jest.fn().mock实现在重置变量';在beforeach作用域中声明了什么?
给定一个正在测试的模块Javascript 为什么jest.fn().mock实现在重置变量';在beforeach作用域中声明了什么?,javascript,scope,jestjs,Javascript,Scope,Jestjs,给定一个正在测试的模块sut.js const { dependencyFunc } = require('./dep') module.exports = () => { return dependencyFunc() } module.exports = { dependencyFunc: () => 'we have hit the dependency' } 具有依赖项dep.js const { dependencyFunc } = require('./de
sut.js
const { dependencyFunc } = require('./dep')
module.exports = () => {
return dependencyFunc()
}
module.exports = {
dependencyFunc: () => 'we have hit the dependency'
}
具有依赖项dep.js
const { dependencyFunc } = require('./dep')
module.exports = () => {
return dependencyFunc()
}
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.
me$纱线测试.js
纱线运行v1.22.5
$jest test.js
通过。/test.js
在每个人面前嘲笑
✓ 第一次测试(17毫秒)
✓ 第二次测试(2毫秒)
console.log
在每个{BeforeAchScope:false,DescripteScope:false}之前运行
反对。(test.js:9:13)
console.log
{beforeachScope:false,descripeScope:false}
反对。(test.js:22:13)
console.log
在每个{BeforeAchScope:false,DescripteScope:false}之前运行
反对。(test.js:9:13)
console.log
{beforeachScope:true,descripeScope:false}
反对。(test.js:26:13)
测试套件:1个通过,共1个
测试:2次通过,共2次
快照:共0个
时间:1.248秒,估计2秒
运行与/test.js/i匹配的所有测试套件。
✨ 3.56秒完成。
我希望两个测试的输出都是
{beforeachscope:false,descripescope:false}
。也就是说,我希望beforeachscope
和descripebscope
变量都重置为false
,无论它们是在beforeach
范围还是descripe
范围内声明的。在我的真实测试中,我认为在<<<代码> > <范围内,它是更干净的,因为它在别处是不需要的。发生什么事?Jest stuff使用的是什么范围?我看起来像是setMock
只考虑给定模块的第一个mock,而不会在第二次调用时覆盖它。或者更确切地说,我认为是require
进行缓存-jest在运行整个测试套件之前只清空模块缓存一次(在声明要模拟的模块之后,预计会清空一次)
然后,您的模拟实现在每次调用之前的第一次beforeachscope
上有一个闭包
您可能会想,为什么它在descripescope
上没有一个闭包呢?事实上,确实如此,您的代码中可能会混淆的是,beforeach
确实运行了descripescope=false
,它总是在登录到任何地方之前将其重置为false
。如果删除该语句,而只是在descripe
范围内初始化let descripescope=false
,那么在第一次sut()
调用之后,它也将变为true
如果我们手动解析作用域并从执行中删除所有jest包装,将会发生以下情况:
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
run生成的闭包,现在在缓存的函数范围之外无法访问。Hi Bergi感谢您的回复。在我的真实测试中,我模拟了一个数据库存储库,我想在测试之间重置数据-descripescope=false
和beforeachscope=false
我正在重置我所面临问题的图示中的数据。在测试期间,我不需要访问模拟数据存储库的内部。如果变量在每个闭包之前的中声明,为什么数据(在此使用beforeachscope
和descripescope
表示)不重置?它会重置。不重置为新模拟模块的是sut=require('./sut')
。您得到的模块与第一次require
调用相同,带有旧数据。很抱歉,我没有跟上。数据是否存储在闭包的范围内(beforeach或descripe),而不管是否需要缓存模块,因为变量就是在这里初始化的?如果不是这样,为什么beforeach的闭包与descripe的闭包的工作方式不同?是的,变量存储在您声明它们的作用域中,没有什么神奇之处。它们的工作方式不同,因为您在您的beforeach
中重置了descripebscope=false
,而不是beforeach
。非常感谢代码示例和持续的帮助-现在一切都清楚了。闭包的工作方式与我预期的略有不同。我已经要求对你的答案进行编辑,将问题归结为结束语。