Skip to content

Commit

Permalink
test: add test for import
Browse files Browse the repository at this point in the history
  • Loading branch information
hal-wang committed Apr 9, 2024
1 parent be08c60 commit da121a2
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 133 deletions.
262 changes: 131 additions & 131 deletions test/build/hook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,139 +8,139 @@ import * as fs from "fs";
import path from "path";
import { expect } from "chai";

// describe("hooks", () => {
// it(`should build and invoke hooks`, async () => {
// const configFileName = `tsconfig.hooks.json`;
// let callCount = 0;
// await runin(`test/build/hook`, async () => {
// createTsconfig(undefined, undefined, configFileName);
// await new CliStartup("test", undefined, {
// tsconfigPath: configFileName,
// })
// .use(async (ctx, next) => {
// await next();

// const configService = await ctx.getService(ConfigService);
// const dict = configService.value["dict"]();

// (!!dict["beforeHook"]).should.true;
// (!!dict["afterHook"]).should.false;
// (!!dict["afterDeclarationsHook"]).should.true;
// callCount++;
// })
// .add(BuildMiddlware)
// .run();
// callCount++;
// });
// callCount.should.eq(2);
// });

// it(`should build and invoke hooks with watch`, async () => {
// const configFileName = `tsconfig.hooks-watch.json`;
// let callCount = 0;
// await runin(`test/build/hook`, async () => {
// createTsconfig(undefined, undefined, configFileName);
// await new CliStartup("test", undefined, {
// watch: true,
// tsconfigPath: configFileName,
// })
// .use(async (ctx, next) => {
// ctx.set("onWatchSuccess", () => {
// callCount++;
// });

// try {
// await next();
// } finally {
// const assetsService = await ctx.getService(AssetsService);
// await assetsService.stopWatch();

// const watchCompilerService =
// await ctx.getService(WatchCompilerService);
// watchCompilerService.stop();
// }

// const configService = await ctx.getService(ConfigService);
// const dict = configService.value["dict"]();

// (!!dict["beforeHook"]).should.true;
// (!!dict["afterHook"]).should.false;
// (!!dict["afterDeclarationsHook"]).should.true;
// callCount++;
// })
// .add(BuildMiddlware)
// .run();
// callCount++;
// });
// callCount.should.eq(3);
// });
// });
describe("hooks", () => {
it(`should build and invoke hooks`, async () => {
const configFileName = `tsconfig.hooks.json`;
let callCount = 0;
await runin(`test/build/hook`, async () => {
createTsconfig(undefined, undefined, configFileName);
await new CliStartup("test", undefined, {
tsconfigPath: configFileName,
})
.use(async (ctx, next) => {
await next();

const configService = await ctx.getService(ConfigService);
const dict = configService.value["dict"]();

(!!dict["beforeHook"]).should.true;
(!!dict["afterHook"]).should.false;
(!!dict["afterDeclarationsHook"]).should.true;
callCount++;
})
.add(BuildMiddlware)
.run();
callCount++;
});
callCount.should.eq(2);
});

it(`should build and invoke hooks with watch`, async () => {
const configFileName = `tsconfig.hooks-watch.json`;
let callCount = 0;
await runin(`test/build/hook`, async () => {
createTsconfig(undefined, undefined, configFileName);
await new CliStartup("test", undefined, {
watch: true,
tsconfigPath: configFileName,
})
.use(async (ctx, next) => {
ctx.set("onWatchSuccess", () => {
callCount++;
});

try {
await next();
} finally {
const assetsService = await ctx.getService(AssetsService);
await assetsService.stopWatch();

const watchCompilerService =
await ctx.getService(WatchCompilerService);
watchCompilerService.stop();
}

const configService = await ctx.getService(ConfigService);
const dict = configService.value["dict"]();

(!!dict["beforeHook"]).should.true;
(!!dict["afterHook"]).should.false;
(!!dict["afterDeclarationsHook"]).should.true;
callCount++;
})
.add(BuildMiddlware)
.run();
callCount++;
});
callCount.should.eq(3);
});
});

