Vue 单元测试:beforeEach() 在所有测试之后执行,而不是在每个测试之前执行
Vue unit test: beforeEach() executing after all tests, not before each test
我正在尝试为 Vue 编写一些单元测试,而不是每次都设置一个新的包装器,我想使用 beforeEach() 来自动处理它。当我 运行 调试器时,它无法通过所有测试,然后 运行 每个测试的 beforeEach() 函数。
这是我的 .spec.js 文件。
import {
mount,
} from '@vue/test-utils';
import QcAddressView from './address-view.vue';
const id = 'test_address-view';
describe('qc-address-view', () => {
let wrapper = null
beforeEach(() => {
console.log("beforeEach executed!");
wrapper = mount(QcAddressView, {
id,
address: {
addrLine1: '111 Testerson Way',
addrLine2: '',
cityName: 'Olympia',
stateCode: 'WA',
zipCode: '98512',
countyName: 'Thurston',
countryName: 'United States of America',
},
})
})
test('sets up a valid address', () => {
console.log('sets up a valid address');
expect(wrapper.attributes('id')).toBe(id);
})
});
控制台显示测试失败:
FAIL: qc-address-view
× sets up a valid address (72ms)
TypeError: Cannot read property 'addrLine1' of undefined
TypeError: Cannot read property 'attributes' of null
它无法读取属性,因为 beforeEach() 尚未设置对象。
然后它 运行s beforeEach() 在测试之后而不是之前:
console.log: beforeEach executed!
当我用三个测试尝试它时,每个测试都会失败,然后 console.log 会打印“beforeEach executed!”三遍。
我如何在每次测试之前让 beforeEach() 达到 运行,而不是每次测试之后?
答案是将 id
和 address
放入一个 attrs
对象中,因此:
wrapper = mount(QcAddressView, {
attrs: {
id,
address: {
addrLine1: '111 Testerson Way',
addrLine2: '',
cityName: 'Olympia',
stateCode: 'WA',
zipCode: '98512',
countyName: 'Thurston',
countryName: 'United States of America',
},
}
})
beforeEach
是 实际上 运行 在你的测试之前。否则,wrapper
在您的测试中将是 null
,并且您会得到不同的错误。
您在 测试后 看到控制台日志,因为 Jest buffers the log output, and dumps it at the end of the tests. You can avoid the bufferring by setting useStderr
。您可以从 jest.config.js
:
执行此操作
module.exports = {
useStderr: true,
}
我正在尝试为 Vue 编写一些单元测试,而不是每次都设置一个新的包装器,我想使用 beforeEach() 来自动处理它。当我 运行 调试器时,它无法通过所有测试,然后 运行 每个测试的 beforeEach() 函数。
这是我的 .spec.js 文件。
import {
mount,
} from '@vue/test-utils';
import QcAddressView from './address-view.vue';
const id = 'test_address-view';
describe('qc-address-view', () => {
let wrapper = null
beforeEach(() => {
console.log("beforeEach executed!");
wrapper = mount(QcAddressView, {
id,
address: {
addrLine1: '111 Testerson Way',
addrLine2: '',
cityName: 'Olympia',
stateCode: 'WA',
zipCode: '98512',
countyName: 'Thurston',
countryName: 'United States of America',
},
})
})
test('sets up a valid address', () => {
console.log('sets up a valid address');
expect(wrapper.attributes('id')).toBe(id);
})
});
控制台显示测试失败:
FAIL: qc-address-view
× sets up a valid address (72ms)
TypeError: Cannot read property 'addrLine1' of undefined
TypeError: Cannot read property 'attributes' of null
它无法读取属性,因为 beforeEach() 尚未设置对象。
然后它 运行s beforeEach() 在测试之后而不是之前:
console.log: beforeEach executed!
当我用三个测试尝试它时,每个测试都会失败,然后 console.log 会打印“beforeEach executed!”三遍。
我如何在每次测试之前让 beforeEach() 达到 运行,而不是每次测试之后?
答案是将 id
和 address
放入一个 attrs
对象中,因此:
wrapper = mount(QcAddressView, {
attrs: {
id,
address: {
addrLine1: '111 Testerson Way',
addrLine2: '',
cityName: 'Olympia',
stateCode: 'WA',
zipCode: '98512',
countyName: 'Thurston',
countryName: 'United States of America',
},
}
})
beforeEach
是 实际上 运行 在你的测试之前。否则,wrapper
在您的测试中将是 null
,并且您会得到不同的错误。
您在 测试后 看到控制台日志,因为 Jest buffers the log output, and dumps it at the end of the tests. You can avoid the bufferring by setting useStderr
。您可以从 jest.config.js
:
module.exports = {
useStderr: true,
}