Skip to content

Commit a6dd31f

Browse files
committed
feat: More reliable way to remove temporary changes
1 parent 4748c85 commit a6dd31f

4 files changed

Lines changed: 18 additions & 14 deletions

File tree

jest.config.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module.exports = {
22
transform: {
3-
"\\.[jt]sx?$": ["esbuild-jest", { format: "esm" }],
3+
"\\.[jt]sx?$": ["esbuild-jest"],
44
},
55
moduleFileExtensions: ["ts", "js"],
66
extensionsToTreatAsEsm: ['.ts'],

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
],
1515
"scripts": {
1616
"prepack": "tsc",
17-
"test": "NODE_OPTIONS=\"--experimental-vm-modules\" jest",
17+
"test": "jest --runInBand",
1818
"semantic-release": "semantic-release -b main"
1919
},
2020
"repository": {

src/source.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ interface Target {
1212
condition?: string;
1313
}
1414

15-
const selfLocation = join(__dirname, "../../");
15+
const selfLocation = join(
16+
__dirname,
17+
__dirname.endsWith("build") ? "../../" : "./"
18+
);
1619

1720
async function materializeFunctionInFile(source: SourceFile, target: Target) {
1821
const localPath = relative(cwd(), source.getFilePath());
@@ -47,27 +50,30 @@ async function materializeFunctionInFile(source: SourceFile, target: Target) {
4750
// prettier-ignore
4851
source.addStatements(`import { materialize } from "${selfLocation}"`);
4952
// prettier-ignore
50-
source.addStatements(`materialize(${func.getName()}, ${target.condition ?? "undefined"}).then(console.log).catch((e:any) => console.error(e.message))`);
53+
source.addStatements(`materialize/*remove*/(${func.getName()}, ${target.condition ?? "undefined"}).then(console.log).catch((e:any) => console.error(e.message))`);
5154
await source.save();
55+
await source.refreshFromFileSystem();
5256

5357
let error: string | undefined = undefined;
5458
try {
5559
const result = await execAsync(`ts-node ${source.getFilePath()}`);
56-
57-
await source.refreshFromFileSystem();
58-
5960
try {
6061
const data = extractResponse(result.stdout);
6162
func.setBodyText(`/* __materialized__ */\nreturn ${data}`);
6263
} catch (e: any) {
6364
error = result.stderr.trim();
6465
}
6566
} catch (e: any) {
66-
await source.refreshFromFileSystem();
6767
error = e.message.trim();
6868
}
6969

70-
source.removeStatement(source.getStatements().length - 1);
70+
const st = source.getStatements();
71+
st.forEach((s) => {
72+
if (s.getText().startsWith("materialize/*remove*/(")) {
73+
s.remove();
74+
}
75+
});
76+
7177
source.organizeImports();
7278
await source.save();
7379
log(`${error ?? "completed"}`);

tests/fixture.spec.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ describe("test ficture", () => {
1616
});
1717

1818
test("expect both materialize1 and materialize2 to mutate", async () => {
19-
//@ts-ignore
20-
const module = await import("./fixtures/test-cases.ts");
19+
const module = require("./fixtures/test-cases.ts");
2120
expect(await module.materialize1()).not.toEqual(
2221
await module.materialize1()
2322
);
@@ -28,14 +27,13 @@ describe("test ficture", () => {
2827

2928
test("modify source", async () => {
3029
const current = await readFile("tests/fixtures/test-cases.ts");
31-
await main();
30+
await main("tests/fixtures/test-cases.ts");
3231
const replaced = await readFile("tests/fixtures/test-cases.ts");
3332
expect(current).not.toEqual(replaced);
3433
});
3534

3635
test("expect only materialize2 to mutate", async () => {
37-
//@ts-ignore
38-
const module = await import("./fixtures/test-cases.ts");
36+
const module = require("./fixtures/test-cases.ts");
3937
expect(await module.materialize1()).toEqual(await module.materialize1());
4038
expect(await module.materialize2()).not.toEqual(
4139
await module.materialize2()

0 commit comments

Comments
 (0)