github.com/tilt-dev/wat@v0.0.2-0.20180626175338-9349b638e250/cli/wat/plugin_nodejs_test.go (about)

     1  package wat
     2  
     3  import (
     4  	"reflect"
     5  	"sort"
     6  	"strings"
     7  	"testing"
     8  )
     9  
    10  func TestPluginNodeJSEmptyRepo(t *testing.T) {
    11  	f := newWatFixture(t)
    12  	defer f.tearDown()
    13  
    14  	plugin := PluginNodeJS{}
    15  	cmds, err := plugin.run(f.ctx, f.root.Path())
    16  	if err != nil {
    17  		t.Fatal(err)
    18  	}
    19  
    20  	if len(cmds) != 0 {
    21  		t.Errorf("Expected 0 commands. Actual: %v", cmds)
    22  	}
    23  }
    24  
    25  func TestPluginNodeJSBasic(t *testing.T) {
    26  	f := newWatFixture(t)
    27  	defer f.tearDown()
    28  
    29  	f.write("package.json", `
    30  {
    31    "name": "my-package",
    32    "scripts": {
    33      "test": "jest"
    34    }
    35  }`)
    36  
    37  	plugin := PluginNodeJS{}
    38  	cmds, err := plugin.run(f.ctx, f.root.Path())
    39  	if err != nil {
    40  		t.Fatal(err)
    41  	}
    42  
    43  	if len(cmds) != 1 || !strings.Contains(cmds[0].Command, "jest") {
    44  		t.Errorf("Expected 1 jest command. Actual: %v", cmds)
    45  	}
    46  }
    47  
    48  func TestPluginNodeJSMultiple(t *testing.T) {
    49  	f := newWatFixture(t)
    50  	defer f.tearDown()
    51  
    52  	// Adapted from https://github.com/react-navigation/react-navigation/blob/master/package.json
    53  	f.write("package.json", `
    54  {
    55    "name": "my-package",
    56    "scripts": {
    57      "test": "npm run lint && npm run jest",
    58      "eslint": "eslint .",
    59      "format": "eslint --fix .",
    60      "jest": "jest",
    61      "ios": "cd examples/NavigationPlayground && yarn && yarn ios"
    62    }
    63  }`)
    64  
    65  	plugin := PluginNodeJS{}
    66  	cmds, err := plugin.run(f.ctx, f.root.Path())
    67  	if err != nil {
    68  		t.Fatal(err)
    69  	}
    70  
    71  	if len(cmds) != 3 {
    72  		t.Errorf("Expected 3 commands. Actual: %v", cmds)
    73  	}
    74  
    75  	cmdStrs := make([]string, 0)
    76  	for _, cmd := range cmds {
    77  		// Strip out the part of the command that sets PATH
    78  		cmdStr := strings.Join(strings.Split(cmd.Command, " ")[1:], " ")
    79  		cmdStrs = append(cmdStrs, cmdStr)
    80  	}
    81  	sort.Strings(cmdStrs)
    82  
    83  	expected := []string{
    84  		"eslint .",
    85  		"jest",
    86  		"npm run lint && npm run jest",
    87  	}
    88  	if !reflect.DeepEqual(expected, cmdStrs) {
    89  		t.Errorf("Expected %v. Actual: %v", expected, cmdStrs)
    90  	}
    91  }