guard-jest 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 12086254600f743764afab326f8cd7d944139982
4
- data.tar.gz: 754a8c45c368998d810518e74dd4968f9baa0733
3
+ metadata.gz: e51173ecdd1ecbf732e9838991950132fcde518f
4
+ data.tar.gz: 1ede3705d1d72367abc4b91e91b9fdd1e243437a
5
5
  SHA512:
6
- metadata.gz: f8d72ca3d8057279249bfafe8709925b4f058ea484180c72a46764ea3e2b52f0e2f178c83ada98a12f3df29d6ea619951b11b2f5f2c0decd27779a0ae9567036
7
- data.tar.gz: 02c4369e5df3c54de3088e940638baa79fdd81163b87b095d43a7fefd301198d0901446d0f3e030443d31c88c53776fc6fdf72308bd3e5ad305956e2333cb38c
6
+ metadata.gz: 58e8e05287085d9b1ed0ee308fc2796fe1cf96cac926e746372b1553936c274419a9e0068564311e3fc1b4e50a72f6c468382e4856777d3223c4e0dccee78f68
7
+ data.tar.gz: c0364f4dd06632399822b389924f194f98b31683db514828d0ba12b22b03b46e14a2a58e7162572c4998839f0b8b0dee1da909a44447f7809edb470cb28e0e9b
data/README.md CHANGED
@@ -38,6 +38,11 @@ guard 'jest' do
38
38
  end
