Skip to content

Commit b8b6b92

Browse files
committed
Refactor babel plugin tests
1 parent 62900f2 commit b8b6b92

File tree

1 file changed

+148
-98
lines changed

1 file changed

+148
-98
lines changed
Lines changed: 148 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,115 +1,165 @@
11
import assert from "node:assert/strict";
2-
import { describe, it } from "node:test";
2+
import { describe, it, TestContext } from "node:test";
33
import path from "node:path";
44

55
import { transformFileSync } from "@babel/core";
66

7-
import { plugin } from "./plugin.js";
7+
import { plugin, PluginOptions } from "./plugin.js";
88
import { setupTempDirectory } from "../test-utils.js";
9-
import { getLibraryName } from "../path-utils.js";
9+
10+
type TestTransformationOptions = {
11+
files: Record<string, string>;
12+
inputFilePath: string;
13+
assertion: (code: string) => void;
14+
options?: PluginOptions;
15+
};
16+
17+
function itTransforms(
18+
title: string,
19+
{ files, inputFilePath, assertion, options = {} }: TestTransformationOptions
20+
) {
21+
it(`transforms ${title}`, (context: TestContext) => {
22+
const tempDirectoryPath = setupTempDirectory(context, files);
23+
const result = transformFileSync(
24+
path.join(tempDirectoryPath, inputFilePath),
25+
{ plugins: [[plugin, options]] }
26+
);
27+
assert(result, "Expected transformation to produce a result");
28+
const { code } = result;
29+
assert(code, "Expected transformation to produce code");
30+
assert(assertion(code), `Unexpected code: ${code}`);
31+
});
32+
}
1033

