Skip to content

Commit b88c3d0

Browse files
committed
Modernize test/bench runners
1 parent d0f7ad3 commit b88c3d0

File tree

10 files changed

+166
-150
lines changed

10 files changed

+166
-150
lines changed

.monorepolint.config.mjs

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ const JS_PACKAGES = []; // projects that use javascript/rollup to build
1717
const MAIN_PACKAGE = "@turf/turf";
1818

1919
const TAPE_PACKAGES = []; // projects that have tape tests
20+
const NODE_TEST_PACKAGES = []; // projects that use node's native test runner
2021
const TYPES_PACKAGES = []; // projects that have types tests
21-
const TSTYCHE_PACKAGES = []; // projects that use tstyche for type tests.
22-
const BENCH_PACKAGES = []; // projects that have benchmarks
22+
const TSTYCHE_PACKAGES = []; // projects that use tstyche for type tests
2323

2424
// iterate all the packages and figure out what buckets everything falls into
2525
const packagesPath = path.join(process.cwd(), "packages");
@@ -38,8 +38,16 @@ for (const pk of await fs.readdir(packagesPath)) {
3838
JS_PACKAGES.push(name);
3939
}
4040

41-
if (existsSync(path.join(pk, "test.js"))) {
42-
TAPE_PACKAGES.push(name);
41+
if (existsSync(path.join(packagesPath, pk, "test.ts"))) {
42+
const testFileContents = await fs.readFile(
43+
path.join(packagesPath, pk, "test.ts"),
44+
"utf-8"
45+
);
46+
if (testFileContents.includes(`from "tape"`)) {
47+
TAPE_PACKAGES.push(name);
48+
} else {
49+
NODE_TEST_PACKAGES.push(name);
50+
}
4351
}
4452

4553
if (existsSync(path.join(packagesPath, pk, "types.ts"))) {
@@ -51,13 +59,6 @@ for (const pk of await fs.readdir(packagesPath)) {
5159
}
5260
}
5361

54-
const TS_TAPE_PACKAGES = TAPE_PACKAGES.filter(
55-
(pkg) => -1 !== TS_PACKAGES.indexOf(pkg)
56-
);
57-
const JS_TAPE_PACKAGES = TAPE_PACKAGES.filter(
58-
(pkg) => -1 !== JS_PACKAGES.indexOf(pkg)
59-
);
60-
6162
export default {
6263
rules: [
6364
packageOrder({
@@ -180,7 +181,6 @@ export default {
180181
packageScript({
181182
options: {
182183
scripts: {
183-
docs: REMOVE,
184184
test: "pnpm run /test:.*/",
185185
},
186186
},
@@ -211,9 +211,21 @@ export default {
211211
scripts: {
212212
bench: "tsx bench.ts",
213213
"test:tape": "tsx test.ts",
214+
"test:node": REMOVE,
215+
},
216+
},
217+
includePackages: TAPE_PACKAGES,
218+
}),
219+
220+
packageScript({
221+
options: {
222+
scripts: {
223+
bench: "node bench.ts",
224+
"test:node": "node --test",
225+
"test:tape": REMOVE,
214226
},
215227
},
216-
includePackages: [...TS_TAPE_PACKAGES, ...JS_TAPE_PACKAGES],
228+
includePackages: NODE_TEST_PACKAGES,
217229
}),
218230

219231
packageScript({
@@ -238,14 +250,48 @@ export default {
238250
requireDependency({
239251
options: {
240252
devDependencies: {
241-
benchmark: "^2.1.4",
242-
glob: REMOVE,
243-
tape: "^5.9.0",
244253
tsup: "^8.4.0",
245254
tsx: "^4.19.4",
246255
},
247256
},
248257
includePackages: [...TS_PACKAGES, ...JS_PACKAGES],
258+
excludePackages: NODE_TEST_PACKAGES,
259+
}),
260+
261+
requireDependency({
262+
options: {
263+
devDependencies: {
264+
tape: "^5.9.0",
265+
"@types/tape": "^5.8.1",
266+
},
267+
},
268+
includePackages: TAPE_PACKAGES,
269+
}),
270+
271+
requireDependency({
272+
options: {
273+
devDependencies: {
274+
"@types/benchmark": REMOVE,
275+
"@types/tape": REMOVE,
276+
benchmark: REMOVE,
277+
"load-json-file": REMOVE,
278+
tape: REMOVE,
279+
tsx: REMOVE,
280+
"write-json-file": REMOVE,
281+
},
282+
},
283+
includePackages: NODE_TEST_PACKAGES,
284+
}),
285+
286+
requireDependency({
287+
options: {
288+
devDependencies: {
289+
"@types/benchmark": "^2.1.5",
290+
benchmark: "^2.1.4",
291+
},
292+
},
293+
includePackages: TS_PACKAGES,
294+
excludePackages: NODE_TEST_PACKAGES,
249295
}),
250296

251297
requireDependency({
@@ -254,8 +300,6 @@ export default {
254300
tslib: "^2.8.1",
255301
},
256302
devDependencies: {
257-
"@types/benchmark": "^2.1.5",
258-
"@types/tape": "^5.8.1",
259303
typescript: "^5.8.3",
260304
},
261305
},

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@
3636
"@monorepolint/config": "0.6.0-alpha.6",
3737
"@monorepolint/core": "0.6.0-alpha.6",
3838
"@monorepolint/rules": "0.6.0-alpha.6",
39+
"@types/benchmark": "^2.1.5",
3940
"@types/node": "22.15.3",
4041
"acorn": "^8.14.1",
42+
"benchmark": "^2.1.4",
4143
"camelcase": "^8.0.0",
4244
"d3-queue": "*",
4345
"decamelize": "^6.0.0",

packages/turf-center/bench.ts

Lines changed: 12 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,12 @@
1-
import path from "path";
2-
import { fileURLToPath } from "url";
3-
import { glob } from "glob";
4-
import { loadJsonFileSync } from "load-json-file";
5-
import Benchmark from "benchmark";
6-
import { center } from "./index.js";
7-
8-
const __dirname = path.dirname(fileURLToPath(import.meta.url));
9-
10-
const fixtures = glob
11-
.sync(path.join(__dirname, "test", "in", "*.geojson"))
12-
.map((input) => {
13-
return {
14-
name: path.parse(input).name,
15-
geojson: loadJsonFileSync(input),
16-
};
17-
});
18-
19-
/**
20-
* Single Process Benchmark
21-
*
22-
* feature-collection: 0.445ms
23-
* imbalanced-polygon: 0.051ms
24-
* linestring: 0.027ms
25-
* point: 0.011ms
26-
* polygon: 0.013ms
27-
*/
28-
for (const { name, geojson } of fixtures) {
29-
console.time(name);
30-
center(geojson);
31-
console.timeEnd(name);
32-
}
33-
34-
/**
35-
* Benchmark Results
36-
*
37-
* feature-collection x 2,786,700 ops/sec ±1.50% (83 runs sampled)
38-
* imbalanced-polygon x 1,364,145 ops/sec ±3.33% (76 runs sampled)
39-
* linestring x 4,104,106 ops/sec ±4.16% (81 runs sampled)
40-
* point x 4,901,692 ops/sec ±5.23% (81 runs sampled)
41-
* polygon x 2,862,759 ops/sec ±1.14% (86 runs sampled)
42-
*/
43-
const suite = new Benchmark.Suite("turf-center");
44-
for (const { name, geojson } of fixtures) {
45-
suite.add(name, () => center(geojson));
46-
}
47-
48-
suite.on("cycle", (e) => console.log(String(e.target))).run();
1+
import { center } from "./index.ts";
2+
import { benchFixtures } from "../../support/benchFixtures.mts";
3+
4+
// Benchmark Results
5+
// feature-collection.geojson x 27,241,658 ops/sec ±0.34% (99 runs sampled)
6+
// imbalanced-polygon.geojson x 14,679,583 ops/sec ±0.27% (98 runs sampled)
7+
// linestring.geojson x 34,199,495 ops/sec ±0.52% (95 runs sampled)
8+
// point.geojson x 52,230,993 ops/sec ±0.70% (96 runs sampled)
9+
// points-with-weights.geojson x 24,802,237 ops/sec ±0.33% (100 runs sampled)
10+
// polygon-without-weights.geojson x 18,423,881 ops/sec ±0.28% (100 runs sampled)
11+
// polygon.geojson x 24,990,920 ops/sec ±0.45% (99 runs sampled)
12+
await benchFixtures("turf-center", (input) => center(input));

packages/turf-center/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { BBox, Feature, GeoJsonProperties, Point } from "geojson";
1+
import type { BBox, Feature, GeoJsonProperties, Point } from "geojson";
22
import { bbox } from "@turf/bbox";
3-
import { point, Id, AllGeoJSON } from "@turf/helpers";
3+
import { point, type Id, type AllGeoJSON } from "@turf/helpers";
44

55
/**
66
* Takes a {@link Feature} or {@link FeatureCollection} and returns the absolute center point of all features.

packages/turf-center/package.json

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,24 +46,17 @@
4646
"dist"
4747
],
4848
"scripts": {
49-
"bench": "tsx bench.ts",
49+
"bench": "node bench.ts",
5050
"build": "tsup --config ../../tsup.config.ts",
5151
"test": "pnpm run /test:.*/",
52-
"test:tape": "tsx test.ts",
52+
"test:node": "node --test",
5353
"test:types": "tsc --esModuleInterop --module node16 --moduleResolution node16 --noEmit --strict types.ts"
5454
},
5555
"devDependencies": {
5656
"@turf/bbox-polygon": "workspace:*",
5757
"@turf/meta": "workspace:*",
58-
"@types/benchmark": "^2.1.5",
59-
"@types/tape": "^5.8.1",
60-
"benchmark": "^2.1.4",
61-
"load-json-file": "^7.0.1",
62-
"tape": "^5.9.0",
6358
"tsup": "^8.4.0",
64-
"tsx": "^4.19.4",
65-
"typescript": "^5.8.3",
66-
"write-json-file": "^6.0.0"
59+
"typescript": "^5.8.3"
6760
},
6861
"dependencies": {
6962
"@turf/bbox": "workspace:*",

packages/turf-center/test.ts

Lines changed: 36 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,47 @@
1-
import test from "tape";
2-
import { glob } from "glob";
3-
import path from "path";
4-
import { fileURLToPath } from "url";
5-
import { loadJsonFileSync } from "load-json-file";
6-
import { writeJsonFileSync } from "write-json-file";
7-
import { bboxPolygon } from "@turf/bbox-polygon";
8-
import { bbox } from "@turf/bbox";
9-
import { featureEach, coordEach } from "@turf/meta";
10-
import { lineString, featureCollection } from "@turf/helpers";
11-
import { center } from "./index.js";
1+
import test from "node:test";
2+
import center from "./index.ts";
3+
import { featureCollection, lineString } from "@turf/helpers";
4+
import { coordEach, featureEach } from "@turf/meta";
5+
import bboxPolygon from "@turf/bbox-polygon";
6+
import bbox from "@turf/bbox";
7+
import type { Geometry } from "geojson";
8+
import { testFixtures } from "../../support/testFixtures.mts";
9+
import assert from "assert";
1210

13-
const __dirname = path.dirname(fileURLToPath(import.meta.url));
11+
await test("center fixtures", async (t) => {
12+
await testFixtures(t, (geojson) => {
13+
const options = geojson.options || {};
14+
options.properties = { "marker-symbol": "star", "marker-color": "#F00" };
15+
const centered = center(geojson, options);
1416

15-
test("turf-center", (t) => {
16-
glob
17-
.sync(path.join(__dirname, "test", "in", "*.geojson"))
18-
.forEach((filepath) => {
19-
const geojson = loadJsonFileSync(filepath);
20-
const options = geojson.options || {};
21-
options.properties = { "marker-symbol": "star", "marker-color": "#F00" };
22-
const centered = center(geojson, options);
17+
// Display Results
18+
const results = featureCollection<Geometry>([centered]);
19+
featureEach(geojson, (feature) => results.features.push(feature));
20+
const extent = bboxPolygon(bbox(geojson));
21+
extent.properties = {
22+
stroke: "#00F",
23+
"stroke-width": 1,
24+
"fill-opacity": 0,
25+
};
26+
coordEach(extent, (coord) =>
27+
results.features.push(
28+
lineString([coord, centered.geometry.coordinates], {
29+
stroke: "#00F",
30+
"stroke-width": 1,
31+
})
32+
)
33+
);
34+
results.features.push(extent);
2335

24-
// Display Results
25-
const results = featureCollection([centered]);
26-
featureEach(geojson, (feature) => results.features.push(feature));
27-
const extent = bboxPolygon(bbox(geojson));
28-
extent.properties = {
29-
stroke: "#00F",
30-
"stroke-width": 1,
31-
"fill-opacity": 0,
32-
};
33-
coordEach(extent, (coord) =>
34-
results.features.push(
35-
lineString([coord, centered.geometry.coordinates], {
36-
stroke: "#00F",
37-
"stroke-width": 1,
38-
})
39-
)
40-
);
41-
results.features.push(extent);
42-
43-
const out = filepath.replace(
44-
path.join("test", "in"),
45-
path.join("test", "out")
46-
);
47-
if (process.env.REGEN) writeJsonFileSync(out, results);
48-
t.deepEqual(results, loadJsonFileSync(out), path.parse(filepath).name);
49-
});
50-
t.end();
36+
return results;
37+
});
5138
});
5239

53-
test("turf-center -- properties", (t) => {
40+
test("turf-center -- properties", () => {
5441
const line = lineString([
5542
[0, 0],
5643
[1, 1],
5744
]);
5845
const pt = center(line, { properties: { foo: "bar" } });
59-
t.equal(pt.properties.foo, "bar", "translate properties");
60-
t.end();
46+
assert.strictEqual(pt.properties.foo, "bar", "translate properties");
6147
});

pnpm-lock.yaml

Lines changed: 6 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)