39
39
  ```
40
40
 
41
+ ## Interactive use
42
+
43
+ From the Guard console, typeing 'j u' and pressing enter will trigger a snaphot update.
44
+
45
+
41
46
  ## Options
42
47
 
43
48
  There are many options that can customize Guard::Jest to your needs. Options are simply supplied as hash when
@@ -1,11 +1,10 @@
1
1
  # coding: utf-8
2
2
  lib = File.expand_path('../lib', __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'guard/jest/version'
5
4
 
6
5
  Gem::Specification.new do |spec|
7
6
  spec.name = "guard-jest"
8
- spec.version = Guard::Jest::VERSION
7
+ spec.version = '0.1.1'
9
8
  spec.authors = ["Nathan Stitt"]
10
9
  spec.email = ["nathan@stitt.org"]
11
10
 
@@ -19,6 +19,7 @@ module Guard
19
19
  end
20
20
 
21
21
  DEFAULT_OPTIONS = {
22
+ silent: true,
22
23
  jest_cmd: 'jest'
23
24
  }.freeze
24
25
 
@@ -33,6 +34,7 @@ module Guard
33
34
  def initialize(options = {})
34
35
  options = DEFAULT_OPTIONS.merge(options)
35
36
  options[:server] = @server = options[:server] || Server.new(options)
37
+ Jest.logger = options[:logger] if options[:logger]
36
38
  @runner = options[:runner] || Runner.new(options)
37
39
  super(options)
38
40
  end
@@ -45,6 +47,15 @@ module Guard
45
47
  def start
46
48
  throw :task_has_failed unless server.start
47
49
  run_all if options[:all_on_start]
50
+ Pry::Commands.block_command "j", "Update Jest snapshots" do | cmd |
51
+ server = Guard.state.session.plugins.all(:jest).first.runner.server
52
+ case cmd
53
+ when 'u' then server.update_snapshots
54
+ else
55
+ Formatter.error("unknown command #{cmd}")
56
+ end
57
+
58
+ end
48
59
  end
49
60
 
50
61
  # Called when `stop|quit|exit|s|q|e + enter` is pressed (when Guard quits).
@@ -4,10 +4,9 @@ module Guard
4
4
  class Jest < Plugin
5
5
  class RunRequest
6
6
 
7
- attr_reader :paths, :runner, :result
7
+ attr_reader :paths, :result
8
8
 
9
- def initialize(runner, paths)
10
- @runner = runner
9
+ def initialize(paths = :all)
11
10
  @paths = paths
12
11
  @end_time = Time.now
13
12
  @start_time = Time.now
@@ -34,7 +33,48 @@ module Guard
34
33
  @end_time = Time.now
35
34
  @is_complete.make_true
36
35
  @result = json
37
- runner.notify(self)
36
+ notify(json)
37
+ end
38
+
39
+ def notify(json)
40
+ r = json
41
+
42
+ specs = r['numTotalTests'] - r['numPendingTests']
43
+ failed = r['numFailedTests']
44
+ specs_plural = specs == 1 ? '' : 's'
45
+ failed_plural = failed == 1 ? '' : 's'
46
+
47
+ Formatter.info("Finished in #{elapsed_seconds} seconds")
48
+
49
+ pending = r['numPendingTests'] > 0 ? " #{r['numPendingTests']} pending," : ''
50
+ message = "#{specs} spec#{specs_plural}," \
51
+ "#{pending} #{failed} failure#{failed_plural}"
52
+ full_message = "#{message}\nin #{elapsed_seconds} seconds"
53
+
54
+ if failed.zero?
55
+ Formatter.success(message)
56
+ Formatter.notify(full_message, title: 'Jest suite passed')
57
+ else
58
+ Formatter.error(
59
+ collect_spec_error_messages(r['testResults']).join("\n")
60
+ )
61
+ error_title = collect_spec_error_titles(r['testResults']).join("\n")
62
+ Formatter.notify("#{error_title}\n#{full_message}",
63
+ title: 'Jest test run failed', image: :failed, priority: 2)
64
+ end
65
+ end
66
+
67
+ def collect_spec_error_titles(specs)
68
+ specs.select { |s| s['status'] == 'failed' }.map do |s|
69
+ result = s['assertionResults'].detect { |res| res['status'] == 'failed' }
70
+ result ? result['title'] : 'Unknown failure'
71
+ end
72
+ end
73
+
74
+ def collect_spec_error_messages(specs)
75
+ specs.select { |s| s['status'] == 'failed' }.map do |s|
76
+ s['message']
77
+ end
38
78
  end
39
79
  end
40
80
  end
@@ -20,59 +20,17 @@ module Guard
20
20
  end
21
21
 
22
22
  def test_all
23
- server.run(RunRequest.new(self, :all))
23
+ server.run(RunRequest.new(:all))
24
24
  end
25
25
 
26
26
  def test_paths(paths)
27
27
  paths = paths.concat(last_failed_paths) if options[:keep_failed]
28
-
29
- server.run(RunRequest.new(self, paths.uniq.compact))
28
+ server.run(RunRequest.new(paths.uniq.compact))
30
29
  end
31
30
 
32
31
  def remove_paths(paths)
33
32
  self.last_failed_paths -= paths
34
33
  end
35
-
36
- def notify(request)
37
- @last_result = r = request.result
38
-
39
- specs = r['numTotalTests'] - r['numPendingTests']
40
- failed = r['numFailedTests']
41
- specs_plural = specs == 1 ? '' : 's'
42
- failed_plural = failed == 1 ? '' : 's'
43
-
44
- Formatter.info("Finished in #{request.elapsed_seconds} seconds")
45
-
46
- pending = r['numPendingTests'] > 0 ? " #{r['numPendingTests']} pending," : ''
47
- message = "#{specs} spec#{specs_plural}," \
48
- "#{pending} #{failed} failure#{failed_plural}"
49
- full_message = "#{message}\nin #{request.elapsed_seconds} seconds"
50
-
51
- if failed.zero?
52
- Formatter.success(message)
53
- Formatter.notify(full_message, title: 'Jest suite passed')
54
- else
55
- Formatter.error(
56
- collect_spec_error_messages(r['testResults']).join("\n")
57
- )
58
- error_title = collect_spec_error_titles(r['testResults']).join("\n")
59
- Formatter.notify("#{error_title}\n#{full_message}",
60
- title: 'Jest test run failed', image: :failed, priority: 2)
61
- end
62
- end
63
-
64
- def collect_spec_error_titles(specs)
65
- specs.select { |s| s['status'] == 'failed' }.map do |s|
66
- result = s['assertionResults'].detect { |res| res['status'] == 'failed' }
67
- result ? result['title'] : 'Unknown failure'
68
- end
69
- end
70
-
71
- def collect_spec_error_messages(specs)
72
- specs.select { |s| s['status'] == 'failed' }.map do |s|
73
- s['message']
74
- end
75
- end
76
34
  end
77
35
  end
78
36
  end
@@ -1,3 +1,4 @@
1
+ # coding: utf-8
1
2
  require 'concurrent/array'
2
3
  require 'concurrent/atomic/atomic_boolean'
3
4
  require 'pty'
@@ -6,9 +7,11 @@ require 'json'
6
7
  module Guard
7
8
  class Jest < Plugin
8
9
 
10
+ IGNORED_LINES = /(?:Watch Usage| › Press )/
11
+
9
12
  class Server
10
13
  CR = 13.chr
11
-
14
+ CLEAR = "\x1B[2J\x1B[3J\x1B[H"
12
15
  attr_reader :stdout, :stdin, :pid, :last_result, :options, :cmd, :pending
13
16
 
14
17
  def initialize(options = {})
@@ -36,6 +39,10 @@ module Guard
36
39
  sleep(0.1) while busy?
37
40
  end
38
41
 
42
+ def update_snapshots
43
+ stdin.write('u')
44
+ end
45
+
39
46
  def start
40
47
  @threads = []
41
48
  @work_in_progress.make_true
@@ -59,6 +66,7 @@ module Guard
59
66
  end
60
67
 
61
68
  def stop
69
+ return unless alive?
62
70
  stdin.write('q')
63
71
  sleep(0.1)
64
72
  return unless alive?
@@ -71,7 +79,8 @@ module Guard
71
79
  def reload(options)
72
80
  @options = options
73
81
  @directory = options[:directory]
74
- @cmd = options[:jest_cmd] + ' --json --silent true --watch'
82
+ @cmd = options[:jest_cmd] + ' --json --watch'
83
+ @cmd << ' --silent' if options[:silent]
75
84
  @cmd << " --config #{options[:config_file]}" if options[:config_file]
76
85
  if alive?
77
86
  stop
@@ -107,26 +116,43 @@ module Guard
107
116
  stdin.write(CR)
108
117
  end
109
118
 
119
+ def log(line)
120
+ return if options[:silent] || line =~ IGNORED_LINES
121
+ line.sub!(CLEAR, '') # stop Jest from clearing console history
122
+ Jest.logger.info(line.chomp)
123
+ end
124
+
110
125
  def record_result(line)
111
126
  # looks vaguely jsonish if it starts with {"
112
- return unless line.start_with?('{"')
127
+
128
+ unless line =~ /\{\"numFailedTestSuites\"\:\d+/
129
+ log(line)
130
+ return
131
+ end
132
+
113
133
  begin
134
+ line.sub!(/\A.*?\{/, '{')
135
+ line.sub!(/\}.*?\z/, '}')
114
136
  json = JSON.parse(line)
115
137
  result = @pending.pop
116
- result.satisfy(json) if result
138
+ if result
139
+ stdin.write('o') # revert back to watching changed files
140
+ else
141
+ result = RunRequest.new
142
+ end
143
+ result.satisfy(json)
117
144
  @work_in_progress.make_false
118
145
  work_fifo_queue
119
- rescue => e
120
- Jest.logger.warn e
146
+ rescue e
147
+ Jest.logger.warn e.class.to_s + ": " + e.message
148
+ puts e.backtrace.join("\n")
121
149
  end
122
150
  end
123
151
 
124
152
  def spawn
125
- Jest.logger.debug "starting jest with #{cmd}"
153
+ Jest.logger.info "Starting Jest with #{cmd}"
126
154
  @stdout, @stdin, @pid = PTY.spawn(cmd)
127
155
  end
128
-
129
156
  end
130
-
131
157
  end
132
158
  end
@@ -1,7 +1,7 @@
1
1
  require 'guard/compat/plugin'
2
2
 
3
3
  module Guard
4
- class Jest < Plugin
5
- VERSION = "0.1.0"
6
- end
4
+ class Jest < Plugin
5
+ VERSION = "0.1.1"
6
+ end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: guard-jest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Stitt
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-01-17 00:00:00.000000000 Z
11
+ date: 2017-02-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: guard-compat
@@ -141,7 +141,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
141
141
  version: '0'
142
142
  requirements: []
143
143
  rubyforge_project:
144
- rubygems_version: 2.6.8
144
+ rubygems_version: 2.5.1
145
145
  signing_key:
146
146
  specification_version: 4
147
147
  summary: Guard plugin for auto-running Jest specs