Skip to content

Commit

Permalink
test: smoke test with cjs/esm dual package (#1644)
Browse files Browse the repository at this point in the history
  • Loading branch information
himself65 authored Feb 11, 2025
1 parent 7bd5d93 commit 4d94f6e
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
1 change: 1 addition & 0 deletions e2e/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
logs
.temp
86 changes: 86 additions & 0 deletions e2e/node/smoke.e2e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { execSync } from "node:child_process";
import { mkdir, rm, writeFile } from "node:fs/promises";
import { resolve } from "node:path";
import { test } from "node:test";
import { testRootDir } from "./utils.js";

await test("cjs/esm dual module check", async (t) => {
const esmImports = `import fs from 'node:fs/promises'
import { Document, MetadataMode, VectorStoreIndex } from 'llamaindex'
import { OpenAIEmbedding } from '@llamaindex/openai'
import { Settings } from '@llamaindex/core/global'`;
const cjsRequire = `const fs = require('fs').promises
const { Document, MetadataMode, VectorStoreIndex } = require('llamaindex')
const { OpenAIEmbedding } = require('@llamaindex/openai')
const { Settings } = require('@llamaindex/core/global')`;
const mainCode = `
async function main() {
Settings.embedModel = new OpenAIEmbedding({
model: 'text-embedding-3-small',
apiKey: '${process.env.OPENAI_API_KEY}',
})
const model = Settings.embedModel
if (model == null) {
process.exit(-1)
}
}
main().catch(console.error)`;
t.before(async () => {
await mkdir(resolve(testRootDir, ".temp"), {
recursive: true,
mode: 0o755,
});
});

t.after(async () => {
await rm(resolve(testRootDir, ".temp"), {
recursive: true,
force: true,
});
});

await t.test("cjs", async () => {
const cjsCode = `${cjsRequire}\n${mainCode}`;
const filePath = resolve(
testRootDir,
".temp",
`${crypto.randomUUID()}.cjs`,
);
await writeFile(filePath, cjsCode, "utf-8");

execSync(`${process.argv[0]} ${filePath}`, {
cwd: process.cwd(),
});
});

await t.test("esm", async () => {
const esmCode = `${esmImports}\n${mainCode}`;
const filePath = resolve(
testRootDir,
".temp",
`${crypto.randomUUID()}.mjs`,
);
await writeFile(filePath, esmCode, "utf-8");

execSync(`${process.argv[0]} ${filePath}`, {
cwd: process.cwd(),
});
});

const specialConditions = ["edge-light", "workerd", "react-server"];
for (const condition of specialConditions) {
await t.test(condition, async () => {
const esmCode = `${esmImports}\n${mainCode}`;
const filePath = resolve(
testRootDir,
".temp",
`${crypto.randomUUID()}.mjs`,
);
await writeFile(filePath, esmCode, "utf-8");

execSync(`${process.argv[0]} ${filePath} -C ${condition}`, {
cwd: process.cwd(),
});
});
}
});

0 comments on commit 4d94f6e

Please sign in to comment.