@empiricalrun/test-run 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @empiricalrun/test-run
2
2
 
3
+ ## 0.1.2
4
+
5
+ ### Patch Changes
6
+
7
+ - 177ad09: fix: test-run fails due to a syntax error
8
+
9
+ ## 0.1.1
10
+
11
+ ### Patch Changes
12
+
13
+ - cb2ec31: fix: test-run marking failed tests as pass
14
+
3
15
  ## 0.1.0
4
16
 
5
17
  ### Minor Changes
package/dist/bin/index.js CHANGED
@@ -31,9 +31,18 @@ const __1 = require("..");
31
31
  console.error("forbid-only option is not supported");
32
32
  process.exit(1);
33
33
  }
34
- await (0, __1.runTest)({
35
- name: options.name,
36
- dir: options.dir || "tests",
37
- pwOptions: pwOptions.join(" "),
38
- });
34
+ try {
35
+ const { hasTestPassed } = await (0, __1.runTest)({
36
+ name: options.name,
37
+ dir: options.dir || "tests",
38
+ pwOptions: pwOptions.join(" "),
39
+ });
40
+ if (!hasTestPassed) {
41
+ process.exit(1);
42
+ }
43
+ }
44
+ catch (error) {
45
+ console.error("Error while running playwright test:", error.message);
46
+ process.exit(1);
47
+ }
39
48
  })();
package/dist/index.d.ts CHANGED
@@ -4,5 +4,7 @@ import { TestRunParameters } from "./types";
4
4
  * @export
5
5
  * @param {TestRunParameters} { name, dir, pwOptions }
6
6
  */
7
- export declare function runTest({ name, dir, pwOptions }: TestRunParameters): Promise<void>;
7
+ export declare function runTest({ name, dir, pwOptions, }: TestRunParameters): Promise<{
8
+ hasTestPassed: boolean;
9
+ }>;
8
10
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAC;AAW5C;;;;GAIG;AACH,wBAAsB,OAAO,CAAC,EAAE,IAAI,EAAE,GAAG,EAAE,SAAS,EAAE,EAAE,iBAAiB,iBAoDxE"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAC;AAW5C;;;;GAIG;AACH,wBAAsB,OAAO,CAAC,EAC5B,IAAI,EACJ,GAAG,EACH,SAAS,GACV,EAAE,iBAAiB,GAAG,OAAO,CAAC;IAC7B,aAAa,EAAE,OAAO,CAAC;CACxB,CAAC,CAoDD"}
package/dist/index.js CHANGED
@@ -11,8 +11,7 @@ const utils_1 = require("./utils");
11
11
  * @export
12
12
  * @param {TestRunParameters} { name, dir, pwOptions }
13
13
  */
