class SleepingKingStudios::Tasks::Ci::JestResults

Encapsulates the results of a Jest call.

Public Class Methods

new(results) click to toggle source

@param results [Hash] The raw results of the Jest call.

# File lib/sleeping_king_studios/tasks/ci/jest_results.rb, line 9
def initialize results
  @results = results
end

Public Instance Methods

==(other) click to toggle source

@param other [JestResults] The other results object to compare.

@return [Boolean] True if the results are equal, otherwise false.

# File lib/sleeping_king_studios/tasks/ci/jest_results.rb, line 16
def == other
  if other.is_a?(Hash)
    empty? ? other.empty? : @results == other
  elsif other.is_a?(JestResults)
    to_h == other.to_h
  else
    false
  end
end
duration() click to toggle source

@return [Float] The duration value, in seconds.

# File lib/sleeping_king_studios/tasks/ci/jest_results.rb, line 27
def duration
  (end_time - start_time).to_f / 1000
end
empty?() click to toggle source

@return [Boolean] True if there are no tests, otherwise false.

# File lib/sleeping_king_studios/tasks/ci/jest_results.rb, line 32
def empty?
  test_count.zero?
end
failing?() click to toggle source

@return [Boolean] True if there are any failing tests, otherwise false.

# File lib/sleeping_king_studios/tasks/ci/jest_results.rb, line 37
def failing?
  !failure_count.zero?
end
failure_count() click to toggle source

@return [Integer] The number of failing tests.

# File lib/sleeping_king_studios/tasks/ci/jest_results.rb, line 42
def failure_count
  @results.fetch('numFailedTests', 0)
end
pending?() click to toggle source

@return [Boolean] True if there are any pending tests, otherwise false.

# File lib/sleeping_king_studios/tasks/ci/jest_results.rb, line 47
def pending?
  !pending_count.zero?
end
pending_count() click to toggle source

@return [Intger] The number of pending tests.

# File lib/sleeping_king_studios/tasks/ci/jest_results.rb, line 52
def pending_count
  @results.fetch('numPendingTests', 0)
end
test_count() click to toggle source

@return [Integer] The total number of tests.

# File lib/sleeping_king_studios/tasks/ci/jest_results.rb, line 57
def test_count
  @results.fetch('numTotalTests', 0)
end
to_h() click to toggle source

@return [Hash] The hash representation of the results.

# File lib/sleeping_king_studios/tasks/ci/jest_results.rb, line 62
def to_h
  {
    'duration'      => duration,
    'failure_count' => failure_count,
    'pending_count' => pending_count,
    'test_count'    => test_count
  }
end
to_s() click to toggle source

@return [String] The string representation of the results.

# File lib/sleeping_king_studios/tasks/ci/jest_results.rb, line 72
def to_s # rubocop:disable Metrics/AbcSize
  str = +pluralize(test_count, 'test')

  str << ', ' << pluralize(failure_count, 'failure')

  str << ', ' << pending_count.to_s << ' pending' if pending?

  str << " in #{duration.round(2)} seconds"
end

Private Instance Methods

end_time() click to toggle source
# File lib/sleeping_king_studios/tasks/ci/jest_results.rb, line 84
def end_time
  return 0 unless @results['testResults']

  @results['testResults'].
    map { |test_result| test_result['endTime'] }.
    reduce do |memo, time|
      [memo, time].max
    end
end
pluralize(count, singular, plural = nil) click to toggle source
# File lib/sleeping_king_studios/tasks/ci/jest_results.rb, line 94
def pluralize count, singular, plural = nil
  "#{count} #{tools.int.pluralize count, singular, plural}"
end
start_time() click to toggle source
# File lib/sleeping_king_studios/tasks/ci/jest_results.rb, line 98
def start_time
  @results['startTime'] || 0
end
tools() click to toggle source
# File lib/sleeping_king_studios/tasks/ci/jest_results.rb, line 102
def tools
  SleepingKingStudios::Tools::Toolbelt.instance
end