Skip to content

dataloaders tests #110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
.DS_Store
**/.observablehq/cache/
dist/
docs/.observablehq/cache
node_modules/
test/input/build/*/.observablehq/cache
test/output/*-changed.*
test/output/build/*-changed/
yarn-error.log
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"dev": "tsx watch ./src/preview.ts",
"build": "rm -rf dist && ./bin/observable.ts build",
"test": "yarn test:mocha && yarn test:lint",
"test:mocha": "tsx ./node_modules/.bin/mocha 'test/**/*-test.*'",
"test:mocha": "rm -rf test/.observablehq && tsx ./node_modules/.bin/mocha 'test/**/*-test.*'",
"test:lint": "eslint src test public"
},
"dependencies": {
Expand Down
21 changes: 21 additions & 0 deletions test/dataloaders-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {Loader} from "../src/dataloader.js";
import {readFile} from "node:fs/promises";
import assert from "node:assert";

describe("data loaders are called with the appropriate command", () => {
it("a .js data loader is called with node", async () => {
const loader = Loader.find("test", "dataloaders/data1.txt")!;
const out = await loader.load();
assert.strictEqual(await readFile("test/" + out, "utf-8"), "node\n");
});
it("a .ts data loader is called with tsx", async () => {
const loader = Loader.find("test", "dataloaders/data2.txt");
const out = await loader!.load();
assert.strictEqual(await readFile("test/" + out, "utf-8"), "tsx\n");
});
it("a .sh data loader is called as a shell script", async () => {
const loader = Loader.find("test", "dataloaders/data3.txt");
const out = await loader!.load();
assert.strictEqual(await readFile("test/" + out, "utf-8"), "shell\n");
});
});
6 changes: 6 additions & 0 deletions test/dataloaders/data1.txt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function mytest() {
// TODO: how to make sure this is not called by tsx? (and, does it matter?)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you wanted to test this, you could test the error case: if you put TypeScript annotations in a .js file, you should get a syntax error. It’d probably be a good thing to test how runLoader handles errors anyway!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried that but couldn't make it work, because tsx sends the syntax error anyway since the file extension is js.

(Note that STDERR is passed to the parent (for logging), with the "inherit" option.)

test:

 it("a .js data loader is _not_ called with tsx", async () => {
    const outputPath = ".observablehq/data4.txt";
    await runLoader("test/dataloaders/data4-error.txt.js", outputPath);
    try {
      assert.strictEqual(await readFile(outputPath, "utf-8"), "");
    } catch (error) {
      assert.strictEqual(error.code, "ENOENT");
    } finally {
      unlink(outputPath);
    }
});

test/dataloaders/data4-error.txt.js

/* eslint-disable */
function mytest2(): string {
  return "tsx";
}

console.log(mytest2());

return "node";
}

console.log(mytest());
5 changes: 5 additions & 0 deletions test/dataloaders/data2.txt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function mytest2(): string {
return "tsx";
}

console.log(mytest2());
3 changes: 3 additions & 0 deletions test/dataloaders/data3.txt.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#! /usr/bin/env bash

echo "shell"