(t *testing.T)
| 66 | } |
| 67 | |
| 68 | func TestSkipSyntaxCheck(t *testing.T) { |
| 69 | testCases := []struct { |
| 70 | name string |
| 71 | version string |
| 72 | packageJSON string |
| 73 | filePath string |
| 74 | want bool |
| 75 | }{ |
| 76 | { |
| 77 | name: "Node.js 14", |
| 78 | version: "v14.1.1", |
| 79 | packageJSON: `{"type": "module"}`, |
| 80 | filePath: "index.mjs", |
| 81 | want: false, |
| 82 | }, |
| 83 | { |
| 84 | name: "Node.js 16 with mjs", |
| 85 | version: "v16.1.1", |
| 86 | filePath: "index.mjs", |
| 87 | want: true, |
| 88 | }, |
| 89 | { |
| 90 | name: "Node.js 16 with modules", |
| 91 | version: "v16.1.1", |
| 92 | packageJSON: `{"type": "module"}`, |
| 93 | want: true, |
| 94 | }, |
| 95 | { |
| 96 | name: "Node.js 16 without ESM", |
| 97 | version: "v16.1.1", |
| 98 | want: false, |
| 99 | }, |
| 100 | } |
| 101 | |
| 102 | for _, tc := range testCases { |
| 103 | t.Run(tc.name, func(t *testing.T) { |
| 104 | defer func(fn func(*gcp.Context) (string, error)) { nodeVersion = fn }(nodeVersion) |
| 105 | nodeVersion = func(*gcp.Context) (string, error) { return tc.version, nil } |
| 106 | |
| 107 | home := t.TempDir() |
| 108 | ctx := gcp.NewContext(gcp.WithApplicationRoot(home)) |
| 109 | |
| 110 | var pjs *PackageJSON |
| 111 | if tc.packageJSON != "" { |
| 112 | if err := json.Unmarshal([]byte(tc.packageJSON), &pjs); err != nil { |
| 113 | t.Errorf("failed to unmarshal package.json: %q, err: %v", tc.packageJSON, err) |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | got, err := SkipSyntaxCheck(ctx, tc.filePath, pjs) |
| 118 | if err != nil { |
| 119 | t.Fatalf("Node.js %v: SkipSyntaxCheck(ctx, %q) got error: %v", tc.version, tc.filePath, err) |
| 120 | } |
| 121 | if got != tc.want { |
| 122 | t.Errorf("Node.js %v: SkipSyntaxCheck(ctx, %q) = %t, want %t", tc.version, tc.filePath, got, tc.want) |
| 123 | } |
| 124 | }) |
| 125 | } |
nothing calls this directly
no test coverage detected