aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/packages/turbo-codemod/__tests__/set-default-outputs.test.ts
diff options
context:
space:
mode:
author简律纯 <hsiangnianian@outlook.com>2023-04-28 01:36:44 +0800
committer简律纯 <hsiangnianian@outlook.com>2023-04-28 01:36:44 +0800
commitdd84b9d64fb98746a230cd24233ff50a562c39c9 (patch)
treeb583261ef00b3afe72ec4d6dacb31e57779a6faf /packages/turbo-codemod/__tests__/set-default-outputs.test.ts
parent0b46fcd72ac34382387b2bcf9095233efbcc52f4 (diff)
downloadHydroRoll-dd84b9d64fb98746a230cd24233ff50a562c39c9.tar.gz
HydroRoll-dd84b9d64fb98746a230cd24233ff50a562c39c9.zip
Diffstat (limited to 'packages/turbo-codemod/__tests__/set-default-outputs.test.ts')
-rw-r--r--packages/turbo-codemod/__tests__/set-default-outputs.test.ts391
1 files changed, 391 insertions, 0 deletions
diff --git a/packages/turbo-codemod/__tests__/set-default-outputs.test.ts b/packages/turbo-codemod/__tests__/set-default-outputs.test.ts
new file mode 100644
index 0000000..4a71fa7
--- /dev/null
+++ b/packages/turbo-codemod/__tests__/set-default-outputs.test.ts
@@ -0,0 +1,391 @@
+import { transformer } from "../src/transforms/set-default-outputs";
+import { setupTestFixtures } from "@turbo/test-utils";
+
+describe("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({
+ fixture: "old-outputs",
+ });
+
+ // run the transformer
+ const result = transformer({
+ root,
+ options: { force: false, dry: false, print: false },
+ });
+
+ expect(JSON.parse(read("turbo.json") || "{}")).toStrictEqual({
+ $schema: "https://turbo.build/schema.json",
+ pipeline: {
+ "build-one": {
+ outputs: ["foo"],
+ },
+ "build-two": {},
+ "build-three": {
+ outputs: ["dist/**", "build/**"],
+ },
+ },
+ });
+
+ expect(result.fatalError).toBeUndefined();
+ expect(result.changes).toMatchInlineSnapshot(`
+ Object {
+ "turbo.json": Object {
+ "action": "modified",
+ "additions": 2,
+ "deletions": 1,
+ },
+ }
+ `);
+ });
+
+ it("migrates turbo.json outputs - workspace configs", async () => {
+ // load the fixture for the test
+ const { root, readJson } = useFixture({
+ fixture: "workspace-configs",
+ });
+
+ // run the transformer
+ const result = transformer({
+ root,
+ options: { force: false, dry: false, print: false },
+ });
+
+ expect(readJson("turbo.json") || "{}").toStrictEqual({
+ $schema: "https://turbo.build/schema.json",
+ pipeline: {
+ "build-one": {
+ outputs: ["foo"],
+ },
+ "build-two": {},
+ "build-three": {
+ outputs: ["dist/**", "build/**"],
+ },
+ },
+ });
+
+ expect(readJson("apps/docs/turbo.json") || "{}").toStrictEqual({
+ $schema: "https://turbo.build/schema.json",
+ extends: ["//"],
+ pipeline: {
+ build: {
+ outputs: ["dist/**", "build/**"],
+ },
+ },
+ });
+
+ expect(readJson("apps/web/turbo.json") || "{}").toStrictEqual({
+ $schema: "https://turbo.build/schema.json",
+ extends: ["//"],
+ pipeline: {
+ build: {},
+ },
+ });
+
+ expect(readJson("packages/ui/turbo.json") || "{}").toStrictEqual({
+ $schema: "https://turbo.build/schema.json",
+ extends: ["//"],
+ pipeline: {
+ "build-three": {
+ outputs: ["dist/**", "build/**"],
+ },
+ },
+ });
+
+ expect(result.fatalError).toBeUndefined();
+ expect(result.changes).toMatchInlineSnapshot(`
+ Object {
+ "apps/docs/turbo.json": Object {
+ "action": "modified",
+ "additions": 1,
+ "deletions": 1,
+ },
+ "apps/web/turbo.json": Object {
+ "action": "modified",
+ "additions": 1,
+ "deletions": 0,
+ },
+ "packages/ui/turbo.json": Object {
+ "action": "modified",
+ "additions": 1,
+ "deletions": 1,
+ },
+ "turbo.json": Object {
+ "action": "modified",
+ "additions": 2,
+ "deletions": 1,
+ },
+ }
+ `);
+ });
+
+ it("migrates turbo.json outputs - dry", async () => {
+ // load the fixture for the test
+ const { root, read } = useFixture({
+ fixture: "old-outputs",
+ });
+
+ const turboJson = JSON.parse(read("turbo.json") || "{}");
+
+ // run the transformer
+ const result = transformer({
+ root,
+ options: { force: false, dry: true, print: false },
+ });
+
+ // make sure it didn't change
+ expect(JSON.parse(read("turbo.json") || "{}")).toEqual(turboJson);
+
+ expect(result.fatalError).toBeUndefined();
+ expect(result.changes).toMatchInlineSnapshot(`
+ Object {
+ "turbo.json": Object {
+ "action": "skipped",
+ "additions": 2,
+ "deletions": 1,
+ },
+ }
+ `);
+ });
+
+ it("migrates turbo.json outputs - print", async () => {
+ // load the fixture for the test
+ const { root, read } = useFixture({
+ fixture: "old-outputs",
+ });
+
+ // run the transformer
+ const result = transformer({
+ root,
+ options: { force: false, dry: false, print: true },
+ });
+
+ expect(JSON.parse(read("turbo.json") || "{}")).toStrictEqual({
+ $schema: "https://turbo.build/schema.json",
+ pipeline: {
+ "build-one": {
+ outputs: ["foo"],
+ },
+ "build-two": {},
+ "build-three": {
+ outputs: ["dist/**", "build/**"],
+ },
+ },
+ });
+
+ expect(result.fatalError).toBeUndefined();
+ expect(result.changes).toMatchInlineSnapshot(`
+ Object {
+ "turbo.json": Object {
+ "action": "modified",
+ "additions": 2,
+ "deletions": 1,
+ },
+ }
+ `);
+ });
+
+ it("migrates turbo.json outputs - dry & print", async () => {
+ // load the fixture for the test
+ const { root, read } = useFixture({
+ fixture: "old-outputs",
+ });
+
+ const turboJson = JSON.parse(read("turbo.json") || "{}");
+
+ // run the transformer
+ const result = transformer({
+ root,
+ options: { force: false, dry: true, print: false },
+ });
+
+ // make sure it didn't change
+ expect(JSON.parse(read("turbo.json") || "{}")).toEqual(turboJson);
+
+ expect(result.fatalError).toBeUndefined();
+ expect(result.changes).toMatchInlineSnapshot(`
+ Object {
+ "turbo.json": Object {
+ "action": "skipped",
+ "additions": 2,
+ "deletions": 1,
+ },
+ }
+ `);
+ });
+
+ it("migrates turbo.json outputs - invalid", async () => {
+ // load the fixture for the test
+ const { root, read } = useFixture({
+ fixture: "invalid-outputs",
+ });
+
+ // run the transformer
+ const result = transformer({
+ root,
+ options: { force: false, dry: false, print: false },
+ });
+
+ expect(JSON.parse(read("turbo.json") || "{}")).toStrictEqual({
+ $schema: "https://turbo.build/schema.json",
+ pipeline: {
+ "build-one": {
+ outputs: ["foo"],
+ },
+ "build-two": {},
+ "build-three": {
+ outputs: ["dist/**", "build/**"],
+ },
+ "garbage-in-numeric-0": {
+ outputs: ["dist/**", "build/**"],
+ },
+ "garbage-in-numeric": {
+ outputs: 42,
+ },
+ "garbage-in-string": {
+ outputs: "string",
+ },
+ "garbage-in-empty-string": {
+ outputs: ["dist/**", "build/**"],
+ },
+ "garbage-in-null": {
+ outputs: ["dist/**", "build/**"],
+ },
+ "garbage-in-false": {
+ outputs: ["dist/**", "build/**"],
+ },
+ "garbage-in-true": {
+ outputs: true,
+ },
+ "garbage-in-object": {
+ outputs: {},
+ },
+ },
+ });
+
+ expect(result.fatalError).toBeUndefined();
+ expect(result.changes).toMatchInlineSnapshot(`
+ Object {
+ "turbo.json": Object {
+ "action": "modified",
+ "additions": 6,
+ "deletions": 5,
+ },
+ }
+ `);
+ });
+
+ it("migrates turbo.json outputs - config with no pipeline", async () => {
+ // load the fixture for the test
+ const { root, read } = useFixture({
+ fixture: "no-pipeline",
+ });
+
+ // run the transformer
+ const result = transformer({
+ root,
+ options: { force: false, dry: false, print: false },
+ });
+
+ expect(JSON.parse(read("turbo.json") || "{}")).toStrictEqual({
+ $schema: "https://turbo.build/schema.json",
+ globalDependencies: ["$NEXT_PUBLIC_API_KEY", "$STRIPE_API_KEY", ".env"],
+ pipeline: {},
+ });
+
+ expect(result.fatalError).toBeUndefined();
+ expect(result.changes).toMatchInlineSnapshot(`
+ Object {
+ "turbo.json": Object {
+ "action": "unchanged",
+ "additions": 0,
+ "deletions": 0,
+ },
+ }
+ `);
+ });
+
+ it("migrates turbo.json outputs - config with no outputs", async () => {
+ // load the fixture for the test
+ const { root, read } = useFixture({
+ fixture: "no-outputs",
+ });
+
+ // run the transformer
+ const result = transformer({
+ root,
+ options: { force: false, dry: false, print: false },
+ });
+
+ expect(JSON.parse(read("turbo.json") || "{}")).toStrictEqual({
+ $schema: "https://turbo.build/schema.json",
+ pipeline: {
+ "build-one": {
+ dependsOn: ["build-two"],
+ outputs: ["dist/**", "build/**"],
+ },
+ "build-two": {
+ cache: false,
+ },
+ "build-three": {
+ persistent: true,
+ outputs: ["dist/**", "build/**"],
+ },
+ },
+ });
+
+ expect(result.fatalError).toBeUndefined();
+ expect(result.changes).toMatchInlineSnapshot(`
+ Object {
+ "turbo.json": Object {
+ "action": "modified",
+ "additions": 2,
+ "deletions": 0,
+ },
+ }
+ `);
+ });
+
+ it("errors if no turbo.json can be found", async () => {
+ // load the fixture for the test
+ const { root, read } = useFixture({
+ fixture: "no-turbo-json",
+ });
+
+ expect(read("turbo.json")).toBeUndefined();
+
+ // run the transformer
+ const result = transformer({
+ root,
+ options: { force: false, dry: false, print: false },
+ });
+
+ expect(read("turbo.json")).toBeUndefined();
+ expect(result.fatalError).toBeDefined();
+ expect(result.fatalError?.message).toMatch(
+ /No turbo\.json found at .*?\. Is the path correct\?/
+ );
+ });
+
+ it("errors if package.json config exists and has not been migrated", async () => {
+ // load the fixture for the test
+ const { root } = useFixture({
+ fixture: "old-config",
+ });
+
+ // run the transformer
+ const result = transformer({
+ root,
+ options: { force: false, dry: false, print: false },
+ });
+
+ expect(result.fatalError).toBeDefined();
+ expect(result.fatalError?.message).toMatch(
+ 'turbo" key detected in package.json. Run `npx @turbo/codemod transform create-turbo-config` first'
+ );
+ });
+});