Skip to content
Snippets Groups Projects
Unverified Commit 4d94f6e5 authored by Alex Yang's avatar Alex Yang Committed by GitHub
Browse files

test: smoke test with cjs/esm dual package (#1644)

parent 7bd5d934
Branches
Tags
No related merge requests found
logs
.temp
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% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment