Sha256: 1589a6b5a29cb2e871c5b0dd163ebeabefa26a51be49990c6bba6ad2be1f8145

Contents?: true

Size: 1.83 KB

Versions: 9

Compression:

Stored size: 1.83 KB

Contents

# frozen_string_literal: true

require 'test_helper'

class DevServerRunnerTest < ViteRails::Test
  def setup
    @original_node_env, ENV['NODE_ENV'] = ENV['NODE_ENV'], 'development'
    @original_rails_env, ENV['RAILS_ENV'] = ENV['RAILS_ENV'], 'development'
  end

  def teardown
    ENV['NODE_ENV'] = @original_node_env
    ENV['RAILS_ENV'] = @original_rails_env
  end

  def test_run_cmd_via_node_modules
    cmd = ["#{ test_app_path }/node_modules/.bin/vite", '--mode', 'development']

    verify_command(cmd, use_node_modules: true)
  end

  def test_run_cmd_via_yarn
    cmd = ['yarn', 'vite', '--mode', 'development']

    verify_command(cmd, use_node_modules: false)
  end

  def test_run_cmd_argv
    cmd = ["#{ test_app_path }/node_modules/.bin/vite", '--quiet', '--mode', 'development']

    verify_command(cmd, argv: ['--quiet'])
  end

  def test_run_cmd_argv_with_https
    cmd = ["#{ test_app_path }/node_modules/.bin/vite", '--https', '--mode', 'development']

    dev_server = ViteRails::DevServer.new({})
    def dev_server.host
      'localhost'
    end

    def dev_server.port
      '3035'
    end

    def dev_server.pretty?
      false
    end

    def dev_server.https?
      true
    end
    ViteRails::DevServer.stub(:new, dev_server) do
      verify_command(cmd, argv: ['--https'])
    end
  end

private

  def test_app_path
    File.expand_path('test_app', __dir__)
  end

  def verify_command(cmd, use_node_modules: true, argv: [])
    cwd = Dir.pwd
    Dir.chdir(test_app_path)

    klass = ViteRails::Runner
    instance = klass.new(argv)
    mock = Minitest::Mock.new
    mock.expect(:call, nil, [ViteRails.env, *cmd])

    klass.stub(:new, instance) do
      instance.stub(:executable_exists?, use_node_modules) do
        Kernel.stub(:exec, mock) { ViteRails.run(argv) }
      end
    end

    mock.verify
  ensure
    Dir.chdir(cwd)
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
vite_rails-1.0.8 test/dev_server_runner_test.rb
vite_rails-1.0.7 test/dev_server_runner_test.rb
vite_rails-1.0.6 test/dev_server_runner_test.rb
vite_rails-1.0.5 test/dev_server_runner_test.rb
vite_rails-1.0.4 test/dev_server_runner_test.rb
vite_rails-1.0.3 test/dev_server_runner_test.rb
vite_rails-1.0.2 test/dev_server_runner_test.rb
vite_rails-1.0.1 test/dev_server_runner_test.rb
vite_rails-1.0.0 test/dev_server_runner_test.rb