Skip to content

Commit

Permalink
Add tests for warning output
Browse files Browse the repository at this point in the history
  • Loading branch information
rmosolgo committed Dec 2, 2024
1 parent 9a0a00d commit d670173
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/graphql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ class << self
def ensure_eager_load!
if production? && !eager_loading?
warn <<~WARNING
GraphQL-Ruby thinks this is a production deployment but didn't eager load its constants. Address this by:
GraphQL-Ruby thinks this is a production deployment but didn't eager-load its constants. Address this by:
- Calling `GraphQL.eager_load!` in a production-only initializer or setup hook
- Assign `GraphQL.env = "..."` to something _other_ than `"production"` (for example, `GraphQL.env = "development"`)
- Calling `GraphQL.eager_load!` in a production-only initializer or setup hook
- Assign `GraphQL.env = "..."` to something _other_ than `"production"` (for example, `GraphQL.env = "development"`)
More details: https://graphql-ruby.org/schema/definition#production-considerations
WARNING
Expand Down
40 changes: 40 additions & 0 deletions spec/graphql/autoload_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,44 @@ def self.eager_load!
assert EagerModule::NestedEagerModule::NestedEagerClass
end
end


describe "warning in production" do
before do
@prev_env = ENV.to_hash
ENV.update("HANAMI_ENV" => "production")
end

after do
ENV.update(@prev_env)
end

it "emits a warning when not eager-loading" do
stdout, stderr = capture_io do
GraphQL.ensure_eager_load!
end

assert_equal "", stdout
expected_warning = "GraphQL-Ruby thinks this is a production deployment but didn't eager-load its constants. Address this by:
- Calling `GraphQL.eager_load!` in a production-only initializer or setup hook
- Assign `GraphQL.env = \"...\"` to something _other_ than `\"production\"` (for example, `GraphQL.env = \"development\"`)
More details: https://graphql-ruby.org/schema/definition#production-considerations
"
assert_equal expected_warning, stderr
end

it "silences the warning when GraphQL.env is assigned" do
prev_env = GraphQL.env
GraphQL.env = "staging"
stdout, stderr = capture_io do
GraphQL.ensure_eager_load!
end
assert_equal "", stdout
assert_equal "", stderr
ensure
GraphQL.env = prev_env
end
end
end

0 comments on commit d670173

Please sign in to comment.