| 1 |
import * as babel from "@babel/core"; |
| 2 |
import { join } from "path"; |
| 3 |
import { readFileSync, writeFileSync } from "fs"; |
| 4 |
|
| 5 |
function transformTest(name, cwd, file, options = {}) { |
| 6 |
it(name, () => { |
| 7 |
const inputPath = join(cwd, file); |
| 8 |
const outputPath = join(cwd, file.replace(".js", ".out.js")); |
| 9 |
|
| 10 |
const input = readFileSync(inputPath, "utf8"); |
| 11 |
let expected; |
| 12 |
try { |
| 13 |
expected = readFileSync(outputPath, "utf8"); |
| 14 |
} catch { |
| 15 |
|
| 16 |
} |
| 17 |
|
| 18 |
let { code } = babel.transformSync(input, { |
| 19 |
cwd, |
| 20 |
filename: inputPath, |
| 21 |
...options, |
| 22 |
}); |
| 23 |
|
| 24 |
code = code.replace( |
| 25 |
new RegExp(__dirname.replace(/\\/g, "/"), "g"), |
| 26 |
"<CWD>", |
| 27 |
); |
| 28 |
|
| 29 |
if (expected === undefined) { |
| 30 |
writeFileSync(outputPath, code); |
| 31 |
} else { |
| 32 |
expect(code).toBe(expected); |
| 33 |
} |
| 34 |
}); |
| 35 |
} |
| 36 |
|
| 37 |
describe("true", () => { |
| 38 |
const cwd = join(__dirname, "fixtures", "absoluteImports", "true"); |
| 39 |
|
| 40 |
transformTest("basic behavior", cwd, "main.js"); |
| 41 |
transformTest("relative to config file", cwd, "nested/main.js"); |
| 42 |
transformTest("resolved in a parent directory", cwd, "nested-2/main.js"); |
| 43 |
}); |
| 44 |
|
| 45 |
describe("string", () => { |
| 46 |
const cwd = join(__dirname, "fixtures", "absoluteImports", "string"); |
| 47 |
|
| 48 |
transformTest("basic behavior", cwd, "main.js"); |
| 49 |
}); |
| 50 |
|
| 51 |
describe("subpath", () => { |
| 52 |
const cwd = join(__dirname, "fixtures", "absoluteImports", "subpath"); |
| 53 |
|
| 54 |
transformTest("works", cwd, "main.js"); |
| 55 |
}); |