I have a test where I'm trying to mock a component in two different situations. When I use jest.fn
. It almost looks like the first test is just taking the value from the second.
describe('tests', () => {
let sampleArray = new Array()
Array.prototype.test = function() {
return this.innerArray()
}
describe('empty', () => {
sampleArray.innerArray = jest.fn(() => [])
it('testArray is empty', () => {
expect(sampleArray.test().length).toEqual(0)
})
})
describe('not empty', () => {
sampleArray.innerArray = jest.fn(() => ['test'])
it('testArray is not empty', () => {
console.log(sampleArray.innerArray())
expect(sampleArray.test().length).toEqual(1)
})
})
})
When I console.log
I get the array I expect from innerArray, but it just looks like it doesn't use it.
FAIL test/sample.test.js
tests
empty
? testArray is empty (8ms)
not empty
? testArray is not empty (4ms)
● tests ? empty ? testArray is empty
expect(received).toEqual(expected)
Expected value to equal:
0
Received:
1
edit: If I place it inside the it
scope, it works. But why can't I do it in the describe
scope?
describe('tests', () => {
let sampleArray = new Array()
Array.prototype.test = function() {
return this.innerArray()
}
describe('empty', () => {
it('testArray is empty', () => {
sampleArray.innerArray = jest.fn(() => [])
console.log(sampleArray.innerArray())
expect(sampleArray.test().length).toEqual(0)
})
})
describe('not empty', () => {
it('testArray is not empty', () => {
sampleArray.innerArray = jest.fn(() => ['test'])
expect(sampleArray.test().length).toEqual(1)
})
})//works
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…