Skip to content

Commit

Permalink
test(test): tests\n\nmore tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbinoGeek committed Jun 11, 2024
1 parent 11c773a commit 2ffe4a6
Showing 1 changed file with 50 additions and 11 deletions.
61 changes: 50 additions & 11 deletions pkg/ts/TranspileProject_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package ts

import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"testing"

. "github.com/smartystreets/goconvey/convey"
Expand Down Expand Up @@ -31,23 +33,60 @@ func TestTranspileProject(t *testing.T) {
So(errors.Is(err, ErrNoEntrypoint), ShouldBeTrue)
})

Convey("given valid sources", func() {
// remove banner/footer for testing
JS_BANNER = ""
JS_FOOTER = ""

dstFile := filepath.Join(tmpSrc, "test.js")
Convey("should transpile a single file", func() {
// create a test source file
So(os.WriteFile(filepath.Join(tmpSrc, "test.ts"),
[]byte("console.log('hello world')"), 0777), ShouldBeNil)
So(TranspileProject(tmpSrc, dstFile), ShouldBeNil)

_, err := os.Stat(dstFile)
So(err, ShouldBeNil)
defer os.Remove(dstFile)

// check contents of
contents, err := os.ReadFile(dstFile)
So(err, ShouldBeNil)
So(string(contents), ShouldResemble, fmt.Sprintf("// %s/test.ts\nconsole.log(\"hello world\");\n", strings.Replace(tmpSrc, "\\", "/", -1)))
})

Convey("should transpile multiple files", func() {
So(os.WriteFile(filepath.Join(tmpSrc, "index.ts"),
[]byte("import { fn } from './fnTest'\nfn()"), 0777), ShouldBeNil)
So(os.WriteFile(filepath.Join(tmpSrc, "fnTest.ts"),
[]byte("export function fn() { console.log('hello world') }"), 0777), ShouldBeNil)

So(TranspileProject(tmpSrc, dstFile), ShouldBeNil)

_, err := os.Stat(dstFile)
So(err, ShouldBeNil)
defer os.Remove(dstFile)

// check contents of dstFile
contents, err := os.ReadFile(dstFile)
So(err, ShouldBeNil)
So(string(contents), ShouldResemble, fmt.Sprintf("// %s/fnTest.ts\nfunction fn() {\n console.log(\"hello world\");\n}\n\n// %s/index.ts\nfn();\n", strings.Replace(tmpSrc, "\\", "/", -1), strings.Replace(tmpSrc, "\\", "/", -1)))
})

dstFile := filepath.Join(tmpSrc, "test.js")
Convey("should transpile a single file", func() {
So(TranspileProject(tmpSrc, dstFile), ShouldBeNil)
Convey("should transpile with debug", func() {
So(os.WriteFile(filepath.Join(tmpSrc, "debug.ts"),
[]byte("const debuglogging = false;\nconsole.log(debuglogging);"), 0777), ShouldBeNil)
So(TranspileProject(tmpSrc, dstFile, WithEntrypoints(
filepath.Join(tmpSrc, "debug.ts"),
), WithDebugMode(true)), ShouldBeNil)

_, err := os.Stat(dstFile)
So(err, ShouldBeNil)
_, err := os.Stat(dstFile)
So(err, ShouldBeNil)
defer os.Remove(dstFile)

// check contents of
contents, err := os.ReadFile(dstFile)
So(err, ShouldBeNil)
So(string(contents), ShouldEqual, "console.log('hello world')")
})
// check contents of dstFile
contents, err := os.ReadFile(dstFile)
So(err, ShouldBeNil)
So(string(contents), ShouldResemble, fmt.Sprintf("// %s/debug.ts\nvar debuglogging = true;\nconsole.log(debuglogging);\n", strings.Replace(tmpSrc, "\\", "/", -1)))
})
})
}

0 comments on commit 2ffe4a6

Please sign in to comment.