我有以下在我想要進行單元測驗的賽普拉斯測驗中使用的函式(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 | }
我相信使這種情況復雜化的是賽普拉斯沒有明確匯入 filterTests.js
uj5u.com熱心網友回復:
我認為您可能只想在測驗頂部設定 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 中運行測驗,那么 Cypress 全域應該可以通過設定它來模擬
global.Cypress = {
env: () => 'jenkins1' // or more complicated fn as test requires
}
請注意,我希望這僅適用于簡單的情況。Cypress 封裝了 jQuery、Chai 和 Mocha,因此在 Cypress 測驗運行時它們的行為略有不同。如果你測驗的函式使用了這些特性中的任何一個,即使是隱式的(比如命令重試),那么 Jest 將不會重現正確的環境。
我的建議是用 Cypress 測驗 Cypress。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/436276.html
標籤:javascript 单元测试 测试 开玩笑的 柏
