-
Notifications
You must be signed in to change notification settings - Fork 393
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: smoke test with cjs/esm dual package (#1644)
- Loading branch information
Showing
2 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
logs | ||
.temp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
}); | ||
}); | ||
} | ||
}); |