From 89046b29c33fe0c09f9ea4a2d615e2a02873fa5d Mon Sep 17 00:00:00 2001 From: asararatnakar Date: Tue, 17 Sep 2024 10:39:23 -0400 Subject: [PATCH 1/2] chore: Add testDescription helper and error processing Signed-off-by: asararatnakar --- packages/gqltest/gqltest.js | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/packages/gqltest/gqltest.js b/packages/gqltest/gqltest.js index fab2f1d..da611b2 100644 --- a/packages/gqltest/gqltest.js +++ b/packages/gqltest/gqltest.js @@ -61,10 +61,23 @@ class GQLResponse { // Asserts that the GraphQL response has status 200 and no errors. GQLResponse.prototype.expectOK = function () { chai.expect(this.response.status).to.equal(200); - chai.assert.notGraphQLError(this.body); return this; }; + +// defaultTimeout can be used in describe.timeout() +// for requests against a graphql endpoint. +// Match stepzen AWS 60 second timeout. +const defaultTimeout = 60000 + +function testDescription(testRoot, fullDirName) { + segments = fullDirName.split(path.sep) + rootIndex = segments.findIndex(element => element == testRoot) + // Construct the test description from the unique path from testRoot, which is likely the root of the git repo. + // Intentionally not using `path.sep` as this is not a path to a file now, but a test description. + return segments.slice(rootIndex + 1, -1).join('/') +} + async function _execute({ test, endpoint, @@ -132,8 +145,8 @@ async function execute({ // // (1) value rooted at `data`: {customer: {name: "Fred"}} // (2) root value with no errors: {data: {customer: {name: "Fred"}}} -// (3) not implemented - root value with field errors: {data: {customer: {name: "Fred" email:null}}, "errors":[...]} -// (4) not implemented - root value with request errors: {"errors":[...]} +// (3) root value with field errors: {data: {customer: {name: "Fred" email:null}}, "errors":[...]} +// or or request errors: {"errors":[...]} // // Workarounds for (1) if "data" or "errors" are the root fields under "data" in a response: // - use approach (2) @@ -142,19 +155,11 @@ function assertExpected(response, expected, label) { expected = optionalJSONFromFile(expected, label); // (2),(3) - Response at the root. - if (Object.hasOwn(expected, "data")) { - if (Object.hasOwn(expected, "errors")) { - chai.expect.fail("field errors in response not yet supported.") - } + if ( Object.hasOwn(expected, "data") || Object.hasOwn(expected, "errors")) { chai.expect(response.body).to.deep.equal(expected); return; } - // (4) request errors - if (Object.hasOwn(expected, "errors")) { - chai.expect.fail("request errors in response not yet supported.") - } - // (1) - Non-error response rooted at data. chai.assert.graphQL(response.body, expected); } @@ -184,6 +189,7 @@ async function runtests(label, endpoint, headers, tests) { } describe(label, function () { + this.timeout(defaultTimeout) // Occasional requests take > 2s beforeEach("test-info", function() { this.gql_title = this.currentTest.title; }) @@ -250,3 +256,5 @@ exports.GQLHeaders = GQLHeaders; exports.GQLResponse = GQLResponse; exports.logOnFail = logOnFail; exports.introspectionTests = introspectionTests; +exports.defaultTimeout = defaultTimeout +exports.testDescription = testDescription; From 9d88c4dafca4de7e5c3e4cafd1e56fb7113f8089 Mon Sep 17 00:00:00 2001 From: asararatnakar Date: Tue, 17 Sep 2024 10:53:51 -0400 Subject: [PATCH 2/2] format Signed-off-by: asararatnakar --- packages/gqltest/gqltest.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/gqltest/gqltest.js b/packages/gqltest/gqltest.js index da611b2..41628aa 100644 --- a/packages/gqltest/gqltest.js +++ b/packages/gqltest/gqltest.js @@ -7,7 +7,7 @@ const chai = require("chai"); const chaiGraphQL = require("chai-graphql"); chai.use(chaiGraphQL); -const {introspectionTests} = require("./_introspection.js"); +const { introspectionTests } = require("./_introspection.js"); // GQLHeaders holds headers for a request. // @@ -25,7 +25,7 @@ class GQLHeaders { "Content-Type": "application/json", }); if (process.env.GQLTEST_HEADERS) { - for (let [name,value] of Object.entries(JSON.parse(process.env.GQLTEST_HEADERS))) { + for (let [name, value] of Object.entries(JSON.parse(process.env.GQLTEST_HEADERS))) { this.headers.set(name, value); } } @@ -146,7 +146,7 @@ async function execute({ // (1) value rooted at `data`: {customer: {name: "Fred"}} // (2) root value with no errors: {data: {customer: {name: "Fred"}}} // (3) root value with field errors: {data: {customer: {name: "Fred" email:null}}, "errors":[...]} -// or or request errors: {"errors":[...]} +// or request errors: {"errors":[...]} // // Workarounds for (1) if "data" or "errors" are the root fields under "data" in a response: // - use approach (2) @@ -155,7 +155,7 @@ function assertExpected(response, expected, label) { expected = optionalJSONFromFile(expected, label); // (2),(3) - Response at the root. - if ( Object.hasOwn(expected, "data") || Object.hasOwn(expected, "errors")) { + if (Object.hasOwn(expected, "data") || Object.hasOwn(expected, "errors")) { chai.expect(response.body).to.deep.equal(expected); return; } @@ -190,7 +190,7 @@ async function runtests(label, endpoint, headers, tests) { describe(label, function () { this.timeout(defaultTimeout) // Occasional requests take > 2s - beforeEach("test-info", function() { + beforeEach("test-info", function () { this.gql_title = this.currentTest.title; }) afterEach("log-failure", logOnFail);