14
- async function runTest({ name, dir, pwOptions }) {
15
- console.log("getting all files in directory:", dir);
14
+ async function runTest({ name, dir, pwOptions, }) {
16
15
  const files = await (0, utils_1.getAllFilePaths)(dir);
17
16
  let matchingFilePath = "";
18
17
  // find the first file that contains the test block
@@ -25,23 +24,29 @@ async function runTest({ name, dir, pwOptions }) {
25
24
  }
26
25
  }
27
26
  if (!matchingFilePath) {
28
- console.error("No test block found for the given test name");
29
- return;
27
+ const message = `No test block found for the given test name: ${name}`;
28
+ throw Error(message);
30
29
  }
31
- const testNode = await (0, utils_1.getTestCaseNode)({
30
+ const { testCaseNode, sourceFile } = await (0, utils_1.getTestCaseNode)({
32
31
  filePath: matchingFilePath,
33
32
  scenarioName: name,
34
33
  });
35
- const parentDescribe = (0, utils_1.findFirstSerialDescribeBlock)(testNode);
34
+ const parentDescribe = (0, utils_1.findFirstSerialDescribeBlock)(testCaseNode);
36
35
  const isFileMarkedSerial = await (0, utils_1.hasTopLevelDescribeConfigureWithSerialMode)(matchingFilePath);
37
- console.log("Identified test block:", !!testNode);
36
+ console.log("Identified test block:", !!testCaseNode);
38
37
  console.log("Is parent describe block marked serial:", !!parentDescribe);
39
38
  console.log("Is file marked serial:", isFileMarkedSerial);
40
39
  const currentFileContent = await fs_extra_1.default.readFile(matchingFilePath, "utf-8");
41
40
  // if the file is not marked serial, we need to mark the test or describe block as only
42
- if (!isFileMarkedSerial) {
43
- await (0, utils_1.markTestAsOnly)(matchingFilePath, testNode.getText(), parentDescribe?.getText() || "");
41
+ if (!isFileMarkedSerial && testCaseNode) {
42
+ await (0, utils_1.markTestAsOnly)({
43
+ sourceFile,
44
+ parentDescribeNode: parentDescribe,
45
+ testCaseNode: testCaseNode,
46
+ filePath: matchingFilePath,
47
+ });
44
48
  }
49
+ let hasTestPassed = true;
45
50
  try {
46
51
  const env = Object({ ...process.env });
47
52
  const pwRunCmd = `npx playwright test ${matchingFilePath} ${pwOptions}`;
@@ -51,13 +56,12 @@ async function runTest({ name, dir, pwOptions }) {
51
56
  });
52
57
  }
53
58
  catch (e) {
54
- console.error("Error executing playwright test", e);
59
+ hasTestPassed = false;
55
60
  }
56
61
  // revert the changes made to the file to mark tests as only
57
62
  await fs_extra_1.default.writeFile(matchingFilePath, currentFileContent);
58
- if (!matchingFilePath) {
59
- console.error("No test block found for the given test name");
60
- return;
61
- }
63
+ return {
64
+ hasTestPassed,
65
+ };
62
66
  }
63
67
  exports.runTest = runTest;
@@ -1,4 +1,4 @@
1
- import { Node } from "ts-morph";
1
+ import { Node, SourceFile } from "ts-morph";
2
2
  export declare function cmd(command: string[], options: {
3
3
  env?: Record<string, string>;
4
4
  }): Promise<number>;
@@ -12,12 +12,20 @@ export declare function getAllFilePaths(directoryPath?: string): Promise<string[
12
12
  export declare function getTestCaseNode({ filePath, scenarioName, }: {
13
13
  filePath: string;
14
14
  scenarioName: string;
15
- }): Promise<Node | undefined>;
15
+ }): Promise<{
16
+ testCaseNode: Node | undefined;
17
+ sourceFile: SourceFile;
18
+ }>;
16
19
  export declare function hasTestBlock({ filePath, scenarioName, }: {
17
20
  filePath: string;
18
21
  scenarioName: string;
19
22
  }): Promise<boolean>;
20
23
  export declare function findFirstSerialDescribeBlock(node: Node | undefined): Node | undefined;
21
24
  export declare function hasTopLevelDescribeConfigureWithSerialMode(filePath: string): Promise<boolean>;
22
- export declare function markTestAsOnly(filePath: string, testBlock: string, parentDescribeBlock?: string): Promise<void>;
25
+ export declare function markTestAsOnly({ sourceFile, parentDescribeNode, testCaseNode, filePath, }: {
26
+ sourceFile: SourceFile;
27
+ parentDescribeNode?: Node | undefined;
28
+ testCaseNode: Node;
29
+ filePath: string;
30
+ }): Promise<void>;
23
31
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,IAAI,EAAuB,MAAM,UAAU,CAAC;AAErD,wBAAgB,GAAG,CACjB,OAAO,EAAE,MAAM,EAAE,EACjB,OAAO,EAAE;IAAE,GAAG,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;CAAE,GACxC,OAAO,CAAC,MAAM,CAAC,CA2BjB;AAED;;;;;GAKG;AACH,wBAAsB,eAAe,CACnC,aAAa,GAAE,MAAW,GACzB,OAAO,CAAC,MAAM,EAAE,CAAC,CAqBnB;AAED,wBAAsB,eAAe,CAAC,EACpC,QAAQ,EACR,YAAY,GACb,EAAE;IACD,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,MAAM,CAAC;CACtB,GAAG,OAAO,CAAC,IAAI,GAAG,SAAS,CAAC,CAc5B;AAED,wBAAsB,YAAY,CAAC,EACjC,QAAQ,EACR,YAAY,GACb,EAAE;IACD,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,MAAM,CAAC;CACtB,GAAG,OAAO,CAAC,OAAO,CAAC,CAMnB;AAED,wBAAgB,4BAA4B,CAC1C,IAAI,EAAE,IAAI,GAAG,SAAS,GACrB,IAAI,GAAG,SAAS,CA2BlB;AAED,wBAAsB,0CAA0C,CAC9D,QAAQ,EAAE,MAAM,oBA+BjB;AAED,wBAAsB,cAAc,CAClC,QAAQ,E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AAC;IACnB,QAAQ,EAAE,MAAM,CAAC;CAClB,iBAgBA"}
@@ -72,19 +72,18 @@ async function getTestCaseNode({ filePath, scenarioName, }) {
72
72
  const project = new ts_morph_1.Project();
73
73
  const content = await fs_extra_1.default.readFile(filePath, "utf-8");
74
74
  const sourceFile = project.createSourceFile("test.ts", content);
75
- sourceFile.getDescendants;
76
- const testFunctionNode = sourceFile.getFirstDescendant((node) => !!(node.isKind(ts_morph_1.SyntaxKind.CallExpression) &&
75
+ const testCaseNode = sourceFile.getFirstDescendant((node) => !!(node.isKind(ts_morph_1.SyntaxKind.CallExpression) &&
77
76
  node.getExpression().getText() === "test" &&
78
77
  node.getArguments()[0]?.getText().includes(scenarioName)));
79
- return testFunctionNode;
78
+ return { testCaseNode, sourceFile };
80
79
  }
81
80
  exports.getTestCaseNode = getTestCaseNode;
82
81
  async function hasTestBlock({ filePath, scenarioName, }) {
83
- const testNode = await getTestCaseNode({
82
+ const { testCaseNode } = await getTestCaseNode({
84
83
  filePath,
85
84
  scenarioName,
86
85
  });
87
- return !!testNode;
86
+ return !!testCaseNode;
88
87
  }
89
88
  exports.hasTestBlock = hasTestBlock;
90
89
  function findFirstSerialDescribeBlock(node) {
@@ -139,17 +138,22 @@ async function hasTopLevelDescribeConfigureWithSerialMode(filePath) {
139
138
  return false;
140
139
  }
141
140
  exports.hasTopLevelDescribeConfigureWithSerialMode = hasTopLevelDescribeConfigureWithSerialMode;
142
- async function markTestAsOnly(filePath, testBlock, parentDescribeBlock) {
143
- const fileContent = await fs_extra_1.default.readFile(filePath, "utf-8");
144
- let updatedTestFileContent = fileContent;
145
- if (!parentDescribeBlock) {
146
- const updatedTestBlock = testBlock.replace("test(", "test.only(");
147
- updatedTestFileContent = fileContent.replace(testBlock, updatedTestBlock);
141
+ async function markTestAsOnly({ sourceFile, parentDescribeNode, testCaseNode, filePath, }) {
142
+ let updatedTestFileContent = sourceFile.getFullText();
143
+ if (!parentDescribeNode) {
144
+ const updatedTestBlock = testCaseNode
145
+ .getText()
146
+ .replace("test(", "test.only(");
147
+ testCaseNode?.replaceWithText(updatedTestBlock);
148
+ updatedTestFileContent = sourceFile.getFullText();
148
149
  }
149
150
  else {
150
- const describeMarkedAsOnly = parentDescribeBlock.replace("test.describe(", "test.describe.only(");
151
- updatedTestFileContent = fileContent.replace(parentDescribeBlock, describeMarkedAsOnly);
151
+ const describeMarkedAsOnly = parentDescribeNode
152
+ .getText()
153
+ .replace("test.describe(", "test.describe.only(");
154
+ parentDescribeNode.replaceWithText(describeMarkedAsOnly);
155
+ updatedTestFileContent = sourceFile.getFullText();
152
156
  }
153
- await fs_extra_1.default.writeFile(filePath, updatedTestFileContent);
157
+ await fs_extra_1.default.writeFile(filePath, updatedTestFileContent, "utf-8");
154
158
  }
155
159
  exports.markTestAsOnly = markTestAsOnly;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@empiricalrun/test-run",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "publishConfig": {
5
5
  "registry": "https://registry.npmjs.org/",
6
6
  "access": "public"