describe("build script", () => {
// it("should exec prebuild scripts", async () => {
// const cacheDir = ".cache-build-script-prebuild";
// const configFileName = `tsconfig.${cacheDir}.json`;
// let callCount = 0;
// await runin(`test/build/script`, async () => {
// createTsconfig(undefined, undefined, configFileName);
// await new CliStartup("test", undefined, {
// mode: "production",
// cacheDir: path.resolve(cacheDir),
// tsconfigPath: configFileName,
// })
// .use(async (ctx, next) => {
// await next();

// const configService = await ctx.getService(ConfigService);
// const cfg = configService.value;
// cfg["prebuild1"].should.true;
// cfg["prebuild2"].should.true;
// cfg["prebuild3"].should.true;
// cfg["postbuild1"].should.true;
// cfg["postbuild1"].should.true;
// callCount++;
// })
// .add(BuildMiddlware)
// .run();

// fs.existsSync(`./${cacheDir}`).should.true;
// fs.existsSync(`./${cacheDir}/build-test.js`).should.true;
// callCount++;
// });
// callCount.should.eq(2);
// });

// it(`should build script failed`, async () => {
// const cacheDir = ".cache-build-script-failed";
// const configFileName = `tsconfig.${cacheDir}.json`;
// let callCount = 0;
// await runin(`test/build/script`, async () => {
// createTsconfig(undefined, undefined, configFileName);
// await new CliStartup("test", undefined, {
// mode: "development",
// cacheDir: path.resolve(cacheDir),
// tsconfigPath: configFileName,
// })
// .use(async (ctx, next) => {
// await next();

// const configService = await ctx.getService(ConfigService);
// const cfg = configService.value;
// cfg["prebuild1"].should.true;
// cfg["prebuild2"].should.true;
// expect(cfg["prebuild3"]).undefined;
// expect(cfg["postbuild1"]).undefined;
// expect(cfg["postbuild1"]).undefined;
// callCount++;
// })
// .add(BuildMiddlware)
// .run();

// fs.existsSync(`./${cacheDir}`).should.false;
// callCount++;
// });
// callCount.should.eq(2);
// });
it("should exec prebuild scripts", async () => {
const cacheDir = ".cache-build-script-prebuild";
const configFileName = `tsconfig.${cacheDir}.json`;
let callCount = 0;
await runin(`test/build/script`, async () => {
createTsconfig(undefined, undefined, configFileName);
await new CliStartup("test", undefined, {
mode: "production",
cacheDir: path.resolve(cacheDir),
tsconfigPath: configFileName,
})
.use(async (ctx, next) => {
await next();

const configService = await ctx.getService(ConfigService);
const cfg = configService.value;
cfg["prebuild1"].should.true;
cfg["prebuild2"].should.true;
cfg["prebuild3"].should.true;
cfg["postbuild1"].should.true;
cfg["postbuild1"].should.true;
callCount++;
})
.add(BuildMiddlware)
.run();

fs.existsSync(`./${cacheDir}`).should.true;
fs.existsSync(`./${cacheDir}/build-test.js`).should.true;
callCount++;
});
callCount.should.eq(2);
});

it(`should build script failed`, async () => {
const cacheDir = ".cache-build-script-failed";
const configFileName = `tsconfig.${cacheDir}.json`;
let callCount = 0;
await runin(`test/build/script`, async () => {
createTsconfig(undefined, undefined, configFileName);
await new CliStartup("test", undefined, {
mode: "development",
cacheDir: path.resolve(cacheDir),
tsconfigPath: configFileName,
})
.use(async (ctx, next) => {
await next();

const configService = await ctx.getService(ConfigService);
const cfg = configService.value;
cfg["prebuild1"].should.true;
cfg["prebuild2"].should.true;
expect(cfg["prebuild3"]).undefined;
expect(cfg["postbuild1"]).undefined;
expect(cfg["postbuild1"]).undefined;
callCount++;
})
.add(BuildMiddlware)
.run();

fs.existsSync(`./${cacheDir}`).should.false;
callCount++;
});
callCount.should.eq(2);
});

it(`should exec plugin script error`, async () => {
const cacheDir = ".cache-build-plugin-script-failed";
Expand Down
31 changes: 29 additions & 2 deletions test/services/deps.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { runTest } from "./runTest";
import path from "path";
import fs from "fs";
import { FileService } from "../../src/services/file.service";
import { expect } from "chai";

runTest(DepsService, async (ctx, service) => {
const deps = service.getPackageHalspDeps("@halsp/inject");
console.log("deps1", deps);
Array.isArray(deps).should.true;
deps.length.should.greaterThan(0);

Expand All @@ -21,7 +21,6 @@ runTest(DepsService, async (ctx, service) => {

{
const deps = service["getDeps"](depPath, () => true);
console.log("deps2", deps);
Array.isArray(deps).should.true;
deps.length.should.greaterThan(0);
}
Expand Down Expand Up @@ -90,3 +89,31 @@ runTest(DepsService, async (ctx, service) => {
deps.length.should.eq(0);
});
});

runTest(DepsService, async (ctx, service) => {
await runin("../../", async () => {
const dep = await service["importDep"]("not-exist", "not-exist");
expect(dep).be.null;
});
});

runTest(DepsService, async (ctx, service) => {
const cacheDir = "./.cache-deps-import-error";
if (fs.existsSync(cacheDir)) {
fs.rmSync(cacheDir, {
force: true,
recursive: true,
});
}
fs.mkdirSync(cacheDir);

fs.writeFileSync(path.join(cacheDir, "index.js"), "throw '';");

await runin(path.resolve(cacheDir), async () => {
const dep = await service["importDep"](
path.resolve(cacheDir, "index.js"),
process.cwd(),
);
expect(dep).be.null;
});
});

0 comments on commit da121a2

Please sign in to comment.