github.com/xgoffin/jenkins-library@v1.154.0/cmd/karmaExecuteTests_test.go (about)

     1  package cmd
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  
     7  	"github.com/SAP/jenkins-library/pkg/mock"
     8  
     9  	"github.com/SAP/jenkins-library/pkg/log"
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestRunKarma(t *testing.T) {
    14  	t.Run("success case", func(t *testing.T) {
    15  		opts := karmaExecuteTestsOptions{Modules: []string{"./test"}, InstallCommand: "npm install test", RunCommand: "npm run test"}
    16  
    17  		e := mock.ExecMockRunner{}
    18  		runKarma(opts, &e)
    19  
    20  		assert.Equal(t, e.Dir[0], "./test", "install command dir incorrect")
    21  		assert.Equal(t, e.Calls[0], mock.ExecCall{Exec: "npm", Params: []string{"install", "test"}}, "install command/params incorrect")
    22  
    23  		assert.Equal(t, e.Dir[1], "./test", "run command dir incorrect")
    24  		assert.Equal(t, e.Calls[1], mock.ExecCall{Exec: "npm", Params: []string{"run", "test"}}, "run command/params incorrect")
    25  
    26  	})
    27  
    28  	t.Run("error case install command", func(t *testing.T) {
    29  		var hasFailed bool
    30  		log.Entry().Logger.ExitFunc = func(int) { hasFailed = true }
    31  
    32  		opts := karmaExecuteTestsOptions{Modules: []string{"./test"}, InstallCommand: "fail install test", RunCommand: "npm run test"}
    33  
    34  		e := mock.ExecMockRunner{ShouldFailOnCommand: map[string]error{"fail install test": errors.New("error case")}}
    35  		runKarma(opts, &e)
    36  		assert.True(t, hasFailed, "expected command to exit with fatal")
    37  	})
    38  
    39  	t.Run("error case run command", func(t *testing.T) {
    40  		var hasFailed bool
    41  		log.Entry().Logger.ExitFunc = func(int) { hasFailed = true }
    42  
    43  		opts := karmaExecuteTestsOptions{Modules: []string{"./test"}, InstallCommand: "npm install test", RunCommand: "npm run test"}
    44  
    45  		e := mock.ExecMockRunner{ShouldFailOnCommand: map[string]error{"npm install test": errors.New("error case")}}
    46  		runKarma(opts, &e)
    47  		assert.True(t, hasFailed, "expected command to exit with fatal")
    48  	})
    49  }