设置笑话模拟的问题
我在 cypress 测试中使用了以下函数,我想对其进行单元测试 (filterTests.js):
const filterTests = (definedTags, runTest) => {
console.log(`Cypress tags: ${definedTags}`);
let isFound = true;
const includeTag = Cypress.env('INCLUDETAG');
const excludeTag = Cypress.env('EXCLUDETAG');
if (includeTag) {
isFound = definedTags.includes(includeTag);
}
if (excludeTag) {
isFound = ! definedTags.includes(excludeTag);
}
if (isFound) {
runTest();
}
};
export default filterTests;
需要为 Cypress.env 创建一个测试替身。我不确定这在技术上是否会被视为存根、模拟、假货、虚拟等……,但哲学讨论不是现在的焦点。看起来在 Cypress 世界中,一切都被集中在“mock”下。
我在 Jest 测试文件中开始沿着类似这样的路径:
import filterTests from '../../cypress/support/filterTests';
describe('Something', () => {
jest.mock('Cypress', () => ({
env: {
INCLUDETAG: 'jenkins1'
}
}));
it('Something else ', (done) => {
const tempFunc = () => {
console.log('here a');
done();
};
filterTests(tag, tempFunc);
});
});
但为此我收到错误消息:
Cannot find module 'Cypress' from 'spec/cypress/filterTestsSO2.test.js'
2 |
3 | describe('Something', () => {
> 4 | jest.mock('Cypress', () => ({
| ^
5 | env: {
6 | INCLUDETAG: 'jenkins1'
7 | }
我相信使这种情况复杂化的是 Cypress 没有在 filterTests.js 中显式导入
I have the following function that is used within cypress tests for which I want to do unit testing (filterTests.js):
const filterTests = (definedTags, runTest) => {
console.log(`Cypress tags: ${definedTags}`);
let isFound = true;
const includeTag = Cypress.env('INCLUDETAG');
const excludeTag = Cypress.env('EXCLUDETAG');
if (includeTag) {
isFound = definedTags.includes(includeTag);
}
if (excludeTag) {
isFound = ! definedTags.includes(excludeTag);
}
if (isFound) {
runTest();
}
};
export default filterTests;
A test double for Cypress.env needs to be created. I'm not sure if this would technically be considered a stub, mock, fake, dummy, etc..., but the philosophical discussion isn't the focus right now. It looks like in the Cypress world, everything is lumped together under 'mock'.
I started down the path of something like this in the Jest test file:
import filterTests from '../../cypress/support/filterTests';
describe('Something', () => {
jest.mock('Cypress', () => ({
env: {
INCLUDETAG: 'jenkins1'
}
}));
it('Something else ', (done) => {
const tempFunc = () => {
console.log('here a');
done();
};
filterTests(tag, tempFunc);
});
});
But for that I get the error message:
Cannot find module 'Cypress' from 'spec/cypress/filterTestsSO2.test.js'
2 |
3 | describe('Something', () => {
> 4 | jest.mock('Cypress', () => ({
| ^
5 | env: {
6 | INCLUDETAG: 'jenkins1'
7 | }
I believe what is complicating this situation is that Cypress is not explicitly imported in filterTests.js
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您可能只想在测试顶部设置 env 值
更多信息 - Cypress 有一个 cy.spy() ,它包装一个方法并记录它的调用,但否则结果相同。
还有
cy.stub()
,它记录调用,但也提供虚假结果。Jest 全局变量
如果您在 Jest 中运行测试,那么只需通过设置即可模拟 Cypress 全局变量。
注意我希望这仅适用于简单的情况。 Cypress 包装了 jQuery、Chai 和 Mocha,因此在 Cypress 测试运行时它们的行为略有不同。如果您测试的函数使用任何这些功能,即使是隐式的(例如命令重试),那么 Jest 将无法重现正确的环境。
我的建议是,用 Cypress 测试 Cypress。
I think you might just want to set the env value at the top of the test
Further info - Cypress has a
cy.spy()
which wraps a method and records it's calls but otherwise leaves it's result the same.Also
cy.stub()
which records calls but also provides a fake result.Jest globals
If you are running the test in Jest, then the Cypress global should be able to be mocked simply by setting it up
Note I expect this will only work with simple cases. Cypress wraps jQuery, Chai and Mocha so they behave slightly differently when a Cypress test runs. If the function you test uses any of those features, even implicitly (like command retry), then Jest will not reproduce the right environment.
My recommendation, test Cypress with Cypress.