使用 Cypress.io 对我自己的节点模块进行单元测试时出错
Getting error while unit testing my own Node modules with Cypress.io
. I was able to start using Cypress to unit test my Node's module by using the task() 方法。但是,我遇到了一个错误,我一直在尝试调试或在线查找参考,但我无法做到。
cy:command ✘ assert expected **{ Object (userInvocationStack, specWindow, ...) }** to equal **[]**
这是什么{ Object (userInvocationStack, specWindow, ...) }
?我怎样才能得到 objects
的实际 array
呢?
编辑: 包括 assertions
& task
代码。
文件:utils.spec.js
describe('Unit Tests for utils.js methods', () => {
/**
* Array of objects mocking Tickets Object response
*/
const mockedTickets = {
data: {
issues: [
{
id: 1,
key: 'ticket-key-1',
fields: {
summary: 'This is ticket number 1',
},
},
{
id: 2,
key: 'ticket-key-2',
fields: {
summary: 'This is ticket number 2',
},
},
],
},
};
const mockedEmptyTicketsArray = [];
it('returns an array containing a found ticket summary', () => {
expect(
cy.task('getTicketBySummary', {
issues: mockedTickets,
summaryTitle: 'This is ticket number 1',
})
).eq(mockedTickets.data.issues[0]);
});
it('returns an empty array, when no ticket summary was found', () => {
expect(
cy.task('getTicketBySummary', {
issues: mockedTickets,
summaryTitle: 'This is ticket number 3',
})
).eq(mockedEmptyTicketsArray);
});
});
文件:`plugins/index.js
on('task', {
getTicketBySummary({ issues, summaryTitle }) {
issues.data.issues.filter(issueData => {
return issueData.fields.summary === summaryTitle ? issueData : null;
});
},
});
如评论中所述,您在之前的回答中给出了错误的语法。
Cypress 任务是 returns 可链接的命令。需要用.then()
取值,所以调用顺序要不同
it('returns an array containing a found ticket summary', () => {
cy.task('getTicketBySummary', { issues: mockedTickets, summaryTitle: 'This is ticket number 1' })
.then(result => expect(result).to.eq(mockedTickets.data.issues[0]) );
});
我能够通过像这样返回 task
的值来解决问题:
文件: plugins/index,js
on('task', {
getTicketBySummary({ issues, summaryTitle }) {
return issues.data.issues.filter(issueData => {
return issueData.fields.summary === summaryTitle ? issueData : null;
});
},
});
在我更新我的代码以使用 then()
方法并断言传递的 param
值之后,像这样:
文件: utils.spec.js
it('returns an empty array, when no ticket summary is found', () => {
cy.task('getTicketBySummary', {
issues: mockedTickets,
summaryTitle: 'This is ticket number 3',
}).then(result => expect(result).to.deep.equal(emptyArray));
});
cy:command ✘ assert expected **{ Object (userInvocationStack, specWindow, ...) }** to equal **[]**
这是什么{ Object (userInvocationStack, specWindow, ...) }
?我怎样才能得到 objects
的实际 array
呢?
编辑: 包括 assertions
& task
代码。
文件:utils.spec.js
describe('Unit Tests for utils.js methods', () => {
/**
* Array of objects mocking Tickets Object response
*/
const mockedTickets = {
data: {
issues: [
{
id: 1,
key: 'ticket-key-1',
fields: {
summary: 'This is ticket number 1',
},
},
{
id: 2,
key: 'ticket-key-2',
fields: {
summary: 'This is ticket number 2',
},
},
],
},
};
const mockedEmptyTicketsArray = [];
it('returns an array containing a found ticket summary', () => {
expect(
cy.task('getTicketBySummary', {
issues: mockedTickets,
summaryTitle: 'This is ticket number 1',
})
).eq(mockedTickets.data.issues[0]);
});
it('returns an empty array, when no ticket summary was found', () => {
expect(
cy.task('getTicketBySummary', {
issues: mockedTickets,
summaryTitle: 'This is ticket number 3',
})
).eq(mockedEmptyTicketsArray);
});
});
文件:`plugins/index.js
on('task', {
getTicketBySummary({ issues, summaryTitle }) {
issues.data.issues.filter(issueData => {
return issueData.fields.summary === summaryTitle ? issueData : null;
});
},
});
如评论中所述,您在之前的回答中给出了错误的语法。
Cypress 任务是 returns 可链接的命令。需要用.then()
取值,所以调用顺序要不同
it('returns an array containing a found ticket summary', () => {
cy.task('getTicketBySummary', { issues: mockedTickets, summaryTitle: 'This is ticket number 1' })
.then(result => expect(result).to.eq(mockedTickets.data.issues[0]) );
});
我能够通过像这样返回 task
的值来解决问题:
文件: plugins/index,js
on('task', {
getTicketBySummary({ issues, summaryTitle }) {
return issues.data.issues.filter(issueData => {
return issueData.fields.summary === summaryTitle ? issueData : null;
});
},
});
在我更新我的代码以使用 then()
方法并断言传递的 param
值之后,像这样:
文件: utils.spec.js
it('returns an empty array, when no ticket summary is found', () => {
cy.task('getTicketBySummary', {
issues: mockedTickets,
summaryTitle: 'This is ticket number 3',
}).then(result => expect(result).to.deep.equal(emptyArray));
});