我试图使用graphql-yoga编写开玩笑的测试来涵盖订阅。
如果订阅有效(使用auth),我能够成功测试幸福路径。不幸的是,我无法测试预订Websocket连接被拒绝的情况。
在服务器设置中,我拒绝任何未通过身份验证标准的websocket连接:
const app = await server.start({
cors,
port: process.env.NODE_ENV === "test" ? 0 : 4000,
subscriptions: {
path: "/",
onConnect: async (connectionParams: any) => {
const token = connectionParams.token;
if (!token) {
throw new AssertionError({ message: "NO TOKEN PRESENT" });
}
const decoded = parseToken(token, process.env.JWT_SECRET as string);
const user = await validateTokenVersion(decoded, redis);
if (user === {}) {
throw new AssertionError({ message: "NO VALID USER" });
}
return { user };
}
}
});
第一个测试(幸福的道路)通过了,我希望:
// works as expected.
test("should start a subscription on network interface and unsubscribe", async done => {
const client = new TestClientApollo(process.env.TEST_HOST as string);
await client.register(email, password);
await User.update({ email }, { confirmed: true });
await client.login(email, password);
// set up subscription listener
const sub = client.client.subscribe(defaultOptions).subscribe({
next(result) {
expect(result).toEqual({
data: {
counter: {
count: 0
}
}
});
sub.unsubscribe();
done();
}
});
});
然后,我尝试3种不同的方式来捕捉我希望在未认证的场景中看到的期望。这些测试都没有通过,正如我希望的那样:
// Does not work! I am expecting an error.
test("Unauthed subscriptions are rejected", done => {
const client = new TestClientApollo(process.env.TEST_HOST as string);
const sub = client.client.subscribe(defaultOptions).subscribe({
next(result) {
expect(result).toEqual({
data: {
counter: {
count: 0
}
}
});
sub.unsubscribe();
done();
}
});
// Received value must be a function, but instead "object" was found
expect(sub).toThrow();
});
// does not work
// Error: Uncaught { message: 'NO TOKEN PRESENT' }
test("Unauthed subscriptions are rejected second attempt", done => {
const client = new TestClientApollo(process.env.TEST_HOST as string);
try {
const sub = client.client.subscribe(defaultOptions).subscribe({
next(result) {
expect(result).toEqual({
data: {
counter: {
count: 0
}
}
});
sub.unsubscribe();
// done();
}
});
} catch (error) {
console.log(error);
expect(error).toEqual({
message: "NO TOKEN PRESENT"
});
done();
}
});
// does not work
// Error: Uncaught { message: 'NO TOKEN PRESENT' }
test("Unauthed subscriptions are rejected second attempt", done => {
const client = new TestClientApollo(process.env.TEST_HOST as string);
try {
const sub = client.client.subscribe(defaultOptions).subscribe({
next(result) {
expect(result).toEqual({
data: {
counter: {
count: 0
}
}
});
sub.unsubscribe();
// done();
}
});
} catch (error) {
console.log(error);
expect(error).toEqual({
message: "NO TOKEN PRESENT"
});
done();
}
});
// does not work
// Expected the function to throw an error.
// But it didn't throw anything.
test("Unauthed subscriptions are rejected third attempt", done => {
const client = new TestClientApollo(process.env.TEST_HOST as string);
expect(async () => {
const sub = await client.client.subscribe(defaultOptions).subscribe({
next(result) {
expect(result).toEqual({
data: {
counter: {
count: 0
}
}
});
sub.unsubscribe();
done();
}
});
}).toThrowError();
});
// does not work
// Expected the function to throw an error.
// But it didn't throw anything.
test("Unauthed subscriptions are rejected fourth attempt", done => {
const client = new TestClientApollo(process.env.TEST_HOST as string);
const attempt = async () => {
const sub = await client.client.subscribe(defaultOptions).subscribe({
next(result) {
expect(result).toEqual({
data: {
counter: {
count: 0
}
}
});
sub.unsubscribe();
done();
}
});
};
expect(attempt).toThrowError();
});
有什么想法可以预期我在针对未经身份验证的场景的测试中会遇到的断言错误吗?
完整的仓库在这里:https://github.com/jakelowen/typescript-graphql-boilerplate-server
答案 0 :(得分:1)
Booyah。我阅读了一般的observables,特别是observable.subscribe(),发现第二个可选参数是onError回调函数。将测试重构为:
test("Unauthed subscriptions are rejected", done => {
const client = new TestClientApollo(process.env.TEST_HOST as string);
// jest.setTimeout(1000); // increase timeout
client.client.subscribe(defaultOptions).subscribe(
res => {
console.log(res);
},
err => {
expect(err).toEqual({ message: "NO TOKEN PRESENT" });
done();
}
);
});
所有工作都按预期进行。哇!
侧面社论:经过数十个小时的Google搜索,堆栈溢出和github搜索,我从未发现有关如何正确测试graphql订阅的简单明了的教程。 em>