Skip to content

Commit

Permalink
Add command to run export tests with docker
Browse files Browse the repository at this point in the history
This provides a clean environment for each test, which doesnt have access to all the devDependencies we have in the repo. This simulates the experience of a user installing langchain in their own app
  • Loading branch information
nfcampos committed Apr 9, 2023
1 parent 73eb914 commit ef89125
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 18 deletions.
8 changes: 8 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,14 @@ To run only integration tests, run:
yarn test:int
```

**Environment tests** test whether LangChain works across different JS environments, including Node.js (both ESM and CJS), Edge environments (eg. Cloudflare Workers), and browsers (using Webpack).

To run the environment tests with Docker run:

```bash
yarn test:exports:docker
```

### Building

To build the project, run:
Expand Down
46 changes: 46 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
version: '3'
services:
test-exports-esm:
image: node:18
working_dir: /app
volumes:
- ./test-exports-esm:/package
- ./langchain:/langchain
- ./scripts:/scripts
command: bash /scripts/docker-ci-entrypoint.sh
test-exports-cjs:
image: node:18
working_dir: /app
volumes:
- ./test-exports-cjs:/package
- ./langchain:/langchain
- ./scripts:/scripts
command: bash /scripts/docker-ci-entrypoint.sh
test-exports-cra:
image: node:18
working_dir: /app
volumes:
- ./test-exports-cra:/package
- ./langchain:/langchain
- ./scripts:/scripts
command: bash /scripts/docker-ci-entrypoint.sh
test-exports-cf:
image: node:18
working_dir: /app
volumes:
- ./test-exports-cf:/package
- ./langchain:/langchain
- ./scripts:/scripts
command: bash /scripts/docker-ci-entrypoint.sh
success:
image: alpine:3.14
command: echo "Success"
depends_on:
test-exports-esm:
condition: service_completed_successfully
test-exports-cjs:
condition: service_completed_successfully
test-exports-cra:
condition: service_completed_successfully
test-exports-cf:
condition: service_completed_successfully
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@
"format:check": "turbo run format:check",
"lint": "turbo run lint",
"lint:fix": "yarn lint -- --fix",
"test": "turbo run test test:integration",
"test": "turbo run test test:integration && yarn test:exports:docker",
"test:unit": "turbo run test",
"test:int": "turbo run test:integration",
"test:exports:docker": "docker compose up",
"publish": "bash scripts/release-branch.sh && turbo run build lint test test:integration && yarn workspace langchain run release && echo '🔗 Open https://github.com/hwchase17/langchainjs/compare/release?expand=1 and merge the release PR'",
"example": "turbo run start --filter langchain-examples --",
"prepare": "husky install",
Expand Down
14 changes: 14 additions & 0 deletions scripts/docker-ci-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env bash

set -euxo pipefail

cp -r ../package/* .

# Replace the workspace dependency with the local copy, and install all others
yarn add ../langchain

# Check the build command completes successfully
yarn build

# Check the test command completes successfully
yarn test
3 changes: 2 additions & 1 deletion test-exports-cf/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"start": "wrangler dev",
"deploy": "wrangler publish",
"build": "wrangler publish --dry-run --outdir=dist",
"test:integration": "vitest run"
"test": "vitest run **/*.unit.test.ts",
"test:integration": "vitest run **/*.int.test.ts"
}
}
File renamed without changes.
21 changes: 21 additions & 0 deletions test-exports-cf/src/index.unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { unstable_dev } from "wrangler";
import type { UnstableDevWorker } from "wrangler";
import { describe, expect, it, beforeAll, afterAll } from "vitest";

describe("Worker", () => {
let worker: UnstableDevWorker;

beforeAll(async () => {
worker = await unstable_dev("src/index.ts", {
experimental: { disableExperimentalWarning: true },
});
});

afterAll(async () => {
await worker.stop();
});

it("should start", async () => {
expect(true).toBe(true);
});
});
6 changes: 5 additions & 1 deletion test-exports-cjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,26 @@
"description": "CJS Tests for the things exported by the langchain package",
"main": "./index.mjs",
"scripts": {
"build": "tsc",
"test": "npm run test:esm && npm run test:cjs && npm run test:cjs:import && npm run test:cjs:entrypoints && npm run test:ts",
"test:esm": "node ./index.mjs",
"test:cjs": "node ./require.js",
"test:cjs:import": "node ./import.js",
"test:cjs:entrypoints": "node ./entrypoints.js",
"test:ts": "tsc && node dist/index.js",
"test:ts": "node dist/index.js",
"format": "prettier --write \"**/*.ts\"",
"format:check": "prettier --list-different \"**/*.ts\""
},
"author": "Langchain",
"license": "MIT",
"dependencies": {
"d3-dsv": "2",
"hnswlib-node": "^1.4.2",
"langchain": "workspace:*"
},
"devDependencies": {
"@tsconfig/recommended": "^1.0.2",
"@types/node": "^18.15.11",
"prettier": "^2.8.3",
"typescript": "^4.9.5"
}
Expand Down
6 changes: 3 additions & 3 deletions test-exports-cra/src/App.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { render, screen } from '@testing-library/react';
import App from './App';
import { render, screen } from "@testing-library/react";
import App from "./App";

test('renders learn react link', () => {
test("renders learn react link", () => {
render(<App />);
const linkElement = screen.getByText(/learn react/i);
expect(linkElement).toBeInTheDocument();
Expand Down
23 changes: 11 additions & 12 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5783,6 +5783,13 @@ __metadata:
languageName: node
linkType: hard

"@types/node@npm:^18.15.11":
version: 18.15.11
resolution: "@types/node@npm:18.15.11"
checksum: 977b4ad04708897ff0eb049ecf82246d210939c82461922d20f7d2dcfd81bbc661582ba3af28869210f7e8b1934529dcd46bff7d448551400f9d48b9d3bddec3
languageName: node
linkType: hard

"@types/object-hash@npm:^3.0.2":
version: 3.0.2
resolution: "@types/object-hash@npm:3.0.2"
Expand Down Expand Up @@ -9290,7 +9297,7 @@ __metadata:
languageName: node
linkType: hard

"d3-dsv@npm:^2.0.0":
"d3-dsv@npm:2, d3-dsv@npm:^2.0.0":
version: 2.0.0
resolution: "d3-dsv@npm:2.0.0"
dependencies:
Expand Down Expand Up @@ -21709,6 +21716,9 @@ __metadata:
resolution: "test-exports-cjs@workspace:test-exports-cjs"
dependencies:
"@tsconfig/recommended": ^1.0.2
"@types/node": ^18.15.11
d3-dsv: 2
hnswlib-node: ^1.4.2
langchain: "workspace:*"
prettier: ^2.8.3
typescript: ^4.9.5
Expand All @@ -21730,17 +21740,6 @@ __metadata:
languageName: unknown
linkType: soft

"test-exports@workspace:test-exports":
version: 0.0.0-use.local
resolution: "test-exports@workspace:test-exports"
dependencies:
"@tsconfig/recommended": ^1.0.2
langchain: "workspace:*"
prettier: ^2.8.3
typescript: ^4.9.5
languageName: unknown
linkType: soft

"text-table@npm:^0.2.0":
version: 0.2.0
resolution: "text-table@npm:0.2.0"
Expand Down

0 comments on commit ef89125

Please sign in to comment.