jspacker - plugin_test.go
1 package jspacker
2
3 import (
4 "fmt"
5 "testing"
6
7 "vimagination.zapto.org/javascript"
8 "vimagination.zapto.org/parser"
9 )
10
11 func TestPlugin(t *testing.T) {
12 for n, test := range [...]struct {
13 Input string
14 URL string
15 Output string
16 }{
17 { // 1
18 "import a from './b.js';console.log(a)",
19 "/a.js",
20 "const a_ = await include(\"/b.js\");\n\nconsole.log(a_.default);",
21 },
22 { // 2
23 "import a from '../b.js';console.log(a)",
24 "/a/a.js",
25 "const a_ = await include(\"/b.js\");\n\nconsole.log(a_.default);",
26 },
27 { // 3
28 "import a, {b, c} from './b.js';console.log(a, b, c)",
29 "/a.js",
30 "const a_ = await include(\"/b.js\");\n\nconsole.log(a_.default, a_.b, a_.c);",
31 },
32 { // 4
33 "import * as a from './b.js';console.log(a)",
34 "/a.js",
35 "const a_ = await include(\"/b.js\");\n\nconsole.log(a_);",
36 },
37 } {
38 tks := parser.NewStringTokeniser(test.Input)
39
40 m, err := javascript.ParseModule(&tks)
41 if err != nil {
42 t.Fatalf("test %d: unexpected err: %s", n+1, err)
43 }
44
45 s, err := Plugin(m, test.URL)
46 if err != nil {
47 t.Fatalf("test %d: unexpected err: %s", n+1, err)
48 }
49
50 if output := fmt.Sprintf("%s", s); output != test.Output {
51 t.Errorf("test %d: expecting output: %q\ngot: %q", n+1, test.Output, output)
52 }
53 }
54 }
55