bundler_test.go
package esbuild
import (
"os"
"path/filepath"
"strings"
"testing"
)
func TestNewBundler(t *testing.T) {
cfg := &Config{
Entry: "components/index.ts",
Include: []string{"components"},
}
b := NewBundler(cfg, "output/gen", true)
if b == nil {
t.Fatal("expected bundler to be created")
}
if b.outputDir != "output/gen" {
t.Errorf("expected outputDir 'output/gen', got %q", b.outputDir)
}
if !b.devMode {
t.Error("expected devMode to be true")
}
}
func TestBundler_Config(t *testing.T) {
cfg := &Config{
Entry: "src/index.ts",
Include: []string{"src", "lib"},
}
b := NewBundler(cfg, "dist", false)
got := b.Config()
if got != cfg {
t.Error("expected Config() to return the same config pointer")
}
if got.Entry != "src/index.ts" {
t.Errorf("expected Entry 'src/index.ts', got %q", got.Entry)
}
if len(got.Include) != 2 {
t.Errorf("expected 2 Include dirs, got %d", len(got.Include))
}
}
func TestBundler_ConfigNil(t *testing.T) {
b := NewBundler(nil, "dist", false)
got := b.Config()
if got != nil {
t.Error("expected Config() to return nil when initialized with nil config")
}
}
func TestGenerateVirtualEntry(t *testing.T) {
// Test the unexported generateVirtualEntry function directly (same package)
result := generateVirtualEntry("components/index.ts")
if !strings.Contains(result, "./components/index.ts") {
t.Error("expected import path to include ./components/index.ts")
}
if !strings.Contains(result, "__render") {
t.Error("expected virtual entry to assign __render to window")
}
if !strings.Contains(result, "__unmount") {
t.Error("expected virtual entry to assign __unmount to window")
}
if !strings.Contains(result, "import * as __all") {
t.Error("expected virtual entry to import all exports")
}
}
func TestGenerateVirtualEntry_CustomPath(t *testing.T) {
result := generateVirtualEntry("src/main.tsx")
if !strings.Contains(result, "./src/main.tsx") {
t.Error("expected import path to include ./src/main.tsx")
}
}
func TestBuild_NoEntry(t *testing.T) {
tmpDir := t.TempDir()
outputDir := filepath.Join(tmpDir, "output")
cfg := &Config{
Entry: filepath.Join(tmpDir, "nonexistent/index.ts"),
}
b := NewBundler(cfg, outputDir, false)
err := b.Build()
if err != nil {
t.Fatalf("expected no error for missing entry, got: %v", err)
}
// Should create an empty bundle
bundlePath := filepath.Join(outputDir, "components.js")
data, err := os.ReadFile(bundlePath)
if err != nil {
t.Fatalf("expected empty bundle file to be created: %v", err)
}
content := string(data)
if !strings.Contains(content, "No components") {
t.Error("expected empty bundle to contain 'No components' comment")
}
if !strings.Contains(content, "__render") {
t.Error("expected empty bundle to define __render stub")
}
if !strings.Contains(content, "__unmount") {
t.Error("expected empty bundle to define __unmount stub")
}
}
func TestBuild_NoEntry_CreatesOutputDir(t *testing.T) {
tmpDir := t.TempDir()
outputDir := filepath.Join(tmpDir, "nested", "output", "dir")
cfg := &Config{
Entry: filepath.Join(tmpDir, "nonexistent/index.ts"),
}
b := NewBundler(cfg, outputDir, true)
err := b.Build()
if err != nil {
t.Fatalf("expected no error, got: %v", err)
}
// Verify the nested output directory was created
if _, err := os.Stat(outputDir); os.IsNotExist(err) {
t.Error("expected output directory to be created")
}
}