Skip to content

Commit c49de4b

Browse files
committed
Update CONTRIBUTING
1 parent a6cdcb8 commit c49de4b

File tree

2 files changed

+49
-33
lines changed

2 files changed

+49
-33
lines changed

CONTRIBUTING.md

Lines changed: 49 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,81 @@
11
# Observable Plot - Contributing
22

3-
Observable Plot is released under the ISC license. You are welcome to [fork this repository](https://docs.github.com/en/get-started/quickstart/fork-a-repo) and to [send us pull requests](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests) to contribute bug fixes or new features.
3+
Observable Plot is open source and released under the [ISC license](./LICENSE). You are welcome to [send us pull requests](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests) to contribute bug fixes or new features. We also invite you to participate in [issues](https://github.com/observablehq/plot/issues) and [discussions](https://github.com/observablehq/plot/discussions). We use issues to track and diagnose bugs, as well as to debate and design enhancements to Plot. Discussions are intended for you to ask for help using Plot, or to share something cool you’ve built with Plot. (You can also ask for help on the [Observable Forum](https://talk.observablehq.com).)
44

5-
## Set up
5+
We request that you abide by our [code of conduct](https://observablehq.com/@observablehq/code-of-conduct) when contributing and participating in discussions.
66

7-
To set up your local development environment for Plot, follow these steps on your terminal:
7+
## Development
88

9-
```bash
10-
# 1. Clone your fork of the repository
11-
git clone https://github.com/USER-NAME/plot.git
12-
cd plot
9+
To contribute to Observable Plot, you’ll need a local development environment to make and test changes to Plot’s source code. To get started, follow GitHub’s tutorial on [forking (and cloning) a repository](https://docs.github.com/en/get-started/quickstart/fork-a-repo). Once you’ve cloned your fork of the Plot repository, open a terminal and `cd` in your forked repository. Then run Yarn to install dependencies:
1310

14-
# 2. Install dependencies listed in the package.json file
11+
```bash
1512
yarn
1613
```
1714

18-
## Development
15+
## Testing
1916

20-
Plot is written in ES modules and uses [Vite](https://vitejs.dev/) for development; this means that you can edit the Plot source code, and the test plots will update live in your browser as you save changes. To start Vite:
17+
After making changes to Plot’s source code, run Plot’s test suite to verify that your code is doing what you expect and that you haven’t introduced any other unexpected changes in behavior. Plot has two types of tests: **unit tests** and **snapshot tests**. Tests are run automatically on pull requests (via GitHub Actions), but you’ll want to run them locally to verify your changes before opening a pull request. To run the tests, use Yarn:
2118

22-
```
23-
yarn dev
19+
```bash
20+
yarn test
2421
```
2522

26-
This should open http://localhost:8008/ in your browser where you can see the tests plots defined in `test/plots`.
23+
This will also run ESLint on Plot’s source to help catch simple mistakes, such as unused imports.
2724

28-
![local development environment](img/localhost.png)
25+
### Unit tests
2926

30-
If you’d like to create a new test plot as part of your development, see the instructions below for *snapshot tests*.
27+
Unit tests live in `test` and have the `-test.js` file extension; see [`test/marks/area-test.js`](./test/marks/area-test.js) for example. Generally speaking, unit tests make specific, low-level assertions about the behavior of Plot’s API, including internals and helper methods. If you add a new feature, or change the behavior of an existing feature, please update the unit tests so that we can more easily maintain your contribution into the future. For example, here’s a unit test that tests how Plot formats months:
3128

32-
## Testing
29+
```js
30+
it("formatMonth(locale, format) does the right thing", () => {
31+
assert.strictEqual(Plot.formatMonth("en", "long")(0), "January");
32+
assert.strictEqual(Plot.formatMonth("en", "short")(0), "Jan");
33+
assert.strictEqual(Plot.formatMonth("en", "narrow")(0), "J");
34+
});
35+
```
3336

34-
Plot has both unit tests and snapshot tests.
37+
Plot’s unit tests are written with [Mocha](https://mochajs.org).
3538

36-
**Unit tests** live in `test` and have the `-test.js` extension. These tests are written using [Mocha](https://mochajs.org). Generally speaking, unit tests make specific assertions about the behavior of Plot’s internals and helper methods.
39+
### Snapshot tests
3740

38-
**Snapshot tests** live in `test/plots`; these also serve as examples of how to use the Plot API. Each snapshot test defines a plot by exporting a default async function. For example, here’s a line chart using BLS unemployment data:
41+
Snapshot tests live in `test/plots`; see [`test/plots/aapl-bollinger.js`](./test/plots/aapl-bollinger.js) for example. Unlike unit tests which only test individual methods, snapshot tests actually visualize data—they’re more representative of how we expect people will use Plot. Snapshot tests can also serve as examples of how to use the Plot API, though note that some of the examples intentionally test edge case of the API and may not embody best practices. Each snapshot test defines a plot by exporting a default async function. For example, here’s a line chart using BLS unemployment data:
3942

4043
```js
4144
import * as Plot from "@observablehq/plot";
4245
import * as d3 from "d3";
4346

4447
export default async function() {
45-
const data = await d3.csv("data/bls-metro-unemployment.csv", d3.autoType);
48+
const bls = await d3.csv("data/bls-metro-unemployment.csv", d3.autoType);
4649
return Plot.plot({
4750
marks: [
48-
Plot.line(data, {x: "date", y: "unemployment", z: "division"}),
51+
Plot.line(bls, {x: "date", y: "unemployment", z: "division"}),
4952
Plot.ruleY([0])
5053
]
5154
});
5255
}
5356
```
5457

55-
When a snapshot test is run, its output is compared against the SVG or HTML snapshot saved in the `test/output` folder. This makes it easier to see the effect of code changes and to catch unintended changes.
58+
When a snapshot test is run, its output is compared against the SVG or HTML snapshot saved in the `test/output` folder. This makes it easier to review the effect of code changes and to catch unintended changes. Snapshot tests must have deterministic, reproducible behavior; they should not depend on live data, external servers, the current time, the weather, etc. To use randomness in a test, use a seeded random number generator such as [d3.randomLcg](https://github.com/d3/d3-random/blob/master/README.md#randomLcg).
5659

57-
To add a new snapshot test, create a new JavaScript file in the `test/plots` folder. Then register your test in the test registry, `test/plots/index.js`. Once you’ve registered your test, it will also appear automatically in the test browser (http://localhost:8008), where you can inspect and debug the output. (Snapshot tests must have deterministic, reproducible behavior; they should not depend on live data, external servers, the current time, the weather, etc. To use randomness in a test, use a seeded random number generator such as [d3.randomLcg](https://github.com/d3/d3-random/blob/master/README.md#randomLcg).)
58-
59-
To run the tests:
60+
To add a new snapshot test, create a new JavaScript file in the `test/plots` folder, copying the pattern shown above. Then register your test in the test registry, `test/plots/index.js`, by exporting the snapshot test function. For example:
6061

62+
```js
63+
export {default as mobyDick} from "./moby-dick.js";
6164
```
62-
yarn test
65+
66+
The best thing about snapshot tests is that you can see the live result in your browser as you make changes to Plot’s source code! This lets you immediately assess visually what Plot is doing. Once it “looks right” in a snapshot test, you can codify the expected behavior more formally in a unit test. To preview snapshot tests during development, Plot uses [Vite](https://vitejs.dev). To start Vite:
67+
68+
```bash
69+
yarn dev
6370
```
6471

65-
This will automatically generate any missing snapshots in `test/output`, which you should remember to `git add` before committing your changes. (If you forget, your PR will fail in CI, and you’ll get a reminder.) Changed snapshots are saved alongside the originals with a -changed suffix, for visual inspection. These should never be commited.
72+
This will open http://localhost:8008/ in your browser where you can choose a test plot registered in `test/plots/index.js`. As you edit the source, the currently-selected test plot will update live in your browser as you save changes. You can change the selected test from the drop-down menu. If the drop-down menu is focused, the left and right arrow keys will cycle between tests.
73+
74+
![Plot’s snapshot test live preview](img/localhost.png)
75+
76+
When previewing snapshot tests, consider using your browser’s debugger or element inspector to assist development.
6677

67-
If your code intentionally changes some of the existing snapshots, simply blow away the existing snapshots and run the tests again. You can then review what’s changed using `git diff`.
78+
Running Plot’s snapshot tests will automatically generate any missing snapshots in `test/output`. You should `git add` these before committing your changes. (If you forget, your PR will fail in CI, and you’ll get a reminder.) Changed snapshots are saved alongside the originals with a `-changed` suffix for visual inspection. If your code intentionally changes some of the existing snapshots, simply blow away the existing snapshots and run the tests again. You can then review what’s changed using `git diff`.
6879

6980
```
7081
rm -rf test/output
@@ -73,18 +84,23 @@ yarn test
7384

7485
## Documentation
7586

76-
Please update the [README.md](./README.md) to reflect any changes to Plot’s public API, and reference any related [issues](https://github.com/observablehq/plot/issues).
87+
When submitting a pull request, please remember to update Plot’s [README.md](./README.md) to reflect changes to the public API. You are also welcome to edit Plot’s [CHANGELOG.md](./CHANGELOG.md) to assist with writing future release notes. In addition, please reference any related [issues](https://github.com/observablehq/plot/issues) (or discussions) in your pull request description.
7788

78-
If you’d like to share a motivating example in an Observable notebook, you can bundle your changes into a single file:
89+
If you’d like to share a live demonstration or motivating example of your change to Plot, you can regenerate Plot’s release bundle using Yarn:
7990

8091
```bash
81-
# Compile into the file `dist/plot.umd.js`
8292
yarn prepublishOnly
8393
```
8494

85-
You can then attach the `dist/plot.umd.js` file to your notebook using a [File Attachment](https://observablehq.com/@observablehq/file-attachments), and load it into your notebook:
95+
The generated bundle `dist/plot.umd.js` can then be loaded like so:
96+
97+
```html
98+
<script src="https://cdn.jsdelivr.net/npm/d3@7"></script>
99+
<script src="plot.umd.js"></script>
100+
```
101+
102+
Alternatively, you can attach the `dist/plot.umd.js` file to an [Observable notebook](https://observablehq.com), and then load it like so:
86103

87104
```js
88-
// Load your copy into Observable
89105
Plot = require(await FileAttachment("plot.umd.js").url())
90106
```

img/localhost.png

314 KB
Loading

0 commit comments

Comments
 (0)