Sha256: e2d56db20d592d051bccc1b977dbbdf4196c6ff77709503e2b64ea72092321ee

Contents?: true

Size: 1.52 KB

Versions: 2

Compression:

Stored size: 1.52 KB

Contents

# frozen_string_literal: true

require 'simplecov'
SimpleCov.start {
  add_filter '/test/'
}

require 'minitest/autorun'
require 'rails'
require 'rails/test_help'
require 'pry-byebug'

require_relative 'test_app/config/environment'

Rails.env = 'production'

ViteRails.instance = ViteRails.new

module ViteRailsTestHelpers
  def refresh_config(env_variables = ViteRails.load_env_variables)
    ViteRails.env = env_variables
    (ViteRails.instance = ViteRails.new).config
  end

  def with_rails_env(env)
    original = Rails.env
    Rails.env = ActiveSupport::StringInquirer.new(env)
    yield(refresh_config)
  ensure
    Rails.env = ActiveSupport::StringInquirer.new(original)
    refresh_config
  end

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

  def with_dev_server_running(&block)
    ViteRails.instance.stub(:dev_server_running?, true, &block)
  end
end

class ViteRails::Test < Minitest::Test
  include ViteRailsTestHelpers

private

  def assert_run_command(*argv, use_yarn: false, flags: [])
    command = use_yarn ? %w[yarn vite] : ["#{ test_app_path }/node_modules/.bin/vite"]
    cwd = Dir.pwd
    Dir.chdir(test_app_path)

    klass = ViteRails::Runner
    instance = klass.new(argv)
    mock = Minitest::Mock.new
    mock.expect(:call, nil, [ViteRails.config.to_env, *command, *argv, *flags])

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

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
vite_rails-1.0.12 test/test_helper.rb
vite_rails-1.0.11 test/test_helper.rb