Skip to content

Commit

Permalink
feat(turbo-test-utils): share internal test utils (vercel#3418)
Browse files Browse the repository at this point in the history
This is an inconsequential PR, but in the `turbo-codemod` rewrite I
borrowed some test utils that I wrote for `turbo-ignore`. This revisits
that and abstracts them to a shared `turbo-test-utils` package instead
of duplicating them (we're a monorepo tool, let's practice what we
preach)
  • Loading branch information
tknickman authored Jan 23, 2023
1 parent ef5ac07 commit 3b2377a
Show file tree
Hide file tree
Showing 28 changed files with 320 additions and 165 deletions.
7 changes: 5 additions & 2 deletions packages/turbo-codemod/__tests__/add-package-manager.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { transformer } from "../src/transforms/add-package-manager";
import { setupTestFixtures } from "./test-utils";
import { setupTestFixtures } from "turbo-test-utils";
import fs from "fs-extra";
import * as getPackageManager from "../src/utils/getPackageManager";
import * as getPackageManagerVersion from "../src/utils/getPackageManagerVersion";

describe("add-package-manager", () => {
const { useFixture } = setupTestFixtures({ test: "add-package-manager" });
const { useFixture } = setupTestFixtures({
directory: __dirname,
test: "add-package-manager",
});
test("no package manager - basic", () => {
// load the fixture for the test
const { root, read } = useFixture({ fixture: "no-package-manager" });
Expand Down
7 changes: 5 additions & 2 deletions packages/turbo-codemod/__tests__/create-turbo-config.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { transformer } from "../src/transforms/create-turbo-config";
import { setupTestFixtures } from "./test-utils";
import { setupTestFixtures } from "turbo-test-utils";
import fs from "fs-extra";

describe("create-turbo-config", () => {
const { useFixture } = setupTestFixtures({ test: "create-turbo-config" });
const { useFixture } = setupTestFixtures({
directory: __dirname,
test: "create-turbo-config",
});

test("package.json config exists but no turbo.json config - basic", () => {
// load the fixture for the test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { setupTestFixtures } from "./test-utils";
import { setupTestFixtures } from "turbo-test-utils";
import getTurboUpgradeCommand from "../src/commands/migrate/steps/getTurboUpgradeCommand";
import * as utils from "../src/commands/migrate/utils";
import * as getPackageManager from "../src/utils/getPackageManager";
Expand Down Expand Up @@ -352,6 +352,7 @@ const GLOBAL_INSTALL_COMMANDS = [

describe("get-turbo-upgrade-command", () => {
const { useFixture } = setupTestFixtures({
directory: __dirname,
test: "get-turbo-upgrade-command",
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
migrateConfig,
transformer,
} from "../src/transforms/migrate-env-var-dependencies";
import { setupTestFixtures } from "./test-utils";
import { setupTestFixtures } from "turbo-test-utils";
import type { Schema } from "turbo-types";

const getTestTurboConfig = (override: Schema = { pipeline: {} }): Schema => {
Expand Down Expand Up @@ -371,6 +371,7 @@ describe("migrate-env-var-dependencies", () => {

describe("transform", () => {
const { useFixture } = setupTestFixtures({
directory: __dirname,
test: "migrate-env-var-dependencies",
});

Expand Down
7 changes: 5 additions & 2 deletions packages/turbo-codemod/__tests__/migrate.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MigrateCommandArgument } from "../src/commands";
import migrate from "../src/commands/migrate";
import { setupTestFixtures, spyExit } from "./test-utils";
import { setupTestFixtures, spyExit } from "turbo-test-utils";
import childProcess from "child_process";
import * as checkGitStatus from "../src/utils/checkGitStatus";
import * as getCurrentVersion from "../src/commands/migrate/steps/getCurrentVersion";
Expand All @@ -11,7 +11,10 @@ import * as getPackageManagerVersion from "../src/utils/getPackageManagerVersion

describe("migrate", () => {
const mockExit = spyExit();
const { useFixture } = setupTestFixtures({ test: "migrate" });
const { useFixture } = setupTestFixtures({
directory: __dirname,
test: "migrate",
});

it("migrates from 1.0.0 to 1.7.0", async () => {
const { root, readJson } = useFixture({
Expand Down
7 changes: 5 additions & 2 deletions packages/turbo-codemod/__tests__/set-default-outputs.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { transformer } from "../src/transforms/set-default-outputs";
import { setupTestFixtures } from "./test-utils";
import { setupTestFixtures } from "turbo-test-utils";

describe("set-default-outputs", () => {
const { useFixture } = setupTestFixtures({ test: "set-default-outputs" });
const { useFixture } = setupTestFixtures({
directory: __dirname,
test: "set-default-outputs",
});
it("migrates turbo.json outputs - basic", async () => {
// load the fixture for the test
const { root, read } = useFixture({
Expand Down
7 changes: 5 additions & 2 deletions packages/turbo-codemod/__tests__/transform.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import transform from "../src/commands/transform";
import { MigrateCommandArgument } from "../src/commands";
import { setupTestFixtures, spyExit } from "./test-utils";
import { setupTestFixtures, spyExit } from "turbo-test-utils";
import * as checkGitStatus from "../src/utils/checkGitStatus";
import * as getPackageManager from "../src/utils/getPackageManager";
import * as getPackageManagerVersion from "../src/utils/getPackageManagerVersion";

describe("transform", () => {
const mockExit = spyExit();
const { useFixture } = setupTestFixtures({ test: "transform" });
const { useFixture } = setupTestFixtures({
directory: __dirname,
test: "transform",
});

it("runs the selected transform", async () => {
const { root, readJson } = useFixture({
Expand Down
12 changes: 10 additions & 2 deletions packages/turbo-codemod/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ module.exports = {
testEnvironment: "node",
transformIgnorePatterns: ["/node_modules/(?!(ansi-regex)/)"],
modulePathIgnorePatterns: ["<rootDir>/node_modules", "<rootDir>/dist"],
testPathIgnorePatterns: ["/__fixtures__/", "/__tests__/test-utils.ts"],
coveragePathIgnorePatterns: ["/__fixtures__/", "/__tests__/test-utils.ts"],
testPathIgnorePatterns: ["/__fixtures__/"],
coveragePathIgnorePatterns: ["/__fixtures__/"],
collectCoverage: true,
coverageThreshold: {
global: {
branches: 80,
functions: 89,
lines: 89,
statements: 89,
},
},
};
4 changes: 2 additions & 2 deletions packages/turbo-codemod/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@
"ts-jest": "^27.1.1",
"tsconfig": "workspace:*",
"tsup": "^5.10.3",
"turbo-test-utils": "workspace:*",
"turbo-types": "workspace:*",
"typescript": "^4.5.5",
"uuid": "^9.0.0"
"typescript": "^4.5.5"
},
"files": [
"dist"
Expand Down
2 changes: 1 addition & 1 deletion packages/turbo-ignore/__tests__/args.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import parseArgs, { help } from "../src/args";
import pkg from "../package.json";
import { spyConsole, spyExit } from "./test-utils";
import { spyConsole, spyExit } from "turbo-test-utils";

describe("parseArgs()", () => {
const mockConsole = spyConsole();
Expand Down
4 changes: 2 additions & 2 deletions packages/turbo-ignore/__tests__/checkCommit.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import child_process, { ChildProcess, ExecException } from "child_process";
import child_process from "child_process";
import { checkCommit } from "../src/checkCommit";
import { mockEnv } from "./test-utils";
import { mockEnv } from "turbo-test-utils";

describe("checkCommit()", () => {
describe("on Vercel", () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/turbo-ignore/__tests__/getComparison.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getComparison } from "../src/getComparison";
import { spyConsole, validateLogs, mockEnv } from "./test-utils";
import { spyConsole, mockEnv } from "turbo-test-utils";

describe("getComparison()", () => {
mockEnv();
Expand Down
8 changes: 5 additions & 3 deletions packages/turbo-ignore/__tests__/getTask.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { getTask } from "../src/getTask";
import { spyConsole, validateLogs } from "./test-utils";
import { spyConsole, validateLogs } from "turbo-test-utils";

describe("getWorkspace()", () => {
const mockConsole = spyConsole();
it("getTask defaults to build", async () => {
expect(getTask({})).toEqual("build");
validateLogs(
['using "build" as the task as it was unspecified'],
mockConsole.log
mockConsole.log,
{ prefix: "≫ " }
);
});

Expand All @@ -19,7 +20,8 @@ describe("getWorkspace()", () => {
).toEqual(`"workspace#task"`);
validateLogs(
['using "workspace#task" as the task from the arguments'],
mockConsole.log
mockConsole.log,
{ prefix: "≫ " }
);
});
});
5 changes: 3 additions & 2 deletions packages/turbo-ignore/__tests__/getWorkspace.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getWorkspace } from "../src/getWorkspace";
import { spyConsole, validateLogs } from "./test-utils";
import { spyConsole, validateLogs } from "turbo-test-utils";

describe("getWorkspace()", () => {
const mockConsole = spyConsole();
Expand All @@ -11,7 +11,8 @@ describe("getWorkspace()", () => {
).toEqual("test-workspace");
validateLogs(
['using "test-workspace" as workspace from arguments'],
mockConsole.log
mockConsole.log,
{ prefix: "≫ " }
);
});

Expand Down
55 changes: 38 additions & 17 deletions packages/turbo-ignore/__tests__/ignore.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import child_process, { ChildProcess, ExecException } from "child_process";
import turboIgnore from "../src/ignore";
import { spyExit, spyConsole, mockEnv, validateLogs } from "./test-utils";
import type { SpyExit } from "./test-utils";
import {
spyConsole,
spyExit,
SpyExit,
mockEnv,
validateLogs,
} from "turbo-test-utils";

function expectBuild(mockExit: SpyExit) {
expect(mockExit.exit).toHaveBeenCalledWith(1);
Expand Down Expand Up @@ -40,7 +45,9 @@ describe("turboIgnore()", () => {
expect.anything()
);

validateLogs(["UNKNOWN_ERROR: error"], mockConsole.error);
validateLogs(["UNKNOWN_ERROR: error"], mockConsole.error, {
prefix: "≫ ",
});

expectBuild(mockExit);
mockExec.mockRestore();
Expand Down Expand Up @@ -77,7 +84,8 @@ describe("turboIgnore()", () => {
[
`turbo-ignore could not complete - no package manager detected, please commit a lockfile, or set "packageManager" in your root "package.json"`,
],
mockConsole.warn
mockConsole.warn,
{ prefix: "≫ " }
);

expectBuild(mockExit);
Expand Down Expand Up @@ -118,7 +126,8 @@ describe("turboIgnore()", () => {
[
`turbo-ignore could not complete - commit does not exist or is unreachable`,
],
mockConsole.warn
mockConsole.warn,
{ prefix: "≫ " }
);

expectBuild(mockExit);
Expand Down Expand Up @@ -156,7 +165,8 @@ describe("turboIgnore()", () => {
[
`turbo-ignore could not complete - parent commit does not exist or is unreachable`,
],
mockConsole.warn
mockConsole.warn,
{ prefix: "≫ " }
);

expectBuild(mockExit);
Expand All @@ -178,7 +188,8 @@ describe("turboIgnore()", () => {
),
],
],
mockConsole.error
mockConsole.error,
{ prefix: "≫ " }
);
expectBuild(mockExit);
});
Expand All @@ -196,7 +207,8 @@ describe("turboIgnore()", () => {
expect.stringContaining(' is missing the "name" field (required).'),
],
],
mockConsole.error
mockConsole.error,
{ prefix: "≫ " }
);
expectBuild(mockExit);
});
Expand Down Expand Up @@ -274,7 +286,8 @@ describe("turboIgnore()", () => {
"this project and its dependencies are not affected",
() => expect.stringContaining("⬜️ ignoring the change"),
],
mockConsole.log
mockConsole.log,
{ prefix: "≫ " }
);

expectIgnore(mockExit);
Expand Down Expand Up @@ -313,7 +326,8 @@ describe("turboIgnore()", () => {
'this commit affects "test-app"',
() => expect.stringContaining("✅ proceeding with deployment"),
],
mockConsole.log
mockConsole.log,
{ prefix: "≫ " }
);

expectBuild(mockExit);
Expand Down Expand Up @@ -351,7 +365,8 @@ describe("turboIgnore()", () => {
'this commit affects "test-app" and 1 dependency (ui)',
() => expect.stringContaining("✅ proceeding with deployment"),
],
mockConsole.log
mockConsole.log,
{ prefix: "≫ " }
);

expectBuild(mockExit);
Expand Down Expand Up @@ -389,7 +404,8 @@ describe("turboIgnore()", () => {
'this commit affects "test-app" and 2 dependencies (ui, tsconfig)',
() => expect.stringContaining("✅ proceeding with deployment"),
],
mockConsole.log
mockConsole.log,
{ prefix: "≫ " }
);

expectBuild(mockExit);
Expand Down Expand Up @@ -421,7 +437,8 @@ describe("turboIgnore()", () => {
[
"failed to parse JSON output from `npx turbo run build --filter=test-app...[HEAD^] --dry=json`.",
],
mockConsole.error
mockConsole.error,
{ prefix: "≫ " }
);

expectBuild(mockExit);
Expand Down Expand Up @@ -457,7 +474,8 @@ describe("turboIgnore()", () => {
[
"failed to parse JSON output from `npx turbo run build --filter=test-app...[HEAD^] --dry=json`.",
],
mockConsole.error
mockConsole.error,
{ prefix: "≫ " }
);

expectBuild(mockExit);
Expand All @@ -482,7 +500,8 @@ describe("turboIgnore()", () => {
"found commit message: [vercel skip]",
() => expect.stringContaining("⬜️ ignoring the change"),
],
mockConsole.log
mockConsole.log,
{ prefix: "≫ " }
);

expectIgnore(mockExit);
Expand All @@ -506,7 +525,8 @@ describe("turboIgnore()", () => {
"found commit message: [vercel deploy]",
() => expect.stringContaining("✅ proceeding with deployment"),
],
mockConsole.log
mockConsole.log,
{ prefix: "≫ " }
);

expectBuild(mockExit);
Expand Down Expand Up @@ -548,7 +568,8 @@ describe("turboIgnore()", () => {
"this project and its dependencies are not affected",
() => expect.stringContaining("⬜️ ignoring the change"),
],
mockConsole.log
mockConsole.log,
{ prefix: "≫ " }
);

expectIgnore(mockExit);
Expand Down
Loading

0 comments on commit 3b2377a

Please sign in to comment.