Skip to content

Commit

Permalink
chore(@turbo/repository): convert tests to node test runner (#7985)
Browse files Browse the repository at this point in the history
Running these tests with Jest takes ~15s, with test runner it takes
~400ms. The test execution itself is the same, so I think the extra time
for Jest is because of transpiling via Babel? Maybe there's an import
that is slow or something that is irrelevant with `node --import`.
  • Loading branch information
mehulkar committed Apr 16, 2024
1 parent 10eab3d commit cba8f49
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 76 deletions.
1 change: 1 addition & 0 deletions .github/workflows/turborepo-native-lib-test.yml
Expand Up @@ -48,6 +48,7 @@ jobs:
with:
windows: ${{ matrix.os.name == 'windows' }}
github-token: "${{ secrets.GITHUB_TOKEN }}"
node-version: "20"

- name: Install Global Turbo
uses: ./.github/actions/install-global-turbo
Expand Down
15 changes: 8 additions & 7 deletions packages/turbo-repository/__tests__/affected-packages.test.ts
@@ -1,3 +1,5 @@
import { describe, test } from "node:test";
import { strict as assert } from "node:assert";
import * as path from "node:path";
import { Workspace, Package, PackageManager } from "../js/dist/index.js";

Expand Down Expand Up @@ -40,12 +42,11 @@ describe("affectedPackages", () => {
},
];

test.each(tests)(
"$description",
async (testParams: AffectedPackagesTestParams) => {
const { files, expected } = testParams;
for (const { description, files, expected } of tests) {
test(description, async () => {
const dir = path.resolve(__dirname, "./fixtures/monorepo");
const workspace = await Workspace.find(dir);

const reduced: PackageReduced[] = (
await workspace.affectedPackages(files)
).map((pkg) => {
Expand All @@ -55,7 +56,7 @@ describe("affectedPackages", () => {
};
});

expect(reduced).toEqual(expected);
}
);
assert.deepEqual(reduced, expected);
});
}
});
14 changes: 8 additions & 6 deletions packages/turbo-repository/__tests__/find-packages.test.ts
@@ -1,27 +1,29 @@
import { describe, it } from "node:test";
import { strict as assert } from "node:assert";
import * as path from "node:path";
import { Workspace, Package } from "../js/dist/index.js";

describe("findPackages", () => {
it("enumerates packages", async () => {
const workspace = await Workspace.find("./fixtures/monorepo");
const packages: Package[] = await workspace.findPackages();
expect(packages.length).not.toBe(0);
assert.notEqual(packages.length, 0);
});

it("returns a package graph", async () => {
const dir = path.resolve(__dirname, "./fixtures/monorepo");
const workspace = await Workspace.find(dir);
const packages = await workspace.findPackagesWithGraph();

expect(Object.keys(packages).length).toBe(2);
assert.equal(Object.keys(packages).length, 2);

const pkg1 = packages["apps/app"];
const pkg2 = packages["packages/ui"];

expect(pkg1.dependencies).toEqual(["packages/ui"]);
expect(pkg1.dependents).toEqual([]);
assert.deepEqual(pkg1.dependencies, ["packages/ui"]);
assert.deepEqual(pkg1.dependents, []);

expect(pkg2.dependencies).toEqual([]);
expect(pkg2.dependents).toEqual(["apps/app"]);
assert.deepEqual(pkg2.dependencies, []);
assert.deepEqual(pkg2.dependents, ["apps/app"]);
});
});
6 changes: 4 additions & 2 deletions packages/turbo-repository/__tests__/workspace.test.ts
@@ -1,16 +1,18 @@
import { describe, it } from "node:test";
import { strict as assert } from "node:assert";
import * as path from "node:path";
import { Workspace, PackageManager } from "../js/dist/index.js";

describe("Workspace", () => {
it("finds a workspace", async () => {
const workspace = await Workspace.find();
const expectedRoot = path.resolve(__dirname, "../../..");
expect(workspace.absolutePath).toBe(expectedRoot);
assert.equal(workspace.absolutePath, expectedRoot);
});

it("finds a package manager", async () => {
const workspace = await Workspace.find();
const packageManager: PackageManager = workspace.packageManager;
expect(packageManager.name).toBe("pnpm");
assert.equal(packageManager.name, "pnpm");
});
});
12 changes: 0 additions & 12 deletions packages/turbo-repository/jest.config.js

This file was deleted.

5 changes: 2 additions & 3 deletions packages/turbo-repository/package.json
Expand Up @@ -8,7 +8,7 @@
"build": "napi build --platform -p turborepo-napi --cargo-cwd ../../ --cargo-name turborepo_napi native --js false --dts ../js/index.d.ts && mkdir -p js/dist && cp js/index.js js/dist/index.js && cp js/index.d.ts js/dist/index.d.ts",
"build:release": "napi build --release --platform -p turborepo-napi --cargo-cwd ../../ --cargo-name turborepo_napi native --js false",
"package": "node scripts/publish.mjs",
"test": "jest"
"test": "node --import tsx --test __tests__/*.test.ts"
},
"keywords": [],
"author": "",
Expand All @@ -17,8 +17,7 @@
"@napi-rs/cli": "^2.16.3",
"execa": "^8.0.1",
"fs-extra": "^11.1.1",
"jest": "^27.4.3",
"ts-jest": "^27.1.1"
"tsx": "^4.7.2"
},
"main": "dist/index.js",
"napi": {
Expand Down
49 changes: 3 additions & 46 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cba8f49

Please sign in to comment.