Skip to content

Commit

Permalink
Merge pull request #20 from shoma-mano/fix
Browse files Browse the repository at this point in the history
fix:  runScript can't be used in imported flow
  • Loading branch information
shoma-mano authored Nov 11, 2024
2 parents ea43723 + 06f7aaf commit 141c390
Show file tree
Hide file tree
Showing 18 changed files with 255 additions and 173 deletions.
1 change: 0 additions & 1 deletion fixtures/foo-script.ts

This file was deleted.

29 changes: 29 additions & 0 deletions fixtures/sample-flow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { getOutput, M } from "maests";
import { openApp } from "@/fixtures/utils/openApp";
import { someScript } from "./utils/script";

// use composable flow easiliy
openApp();

// run script like this
M.runScript(someScript);

// use variables set in someScript
M.assertVisible({ id: getOutput("id") });

// use runFlow to run some flow with condition
M.runFlow({
flow: () => {
M.repeatWhileNotVisible(
{
text: "4",
},
() => {
M.tapOnText("Increment");
}
);
},
condition: {
visible: "Increment",
},
});
1 change: 1 addition & 0 deletions fixtures/utils/hello.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const hello = () => "Hello, World!";
6 changes: 6 additions & 0 deletions fixtures/utils/openApp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { M } from "maests";

export const openApp = () => {
M.initFlow({ appId: "com.my.app" });
M.launchApp({ appId: "com.my.app" });
};
22 changes: 22 additions & 0 deletions fixtures/utils/script.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { APIResult } from "./type";
import { hello } from "./hello";

export const someScript = () => {
// typed http request
const body = http.get("https://jsonplaceholder.typicode.com/todos/1").body;
const result = json<APIResult>(body);
console.log("id " + result.userId);

// you can use environment variables
console.log(`appId from env: ${process.env.APP_ID}`);

// you can use imported functions
console.log("imported file " + hello());

if (maestro.platform === "android") {
console.log("platform is android");
}

// set a variable to output to use in flow
output.id = "com.my.app:id/action_bar_root";
};
6 changes: 6 additions & 0 deletions fixtures/utils/type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export type APIResult = {
userId: number;
id: number;
title: string;
completed: boolean;
};
7 changes: 2 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
"devDependencies": {
"@types/node": "^18.14.0",
"changelogen": "^0.5.7",
"typescript": "^5.5.4",
"@types/babel__core": "^7.20.5",
"unbuild": "^2.0.0",
"vitest": "^2.1.3"
},
Expand All @@ -40,6 +38,7 @@
"keywords": [
"maestro",
"e2e",
"typescript",
"testing",
"React Native"
],
Expand All @@ -49,13 +48,11 @@
"homepage": "https://github.com/shoma-mano/maests#readme",
"license": "MIT",
"dependencies": {
"@babel/core": "^7.26.0",
"@babel/types": "^7.26.0",
"babel": "^6.23.0",
"citty": "^0.1.6",
"consola": "^3.2.3",
"dotenv": "^16.4.5",
"esbuild": "^0.24.0",
"typescript": "^5.5.4",
"get-tsconfig": "^4.8.1",
"jiti": "2.4.0",
"magicast": "^0.3.5",
Expand Down
3 changes: 3 additions & 0 deletions playground/e2e/utils/nest-script.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const nestScript = () => {
console.log("nestScript");
};
2 changes: 2 additions & 0 deletions playground/e2e/utils/openApp.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { M } from "maests";
import { nestScript } from "./nest-script";

export const openApp = () => {
M.initFlow({ appId: "com.my.app" });
M.launchApp({ appId: "com.my.app" });
M.runScript(nestScript);
};
2 changes: 1 addition & 1 deletion playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
"expo-status-bar": "~1.12.1",
"expo-system-ui": "~3.0.7",
"expo-web-browser": "~13.0.3",
"maests": "link:..",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-native": "0.74.5",
Expand All @@ -36,6 +35,7 @@
},
"devDependencies": {
"@babel/core": "^7.20.0",
"maests": "link:..",
"@types/jest": "^29.5.12",
"@types/react": "~18.2.45",
"@types/react-test-renderer": "^18.0.7",
Expand Down
6 changes: 3 additions & 3 deletions playground/pnpm-lock.yaml

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

3 changes: 2 additions & 1 deletion playground/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"**/*.tsx",
".expo/types/**/*.ts",
"expo-env.d.ts",
"test.js"
"test.js",
"e2e/sample-flow.mts"
]
}
58 changes: 3 additions & 55 deletions pnpm-lock.yaml

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

31 changes: 17 additions & 14 deletions src/commands/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ import { deleteExport } from "../rewrite-code";

export const runScript = (path: string | (() => void), funcName?: string) => {
if (typeof path === "function") return;
const scriptPath = createScriptOutPath(path);

// add command that references scriptPath
const command = `- runScript: ${scriptPath}\n`;
addOut(command);

// write script file to scriptPath
const { outputFiles } = buildSync({
entryPoints: [path],
bundle: true,
Expand All @@ -18,17 +25,13 @@ export const runScript = (path: string | (() => void), funcName?: string) => {
legalComments: "none",
write: false,
});

let code = outputFiles[0].text;
code = code.replace(/(?:\${)?process\.env\.([^\n\s}]*)}?/g, (_, p1) => {
return process.env[p1] || "";
});
code = deleteExport(code);
code += `\n${funcName ? `${funcName}();` : ""}`;
const scriptPath = createScriptOutPath(path);
writeFileWithDirectorySync(scriptPath, code);
const command = `- runScript: ${scriptPath}\n`;
addOut(command);
};

if (import.meta.vitest) {
Expand All @@ -47,19 +50,19 @@ if (import.meta.vitest) {
expect(code).toMatchInlineSnapshot(`
"// playground/e2e/utils/hello.ts
var hello = () => "Hello, World!";
// playground/e2e/utils/script.ts
var someScript = () => {
const body = http.get("https://jsonplaceholder.typicode.com/todos/1").body;
const result = json(body);
console.log("id " + result.userId);
console.log(\`appId from env: \`);
console.log("imported file " + hello());
if (maestro.platform === "android") {
console.log("platform is android");
}
output.id = "com.my.app:id/action_bar_root";
const body = http.get("https://jsonplaceholder.typicode.com/todos/1").body;
const result = json(body);
console.log("id " + result.userId);
console.log(\`appId from env: \`);
console.log("imported file " + hello());
if (maestro.platform === "android") {
console.log("platform is android");
}
output.id = "com.my.app:id/action_bar_root";
};
someScript();"
`);
unlinkSync(scriptPath);
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const main = defineCommand({
: join(cwd, args.path);

let code = fs.readFileSync(args.path, "utf-8");
code = rewriteCode({ code, yamlOutPath, fullFlowPath });
code = await rewriteCode({ yamlOutPath, fullFlowPath });

const tempFilePath = fullFlowPath.replace(".ts", ".temp.ts");
writeFileSync(tempFilePath, code);
Expand Down
Loading

0 comments on commit 141c390

Please sign in to comment.