Skip to content

Commit b3fe143

Browse files
committed
Update require tests
1 parent 2a9af3f commit b3fe143

File tree

2 files changed

+41
-47
lines changed

2 files changed

+41
-47
lines changed

test/unit/require.spec.ts

Lines changed: 40 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,123 +2,117 @@ import * as ts from "typescript";
22
import * as util from "../util";
33

44
const requireRegex = /require\("(.*?)"\)/;
5+
const expectToRequire = (expected: string): util.TapCallback => builder => {
6+
const match = requireRegex.exec(builder.getMainLuaCodeChunk());
7+
if (util.expectToBeDefined(match)) {
8+
expect(match[1]).toBe(expected);
9+
}
10+
};
511

612
test.each([
713
{
814
filePath: "main.ts",
915
usedPath: "./folder/Module",
10-
expectedPath: "folder.Module",
16+
expected: "folder.Module",
1117
options: { rootDir: "." },
1218
throwsError: false,
1319
},
1420
{
1521
filePath: "main.ts",
1622
usedPath: "./folder/Module",
17-
expectedPath: "folder.Module",
23+
expected: "folder.Module",
1824
options: { rootDir: "./" },
1925
throwsError: false,
2026
},
2127
{
2228
filePath: "src/main.ts",
2329
usedPath: "./folder/Module",
24-
expectedPath: "src.folder.Module",
30+
expected: "src.folder.Module",
2531
options: { rootDir: "." },
2632
throwsError: false,
2733
},
2834
{
2935
filePath: "main.ts",
3036
usedPath: "folder/Module",
31-
expectedPath: "folder.Module",
37+
expected: "folder.Module",
3238
options: { rootDir: ".", baseUrl: "." },
3339
throwsError: false,
3440
},
3541
{
3642
filePath: "main.ts",
3743
usedPath: "folder/Module",
38-
expectedPath: "folder.Module",
44+
expected: "folder.Module",
3945
options: { rootDir: "./", baseUrl: "." },
4046
throwsError: false,
4147
},
4248
{
4349
filePath: "src/main.ts",
4450
usedPath: "./folder/Module",
45-
expectedPath: "folder.Module",
51+
expected: "folder.Module",
4652
options: { rootDir: "src" },
4753
throwsError: false,
4854
},
4955
{
5056
filePath: "src/main.ts",
5157
usedPath: "./folder/Module",
52-
expectedPath: "folder.Module",
58+
expected: "folder.Module",
5359
options: { rootDir: "./src" },
5460
throwsError: false,
5561
},
5662
{
5763
filePath: "main.ts",
5864
usedPath: "../Module",
59-
expectedPath: "",
65+
expected: "",
6066
options: { rootDir: "./src" },
6167
throwsError: true,
6268
},
6369
{
6470
filePath: "src/dir/main.ts",
6571
usedPath: "../Module",
66-
expectedPath: "Module",
72+
expected: "Module",
6773
options: { rootDir: "./src" },
6874
throwsError: false,
6975
},
7076
{
7177
filePath: "src/dir/dir/main.ts",
7278
usedPath: "../../dir/Module",
73-
expectedPath: "dir.Module",
79+
expected: "dir.Module",
7480
options: { rootDir: "./src" },
7581
throwsError: false,
7682
},
77-
])(
78-
"require paths root from --baseUrl or --rootDir (%p)",
79-
({ filePath, usedPath, expectedPath, options, throwsError }) => {
80-
const builder = util.testModule`
81-
import * as module from "${usedPath}";
82-
module;
83-
`;
84-
85-
builder.options(options).setMainFileName(filePath);
83+
])("require paths root from --baseUrl or --rootDir (%p)", ({ filePath, usedPath, expected, options, throwsError }) => {
84+
const builder = util.testModule`
85+
import * as module from "${usedPath}";
86+
module;
87+
`;
8688

87-
if (throwsError) {
88-
builder.expectToHaveDiagnostics();
89-
} else {
90-
const match = requireRegex.exec(builder.getMainLuaCodeChunk());
89+
builder.options(options).setMainFileName(filePath);
9190

92-
if (util.expectToBeDefined(match)) {
93-
expect(match[1]).toBe(expectedPath);
94-
}
95-
}
91+
if (throwsError) {
92+
builder.expectToHaveDiagnostics();
93+
} else {
94+
builder.tap(expectToRequire(expected));
9695
}
97-
);
96+
});
9897

99-
test.each([{ comment: "", expectedPath: "src.fake" }, { comment: "/** @noResolution */", expectedPath: "fake" }])(
98+
test.each([{ comment: "", expected: "src.fake" }, { comment: "/** @noResolution */", expected: "fake" }])(
10099
"noResolution on ambient modules causes no path alterations (%p)",
101-
({ comment, expectedPath }) => {
102-
const builder = util.testModule`
100+
({ comment, expected }) => {
101+
util.testModule`
103102
import * as fake from "fake";
104103
fake;
105-
`;
106-
107-
builder.setMainFileName("src/main.ts").addExtraFile("module.d.ts", `${comment} declare module "fake" {}`);
108-
const match = requireRegex.exec(builder.getMainLuaCodeChunk());
109-
110-
if (util.expectToBeDefined(match)) {
111-
expect(match[1]).toBe(expectedPath);
112-
}
104+
`
105+
.setMainFileName("src/main.ts")
106+
.addExtraFile("module.d.ts", `${comment} declare module "fake" {}`)
107+
.tap(expectToRequire(expected));
113108
}
114109
);
115110

116111
test("ImportEquals declaration require", () => {
117-
const input = `import foo = require("./foo/bar"); foo;`;
118-
119-
const lua = util.transpileString(input, { module: ts.ModuleKind.CommonJS });
120-
const match = requireRegex.exec(lua);
121-
if (util.expectToBeDefined(match)) {
122-
expect(match[1]).toBe("foo.bar");
123-
}
112+
util.testModule`
113+
import foo = require("./foo/bar");
114+
foo;
115+
`
116+
.options({ module: ts.ModuleKind.CommonJS })
117+
.tap(expectToRequire("foo.bar"));
124118
});

test/unit/tuples.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ test("Tuple Destruct", () => {
5454
`.expectToMatchJsResult();
5555
});
5656

57-
const expectNoUnpack: util.TapCallback = b => expect(b.getMainLuaCodeChunk()).not.toContain("unpack");
57+
const expectNoUnpack: util.TapCallback = builder => expect(builder.getMainLuaCodeChunk()).not.toContain("unpack");
5858

5959
test("Tuple Destruct Array Literal", () => {
6060
util.testFunction`

0 commit comments

Comments
 (0)