From 930ea01aee7ecda708c563911bcc48da750853ed Mon Sep 17 00:00:00 2001
From: Paul Slaughter <pslaughter@gitlab.com>
Date: Wed, 24 Mar 2021 09:18:01 -0500
Subject: [PATCH] Add gitlab_schema.gql warning in integration spec

- A missing gitlab_schema.graphql file can cause
  unrelated and hard to debug issues.
- This can be overwritten with GL_IGNORE_WARNINGS=1
---
 .../helpers/check_frontend_integration_env.js | 37 +++++++++++++++++++
 jest.config.integration.js                    |  3 ++
 .../test_helpers/mock_server/graphql.js       | 11 +++---
 3 files changed, 45 insertions(+), 6 deletions(-)
 create mode 100644 config/helpers/check_frontend_integration_env.js

diff --git a/config/helpers/check_frontend_integration_env.js b/config/helpers/check_frontend_integration_env.js
new file mode 100644
index 0000000000000..38393c89445fb
--- /dev/null
+++ b/config/helpers/check_frontend_integration_env.js
@@ -0,0 +1,37 @@
+const fs = require('fs');
+const isESLint = require('./is_eslint');
+
+const GRAPHQL_SCHEMA_PATH = 'tmp/tests/graphql/gitlab_schema.graphql';
+const GRAPHQL_SCHEMA_JOB = 'bundle exec rake gitlab:graphql:schema:dump';
+
+const shouldIgnoreWarnings = JSON.parse(process.env.GL_IGNORE_WARNINGS || '0');
+
+const failCheck = (message) => {
+  console.error(message);
+
+  if (!shouldIgnoreWarnings) {
+    process.exit(1);
+  }
+};
+
+const checkGraphqlSchema = () => {
+  if (!fs.existsSync(GRAPHQL_SCHEMA_PATH)) {
+    const message = `
+ERROR: Expected to find "${GRAPHQL_SCHEMA_PATH}" but file does not exist. Try running:
+
+    ${GRAPHQL_SCHEMA_JOB}
+`;
+
+    failCheck(message);
+  }
+};
+
+const check = () => {
+  if (isESLint(module)) {
+    return;
+  }
+
+  checkGraphqlSchema();
+};
+
+module.exports = check;
diff --git a/jest.config.integration.js b/jest.config.integration.js
index d85e14fe218d7..92296fb751e19 100644
--- a/jest.config.integration.js
+++ b/jest.config.integration.js
@@ -1,5 +1,8 @@
+const checkEnvironment = require('./config/helpers/check_frontend_integration_env');
 const baseConfig = require('./jest.config.base');
 
+checkEnvironment();
+
 module.exports = {
   ...baseConfig('spec/frontend_integration', {
     moduleNameMapper: {
diff --git a/spec/frontend_integration/test_helpers/mock_server/graphql.js b/spec/frontend_integration/test_helpers/mock_server/graphql.js
index e265885259974..27396842523e5 100644
--- a/spec/frontend_integration/test_helpers/mock_server/graphql.js
+++ b/spec/frontend_integration/test_helpers/mock_server/graphql.js
@@ -1,13 +1,11 @@
 import { buildSchema, graphql } from 'graphql';
+import { memoize } from 'lodash';
 
-/* eslint-disable import/no-unresolved */
-// This rule is disabled for the following line.
 // The graphql schema is dynamically generated in CI
 // during the `graphql-schema-dump` job.
-import gitlabSchemaStr from '../../../../tmp/tests/graphql/gitlab_schema.graphql';
-/* eslint-enable import/no-unresolved */
+// eslint-disable-next-line global-require, import/no-unresolved
+const getGraphqlSchema = () => require('../../../../tmp/tests/graphql/gitlab_schema.graphql');
 
-const graphqlSchema = buildSchema(gitlabSchemaStr.loc.source.body);
 const graphqlResolvers = {
   project({ fullPath }, schema) {
     const result = schema.projects.findBy({ path_with_namespace: fullPath });
@@ -21,6 +19,7 @@ const graphqlResolvers = {
     };
   },
 };
+const buildGraphqlSchema = memoize(() => buildSchema(getGraphqlSchema().loc.source.body));
 
 export const graphqlQuery = (query, variables, schema) =>
-  graphql(graphqlSchema, query, graphqlResolvers, schema, variables);
+  graphql(buildGraphqlSchema(), query, graphqlResolvers, schema, variables);
-- 
GitLab