1134
describe("plugin", () => {
12-
it("transforms require calls, regardless", (context) => {
13-
const tempDirectoryPath = setupTempDirectory(context, {
14-
"package.json": `{ "name": "my-package" }`,
15-
"addon-1.apple.node/addon-1.node":
16-
"// This is supposed to be a binary file",
17-
"addon-2.apple.node/addon-2.node":
18-
"// This is supposed to be a binary file",
19-
"addon-1.js": `
20-
const addon = require('./addon-1.node');
21-
console.log(addon);
22-
`,
23-
"addon-2.js": `
24-
const addon = require('./addon-2.node');
25-
console.log(addon);
26-
`,
27-
"sub-directory/addon-1.js": `
28-
const addon = require('../addon-1.node');
29-
console.log(addon);
30-
`,
31-
"addon-1-bindings.js": `
32-
const addon = require('bindings')('addon-1');
33-
console.log(addon);
34-
`,
35-
"require-js-file.js": `
36-
const addon = require('./addon-1.js');
37-
console.log(addon);
38-
`,
35+
describe("transforming require(...) calls", () => {
36+
itTransforms("a simple call", {
37+
files: {
38+
"package.json": `{ "name": "my-package" }`,
39+
"my-addon.apple.node/my-addon.node":
40+
"// This is supposed to be a binary file",
41+
"index.js": `
42+
const addon = require('./my-addon.node');
43+
console.log(addon);
44+
`,
45+
},
46+
inputFilePath: "index.js",
47+
assertion: (code) =>
48+
code.includes(`requireNodeAddon("my-package--my-addon")`),
3949
});
4050

41-
const ADDON_1_REQUIRE_ARG = getLibraryName(
42-
path.join(tempDirectoryPath, "addon-1"),
43-
{ stripPathSuffix: false }
44-
);
45-
const ADDON_2_REQUIRE_ARG = getLibraryName(
46-
path.join(tempDirectoryPath, "addon-2"),
47-
{ stripPathSuffix: false }
48-
);
51+
itTransforms("from sub-directory", {
52+
files: {
53+
"package.json": `{ "name": "my-package" }`,
54+
"my-addon.apple.node/my-addon.node":
55+
"// This is supposed to be a binary file",
56+
"sub-dir/index.js": `
57+
const addon = require('../my-addon.node');
58+
console.log(addon);
59+
`,
60+
},
61+
inputFilePath: "sub-dir/index.js",
62+
assertion: (code) =>
63+
code.includes(`requireNodeAddon("my-package--my-addon")`),
64+
});
4965

50-
{
51-
const result = transformFileSync(
52-
path.join(tempDirectoryPath, "./addon-1.js"),
53-
{ plugins: [[plugin, { stripPathSuffix: false }]] }
54-
);
55-
assert(result);
56-
const { code } = result;
57-
assert(
58-
code && code.includes(`requireNodeAddon("${ADDON_1_REQUIRE_ARG}")`),
59-
`Unexpected code: ${code}`
60-
);
61-
}
66+
itTransforms("addon in sub-directory", {
67+
files: {
68+
"package.json": `{ "name": "my-package" }`,
69+
"sub-dir/my-addon.apple.node/my-addon.node":
70+
"// This is supposed to be a binary file",
71+
"index.js": `
72+
const addon = require('./sub-dir/my-addon.node');
73+
console.log(addon);
74+
`,
75+
},
76+
inputFilePath: "index.js",
77+
assertion: (code) =>
78+
code.includes(`requireNodeAddon("my-package--sub-dir-my-addon")`),
79+
});
6280

63-
{
64-
const result = transformFileSync(
65-
path.join(tempDirectoryPath, "./addon-2.js"),
66-
{ plugins: [[plugin, { naming: "hash" }]] }
67-
);
68-
assert(result);
69-
const { code } = result;
70-
assert(
71-
code && code.includes(`requireNodeAddon("${ADDON_2_REQUIRE_ARG}")`),
72-
`Unexpected code: ${code}`
73-
);
74-
}
81+
itTransforms(
82+
"and returns package name when passed stripPathSuffix option",
83+
{
84+
files: {
85+
"package.json": `{ "name": "my-package" }`,
86+
"sub-dir/my-addon.apple.node/my-addon.node":
87+
"// This is supposed to be a binary file",
88+
"index.js": `
89+
const addon = require('./sub-dir/my-addon.node');
90+
console.log(addon);
91+
`,
92+
},
93+
inputFilePath: "index.js",
94+
options: { stripPathSuffix: true },
95+
assertion: (code) => code.includes(`requireNodeAddon("my-package")`),
96+
}
97+
);
7598

76-
{
77-
const result = transformFileSync(
78-
path.join(tempDirectoryPath, "./sub-directory/addon-1.js"),
79-
{ plugins: [[plugin, { naming: "hash" }]] }
80-
);
81-
assert(result);
82-
const { code } = result;
83-
assert(
84-
code && code.includes(`requireNodeAddon("${ADDON_1_REQUIRE_ARG}")`),
85-
`Unexpected code: ${code}`
86-
);
87-
}
99+
itTransforms("and does not touch required JS files", {
100+
files: {
101+
"package.json": `{ "name": "my-package" }`,
102+
// TODO: Add a ./my-addon.node to make this test complete
103+
"my-addon.js": "// Some JS file",
104+
"index.js": `
105+
const addon = require('./my-addon');
106+
console.log(addon);
107+
`,
108+
},
109+
inputFilePath: "index.js",
110+
options: { stripPathSuffix: true },
111+
assertion: (code) => !code.includes("requireNodeAddon"),
112+
});
113+
});
88114

89-
{
90-
const result = transformFileSync(
91-
path.join(tempDirectoryPath, "./addon-1-bindings.js"),
92-
{ plugins: [[plugin, { naming: "hash" }]] }
93-
);
94-
assert(result);
95-
const { code } = result;
96-
assert(
97-
code && code.includes(`requireNodeAddon("${ADDON_1_REQUIRE_ARG}")`),
98-
`Unexpected code: ${code}`
99-
);
100-
}
115+
describe("transforming require('binding')(...) calls", () => {
116+
itTransforms("a simple call", {
117+
files: {
118+
"package.json": `{ "name": "my-package" }`,
119+
"my-addon.apple.node/my-addon.node":
120+
"// This is supposed to be a binary file",
121+
"index.js": `
122+
const addon = require('bindings')('my-addon');
123+
console.log(addon);
124+
`,
125+
},
126+
inputFilePath: "index.js",
127+
assertion: (code) =>
128+
code.includes(`requireNodeAddon("my-package--my-addon")`),
129+
});
130+
131+
describe("in 'build/Release'", () => {
132+
itTransforms("a nested addon", {
133+
files: {
134+
"package.json": `{ "name": "my-package" }`,
135+
"build/Release/my-addon.apple.node/my-addon.node":
136+
"// This is supposed to be a binary file",
137+
"index.js": `
138+
const addon = require('bindings')('my-addon');
139+
console.log(addon);
140+
`,
141+
},
142+
inputFilePath: "index.js",
143+
assertion: (code) =>
144+
code.includes(
145+
`requireNodeAddon("my-package--build-Release-my-addon")`
146+
),
147+
});
101148

102-
{
103-
const result = transformFileSync(
104-
path.join(tempDirectoryPath, "./require-js-file.js"),
105-
{ plugins: [[plugin, { naming: "hash" }]] }
106-
);
107-
assert(result);
108-
const { code } = result;
109-
assert(
110-
code && !code.includes(`requireNodeAddon`),
111-
`Unexpected code: ${code}`
112-
);
113-
}
149+
itTransforms("strips path suffix when passing stripPathSuffix option", {
150+
files: {
151+
"package.json": `{ "name": "my-package" }`,
152+
"build/Release/my-addon.apple.node/my-addon.node":
153+
"// This is supposed to be a binary file",
154+
"index.js": `
155+
const addon = require('bindings')('my-addon');
156+
console.log(addon);
157+
`,
158+
},
159+
inputFilePath: "index.js",
160+
options: { stripPathSuffix: true },
161+
assertion: (code) => code.includes(`requireNodeAddon("my-package")`),
162+
});
163+
});
114164
});
115165
});

0 commit comments

Comments
 (0)