设置笑话模拟的问题
Issue with setting up jest mock
我在 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
中明确导入
我想你可能只想在测试的顶部设置 env 值
describe('Something', () => {
Cypress.env(INCLUDETAG, 'jenkins1')
it('Something else ', (done) => {
const tempFunc = () => {
console.log('here a');
done();
};
filterTests(tag, tempFunc); // this function will read the env set above
})
})
更多信息 - 赛普拉斯有一个 cy.spy()
包装一个方法并记录它的调用,但在其他方面保持相同的结果。
还有 cy.stub()
记录通话但也提供虚假结果。
Jest 全局变量
如果您是 运行 Jest 中的测试,那么只需设置 Cypress 全局变量就可以对其进行模拟
global.Cypress = {
env: () => 'jenkins1' // or more complicated fn as test requires
}
请注意,我希望这只适用于简单的情况。 Cypress 封装了 jQuery、Chai 和 Mocha,因此在 Cypress 测试运行时它们的行为略有不同。如果您测试的函数使用了这些功能中的任何一个,即使是隐式的(如命令重试),那么 Jest 将无法重现正确的环境。
我的建议是,用 Cypress 测试 Cypress。
我在 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
中明确导入我想你可能只想在测试的顶部设置 env 值
describe('Something', () => {
Cypress.env(INCLUDETAG, 'jenkins1')
it('Something else ', (done) => {
const tempFunc = () => {
console.log('here a');
done();
};
filterTests(tag, tempFunc); // this function will read the env set above
})
})
更多信息 - 赛普拉斯有一个 cy.spy()
包装一个方法并记录它的调用,但在其他方面保持相同的结果。
还有 cy.stub()
记录通话但也提供虚假结果。
Jest 全局变量
如果您是 运行 Jest 中的测试,那么只需设置 Cypress 全局变量就可以对其进行模拟
global.Cypress = {
env: () => 'jenkins1' // or more complicated fn as test requires
}
请注意,我希望这只适用于简单的情况。 Cypress 封装了 jQuery、Chai 和 Mocha,因此在 Cypress 测试运行时它们的行为略有不同。如果您测试的函数使用了这些功能中的任何一个,即使是隐式的(如命令重试),那么 Jest 将无法重现正确的环境。
我的建议是,用 Cypress 测试 Cypress。