Skip to content

Commit cbafbe3

Browse files
committed
chore: switch to use stepzen-dev/gqltest test utility
1 parent 0445a6b commit cbafbe3

File tree

3 files changed

+156
-202
lines changed

3 files changed

+156
-202
lines changed

tests/gqltest.js

Lines changed: 25 additions & 42 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,49 +47,32 @@ 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) {
50+
async function runGqlOk(authType, endpoint, query, variables, operationName, expected) {
51+
let headers = new GQLHeaders();
4952
switch (authType) {
5053
case authTypes.adminKey:
51-
authValue = adminKey;
54+
headers.withAPIKey(adminKey);
5255
break;
5356
case authTypes.apiKey:
54-
authValue = apiKey;
57+
headers.withAPIKey(apiKey);
5558
break;
5659
// Have not implemented jwt and noAuth yet
5760
case authTypes.jwt:
5861
case authTypes.noAuth:
5962
default:
6063
authValue = "";
6164
}
62-
return fetch(endpoint, {
63-
method: "POST",
64-
headers: {
65-
"Content-Type": "application/json",
66-
Authorization: authValue,
67-
},
68-
body: JSON.stringify({
65+
await executeOK({
66+
test: this,
67+
endpoint,
68+
headers,
69+
request: {
6970
query: query,
7071
variables: variables,
7172
operationName: operationName,
72-
}),
73+
},
74+
expected,
7375
})
74-
.then(function (result) {
75-
expect(result.status).to.equal(200);
76-
return result;
77-
})
78-
.then(function (result) {
79-
return result.json();
80-
})
81-
.then(function (response) {
82-
expect(response.errors, `no errors should exist: ${JSON.stringify(response.errors)}`).to.be.undefined;
83-
return response;
84-
});
85-
}
86-
87-
// tests that the data key in a GraphQL response
88-
// is equal to value.
89-
function expectData(response, value) {
90-
expect(response.data).to.eql(value);
9176
}
9277

9378
// deploys graphql schema located in dirname to the test endpoint provided by the environment (process.env.STEPZEN_ENDPOINT),
@@ -101,19 +86,19 @@ function deployAndRun(dirname, tests) {
10186
return deployEndpoint(endpoint, dirname);
10287
});
10388

89+
afterEach('log-failure', logOnFail)
10490
tests.forEach(
10591
({ label, query, variables, operationName, expected, authType }) => {
106-
it(label, function () {
92+
it(label, async function () {
10793
this.timeout(4000); // Occasional requests take > 2s
108-
return runGqlOk(
94+
return await runGqlOk(
10995
authType,
11096
endpoint,
11197
query,
11298
variables,
113-
operationName
114-
).then(function (response) {
115-
expectData(response, expected);
116-
});
99+
operationName,
100+
expected,
101+
);
117102
});
118103
}
119104
);
@@ -127,8 +112,6 @@ function getTestDescription(testRoot, fullDirName) {
127112
return segments.slice(rootIndex + 1, -1).join("/");
128113
}
129114

130-
exports.runGqlOk = runGqlOk;
131-
exports.expectData = expectData;
132115
exports.deployAndRun = deployAndRun;
133116
exports.authTypes = authTypes;
134117
exports.getTestDescription = getTestDescription;

0 commit comments

Comments
 (0)