如何在玩笑中模拟文件上传或文件对象? [英] How to mock file upload or file object in jest?
问题描述
我想用玩笑模拟文件上传来测试我的阿波罗服务器,我使用 graphql 来定义和传递数据.但是我发现在玩笑中模拟文件对象非常困难.我用谷歌搜索并花了很多时间,但仍然找不到答案.
正如有人建议的那样,我使用这种方法.https://gist.github.com/josephhanson/372b44f93472f9c5a2d0725d4
但是当我看到后端的返回时,它告诉我 createreadstream 不是一个函数.
这是我定义的graphql.
类型变异{changeFlowStatus(flowLog: FlowLogInput!, files: [Upload!], newStatus: Status): FlowLog}解析器{上传:GraphQLUpload,突变:{更改流状态:}
这是开玩笑的部分.
//改变FlowStatus变量大小 = 1024 * 1024 * 2;var mock = new MockFile();const file = mock.create("pic.jpg", size, "image/jpeg");const changeFlowStatus = await toPromise(graphql({查询:CHANGE_FLOW_STATUS,变量:{flowLog: { reflowId: reflowId, 时间戳: '1562716800', 评论: 'flowLog_comment', title: 'flowLog_title' },文件:[文件],新状态:'SP_RECEIVED',},语境: {useMultipart: 真,},}),);
file
是我用上面提到的类创建的对象.
我的期望很简单,用玩笑模拟文件上传过程或文件对象.谢谢.
我想你使用 apollographql
、graphql-upload
和 jest.js
>,这里是文件上传的集成测试:
it('应该正确上传文件', async() => {const body = new FormData();body.append('操作',JSON.stringify({查询:`突变($文件:上传!){单上传(文件:$文件){代码信息}}`,变量:{文件:空,},}),);const 文件名 = '15625760447371547012340909WX20190108-124331.png';body.append('map', JSON.stringify({ 1: ['variables.file'] }));body.append('1', fs.createReadStream(path.resolve(__dirname, `./files/${filename}`)));const json = await fetch('http://localhost:4000', { method: 'POST', body }).then((response) => response.json());//logger.debug('upload testing#1', { arguments: { json } });期望(json).toEqual(期望.objectContaining({数据: {单个上传:{代码:expect.any(Number),消息:expect.any(String),},},}),);});
参见 Github 示例存储库
I want to mock file-upload with jest to test my apollo-server, I use graphql to define and pass data. But I find it is very difficult to mock file object in jest. I search with google and spend lots of time, but still can't find an answer.
As some guy suggest, I use this method. https://gist.github.com/josephhanson/372b44f93472f9c5a2d025d40e7bb4cc,
but when I see the return of the back-end, it tells me createreadstream is not a function.
This is the graphql I defined.
type Mutation{
changeFlowStatus(flowLog: FlowLogInput!, files: [Upload!], newStatus: Status): FlowLog
}
resolvers{
Upload: GraphQLUpload,
Mutation: {
changeFlowStatus:
}
This is the jest part.
//changeFlowStatus
var size = 1024 * 1024 * 2;
var mock = new MockFile();
const file = mock.create("pic.jpg", size, "image/jpeg");
const changeFlowStatus = await toPromise(
graphql({
query: CHANGE_FLOW_STATUS,
variables: {
flowLog: { reflowId: reflowId, timestamp: '1562716800', comment: 'flowLog_comment', title: 'flowLog_title' },
files: [file],
newStatus: 'SP_RECEIVED',
},
context: {
useMultipart: true,
},
}),
);
file
is the object I create with the class mentioned above.
My expectation is simple, mock a file-upload process or file object with jest. Thanks.
I suppose you use apollographql
, graphql-upload
and jest.js
, here is a integretion test for file upload:
it('should upload file correctly', async () => {
const body = new FormData();
body.append(
'operations',
JSON.stringify({
query: `
mutation ($file: Upload!) {
singleUpload(file: $file) {
code
message
}
}
`,
variables: {
file: null,
},
}),
);
const filename = '15625760447371547012340909WX20190108-124331.png';
body.append('map', JSON.stringify({ 1: ['variables.file'] }));
body.append('1', fs.createReadStream(path.resolve(__dirname, `./files/${filename}`)));
const json = await fetch('http://localhost:4000', { method: 'POST', body }).then((response) => response.json());
// logger.debug('upload testing#1', { arguments: { json } });
expect(json).toEqual(
expect.objectContaining({
data: {
singleUpload: {
code: expect.any(Number),
message: expect.any(String),
},
},
}),
);
});
这篇关于如何在玩笑中模拟文件上传或文件对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!