TestInjectSideEffectImport exercises the bare-import form, deduplication, and per-module placement against a clausal import for the same module.
(t *testing.T)
| 140 | // TestInjectSideEffectImport exercises the bare-import form, deduplication, |
| 141 | // and per-module placement against a clausal import for the same module. |
| 142 | func TestInjectSideEffectImport(t *testing.T) { |
| 143 | t.Parallel() |
| 144 | |
| 145 | ts := loadTypescript(t) |
| 146 | ts.ApplyMutations( |
| 147 | config.InjectImport("zod", "z"), |
| 148 | config.InjectSideEffectImport("./polyfill"), |
| 149 | // Second call must dedupe. |
| 150 | config.InjectSideEffectImport("./polyfill"), |
| 151 | // Side-effect for a module that also has a clausal import. Side-effect |
| 152 | // goes before the clausal import for the same module. |
| 153 | config.InjectSideEffectImport("zod"), |
| 154 | ) |
| 155 | output, err := ts.Serialize() |
| 156 | require.NoError(t, err, "serialize") |
| 157 | |
| 158 | block := importBlock(t, output) |
| 159 | require.Contains(t, block, `import "./polyfill";`, |
| 160 | "side-effect import must emit with no clause") |
| 161 | require.Equal(t, 1, strings.Count(block, `import "./polyfill";`), |
| 162 | "side-effect imports must dedupe per module") |
| 163 | |
| 164 | sideZodIdx := strings.Index(block, `import "zod";`) |
| 165 | clausalZodIdx := strings.Index(block, `import { z } from "zod";`) |
| 166 | require.GreaterOrEqual(t, sideZodIdx, 0, "side-effect zod import must appear") |
| 167 | require.GreaterOrEqual(t, clausalZodIdx, 0, "clausal zod import must appear") |
| 168 | require.Less(t, sideZodIdx, clausalZodIdx, |
| 169 | "side-effect import must precede clausal import for the same module") |
| 170 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…