Skip to content

Commit dc0cac1

Browse files
Filmbostock
andauthored
dataloaders tests (#110)
* support data loaders in sub directories of docs/ closes #130 * test data loaders * Update test/dataloaders-test.ts Co-authored-by: Mike Bostock <[email protected]> * ignore all .observablehq/cache --------- Co-authored-by: Mike Bostock <[email protected]>
1 parent 24ec737 commit dc0cac1

File tree

6 files changed

+37
-3
lines changed

6 files changed

+37
-3
lines changed

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
.DS_Store
2+
**/.observablehq/cache/
23
dist/
3-
docs/.observablehq/cache
44
node_modules/
5-
test/input/build/*/.observablehq/cache
65
test/output/*-changed.*
76
test/output/build/*-changed/
87
yarn-error.log

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"dev": "tsx watch ./src/preview.ts",
2626
"build": "rm -rf dist && ./bin/observable.ts build",
2727
"test": "yarn test:mocha && yarn test:lint",
28-
"test:mocha": "tsx ./node_modules/.bin/mocha 'test/**/*-test.*'",
28+
"test:mocha": "rm -rf test/.observablehq && tsx ./node_modules/.bin/mocha 'test/**/*-test.*'",
2929
"test:lint": "eslint src test public"
3030
},
3131
"dependencies": {

test/dataloaders-test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import {Loader} from "../src/dataloader.js";
2+
import {readFile} from "node:fs/promises";
3+
import assert from "node:assert";
4+
5+
describe("data loaders are called with the appropriate command", () => {
6+
it("a .js data loader is called with node", async () => {
7+
const loader = Loader.find("test", "dataloaders/data1.txt")!;
8+
const out = await loader.load();
9+
assert.strictEqual(await readFile("test/" + out, "utf-8"), "node\n");
10+
});
11+
it("a .ts data loader is called with tsx", async () => {
12+
const loader = Loader.find("test", "dataloaders/data2.txt");
13+
const out = await loader!.load();
14+
assert.strictEqual(await readFile("test/" + out, "utf-8"), "tsx\n");
15+
});
16+
it("a .sh data loader is called as a shell script", async () => {
17+
const loader = Loader.find("test", "dataloaders/data3.txt");
18+
const out = await loader!.load();
19+
assert.strictEqual(await readFile("test/" + out, "utf-8"), "shell\n");
20+
});
21+
});

test/dataloaders/data1.txt.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
function mytest() {
2+
// TODO: how to make sure this is not called by tsx? (and, does it matter?)
3+
return "node";
4+
}
5+
6+
console.log(mytest());

test/dataloaders/data2.txt.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function mytest2(): string {
2+
return "tsx";
3+
}
4+
5+
console.log(mytest2());

test/dataloaders/data3.txt.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#! /usr/bin/env bash
2+
3+
echo "shell"

0 commit comments

Comments
 (0)