Skip to content

Commit 8e7e8d2

Browse files
authored
chore: switch to use stepzen-dev/gqltest test utility (#55)
1 parent dfd12ca commit 8e7e8d2

File tree

3 files changed

+157
-211
lines changed

3 files changed

+157
-211
lines changed

tests/gqltest.js

Lines changed: 26 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
require("dotenv").config();
2-
const fetch = require("node-fetch");
3-
const { expect } = require("chai");
42
const { execSync } = require("child_process");
53
const { URL } = require("url");
64
const path = require("node:path");
75

6+
const {
7+
GQLHeaders,
8+
executeOK,
9+
logOnFail,
10+
} = require('gqltest/packages/gqltest/gqltest.js')
11+
812
const authTypes = {
913
adminKey: 1,
1014
apiKey: 2,
@@ -15,10 +19,8 @@ Object.freeze(authTypes);
1519

1620
// We use admin key to test because there is a cache optimization for apikey's that is not conducive
1721
// to rapid deploy and run cycles that occur with this type of testing
18-
const adminKey =
19-
`apikey ` + execSync(`stepzen whoami --adminkey`).toString().trim();
20-
const apiKey =
21-
`apikey ` + execSync(`stepzen whoami --apikey`).toString().trim();
22+
const adminKey = execSync(`stepzen whoami --adminkey`).toString().trim();
23+
const apiKey = execSync(`stepzen whoami --apikey`).toString().trim();
2224

2325
const endpoint = process.env.STEPZEN_ENDPOINT;
2426

@@ -45,56 +47,31 @@ function deployEndpoint(endpoint, dirname) {
4547
// as a test returning the response.
4648
// The test will fail if the request does not
4749
// have status 200 or has any GraphQL errors.
48-
function runGqlOk(authType, endpoint, query, variables, operationName) {
49-
let authValue;
50+
async function runGqlOk(authType, endpoint, query, variables, operationName, expected) {
51+
let headers = new GQLHeaders();
5052
switch (authType) {
5153
case authTypes.adminKey:
52-
authValue = adminKey;
54+
headers.withAPIKey(adminKey);
5355
break;
5456
case authTypes.apiKey:
55-
authValue = apiKey;
57+
headers.withAPIKey(apiKey);
5658
break;
57-
// Have not implemented jwt and noAuth yet
59+
// Have not implemented jwt yet
5860
case authTypes.jwt:
5961
case authTypes.noAuth:
6062
default:
61-
authValue = "";
62-
}
63-
64-
let headers = {
65-
"Content-Type": "application/json",
66-
};
67-
68-
if (authValue) {
69-
headers.Authorization = authValue;
7063
}
71-
72-
return fetch(endpoint, {
73-
method: "POST",
74-
headers: headers,
75-
body: JSON.stringify({
64+
await executeOK({
65+
test: this,
66+
endpoint,
67+
headers,
68+
request: {
7669
query: query,
7770
variables: variables,
7871
operationName: operationName,
79-
}),
72+
},
73+
expected,
8074
})
81-
.then(function (result) {
82-
expect(result.status).to.equal(200);
83-
return result;
84-
})
85-
.then(function (result) {
86-
return result.json();
87-
})
88-
.then(function (response) {
89-
expect(response.errors, `no errors should exist: ${JSON.stringify(response.errors)}`).to.be.undefined;
90-
return response;
91-
});
92-
}
93-
94-
// tests that the data key in a GraphQL response
95-
// is equal to value.
96-
function expectData(response, value) {
97-
expect(response.data).to.eql(value);
9875
}
9976

10077
// deploys graphql schema located in dirname to the test endpoint provided by the environment (process.env.STEPZEN_ENDPOINT),
@@ -108,19 +85,19 @@ function deployAndRun(dirname, tests) {
10885
return deployEndpoint(endpoint, dirname);
10986
});
11087

88+
afterEach('log-failure', logOnFail)
11189
tests.forEach(
11290
({ label, query, variables, operationName, expected, authType }) => {
113-
it(label, function () {
91+
it(label, async function () {
11492
this.timeout(4000); // Occasional requests take > 2s
115-
return runGqlOk(
93+
return await runGqlOk(
11694
authType,
11795
endpoint,
11896
query,
11997
variables,
120-
operationName
121-
).then(function (response) {
122-
expectData(response, expected);
123-
});
98+
operationName,
99+
expected,
100+
);
124101
});
125102
}
126103
);
@@ -134,8 +111,6 @@ function getTestDescription(testRoot, fullDirName) {
134111
return segments.slice(rootIndex + 1, -1).join("/");
135112
}
136113

137-
exports.runGqlOk = runGqlOk;
138-
exports.expectData = expectData;
139114
exports.deployAndRun = deployAndRun;
140115
exports.authTypes = authTypes;
141116
exports.getTestDescription = getTestDescription;

0 commit comments

Comments
 (0)