@rpcbase/test 0.14.0 → 0.15.0

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/package.json CHANGED
@@ -1,9 +1,10 @@
1
1
  {
2
2
  "name": "@rpcbase/test",
3
- "version": "0.14.0",
3
+ "version": "0.15.0",
4
4
  "bin": {
5
5
  "rb-test": "./src/cli.js"
6
6
  },
7
+ "main": "./src/index.js",
7
8
  "scripts": {
8
9
  "build": "wireit",
9
10
  "release": "wireit"
@@ -31,7 +32,8 @@
31
32
  }
32
33
  },
33
34
  "dependencies": {
34
- "@playwright/test": "1.49.1"
35
+ "@playwright/test": "1.50.1",
36
+ "lodash": "4.17.21"
35
37
  },
36
38
  "devDependencies": {}
37
39
  }
package/src/cli.js CHANGED
@@ -1,44 +1,63 @@
1
1
  #!/usr/bin/env node
2
- const { test } = require('@playwright/test');
3
- const fs = require('fs');
4
- const path = require('path');
2
+ const { spawn } = require("child_process");
3
+ const fs = require("fs");
4
+ const path = require("path");
5
5
 
6
- async function runTests() {
7
- try {
6
+ const isAider = process.env.IS_AIDER === "yes";
7
+
8
+ if (process.env.IS_AIDER !== undefined && process.env.IS_AIDER !== "yes") {
9
+ console.warn("Warning: IS_AIDER is set to a value other than 'yes'.");
10
+ }
11
+
12
+ function runTests() {
13
+ return new Promise((resolve, reject) => {
8
14
  // Determine config file path
9
- const configPath = fs.existsSync(path.join(process.cwd(), 'playwright.config.ts'))
10
- ? path.join(process.cwd(), 'playwright.config.ts')
11
- : path.join(__dirname, 'playwright.config.ts');
12
-
13
- // Import the config file
14
- const config = require(configPath);
15
-
16
- // Create a test runner
17
- const { _runTests } = test;
18
-
19
- // Run the tests with the configuration
20
- const testResult = await _runTests({
21
- config,
22
- configFile: configPath,
23
- // Add any additional options you need
24
- workers: config.workers || 1,
25
- timeout: config.timeout || 30000,
15
+ const configPath = fs.existsSync(
16
+ path.join(process.cwd(), "playwright.config.ts"),
17
+ )
18
+ ? path.join(process.cwd(), "playwright.config.ts")
19
+ : path.join(__dirname, "playwright.config.ts");
20
+
21
+ const stdoutBuffer = [];
22
+ const stderrBuffer = [];
23
+
24
+ const playwright = spawn(
25
+ "./node_modules/.bin/playwright",
26
+ ["test", "--config", configPath],
27
+ {
28
+ shell: false,
29
+ },
30
+ );
31
+
32
+ playwright.stdout.on("data", (data) => {
33
+ !isAider && process.stdout.write(data);
34
+ stdoutBuffer.push(data.toString());
26
35
  });
27
36
 
28
- if (testResult.status === 'passed') {
29
- console.log('All tests passed!');
30
- process.exit(0);
31
- } else if (testResult.status === 'failed') {
32
- console.error('Tests failed!');
33
- process.exit(1);
34
- } else if (testResult.status === 'timedout') {
35
- console.error('Tests timed out!');
36
- process.exit(1);
37
- }
38
- } catch (error) {
39
- console.error('Error running tests:', error);
40
- process.exit(1);
41
- }
37
+ playwright.stderr.on("data", (data) => {
38
+ !isAider && process.stderr.write(data);
39
+ stderrBuffer.push(data.toString());
40
+ });
41
+
42
+ playwright.on("close", (code) => {
43
+ if (code === 0) {
44
+ console.log("All tests passed!");
45
+ resolve();
46
+ } else {
47
+ console.error("Tests failed:");
48
+ console.error(stdoutBuffer.join(""))
49
+
50
+ reject(new Error(`Tests failed with exit code: ${code}`));
51
+ }
52
+ });
53
+
54
+ playwright.on("error", (error) => {
55
+ console.error("Error spawning Playwright:", error);
56
+ reject(error);
57
+ });
58
+ });
42
59
  }
43
60
 
44
- runTests();
61
+ runTests()
62
+ .then(() => process.exit(0))
63
+ .catch(() => process.exit(1));
@@ -0,0 +1,108 @@
1
+ const fs = require("fs")
2
+ const {cpus} = require("os")
3
+ const _merge = require("lodash/merge")
4
+ const {defineConfig, devices} = require("@playwright/test")
5
+
6
+
7
+ function mergeConfig(base, override) {
8
+ return _merge({}, base, override);
9
+ }
10
+
11
+ const hasPassedAllPreviousTests = (data) => {
12
+ function checkSuite(suite) {
13
+ if (suite.specs && suite.specs.length > 0) {
14
+ for (const spec of suite.specs) {
15
+ if (!checkSpec(spec)) return false
16
+ }
17
+ }
18
+ if (suite.suites && suite.suites.length > 0) {
19
+ for (const innerSuite of suite.suites) {
20
+ if (!checkSuite(innerSuite)) return false
21
+ }
22
+ }
23
+ return true
24
+ }
25
+
26
+ function checkSpec(spec) {
27
+ if (spec.tests && spec.tests.length > 0) {
28
+ for (const test of spec.tests) {
29
+ if (!checkTest(test)) return false
30
+ }
31
+ }
32
+ return true
33
+ }
34
+
35
+ function checkTest(test) {
36
+ if (test.results && test.results.length > 0) {
37
+ for (const result of test.results) {
38
+ if (!["passed", "skipped"].includes(result.status)) return false
39
+ }
40
+ }
41
+ return true
42
+ }
43
+
44
+ if (data.suites && data.suites.length > 0) {
45
+ for (const suite of data.suites) {
46
+ if (!checkSuite(suite)) return false
47
+ }
48
+ }
49
+
50
+ return true
51
+ }
52
+
53
+
54
+ const isHeadless = () => {
55
+ if (process.env.CI) return true
56
+ try {
57
+ const lastResults = JSON.parse(fs.readFileSync("./build/playwright/results.json", "utf8"))
58
+ const hasPassed = hasPassedAllPreviousTests(lastResults)
59
+ return hasPassed
60
+ } catch (_err) {
61
+ // console.log("Error reading last results", err)
62
+ // Default to show if no previous results or error reading file
63
+ return false
64
+ }
65
+ }
66
+
67
+
68
+ // https://playwright.dev/docs/test-configuration.
69
+ module.exports = function(config) {
70
+
71
+ const baseConfig = {
72
+ globalSetup: ["./spec/helpers/globalSetup.ts"],
73
+ testDir: "./build/spec",
74
+ fullyParallel: false,
75
+ /* Fail the build on CI if you accidentally left test.only in the source code. */
76
+ forbidOnly: !!process.env.CI,
77
+ /* Retry on CI only */
78
+ retries: process.env.CI ? 4 : 0,
79
+ /* Opt out of parallel tests on CI. */
80
+ workers: process.env.CI ? 1 : cpus().length,
81
+ /* Reporter to use. See https://playwright.dev/docs/test-reporters */
82
+ reporter: [
83
+ ["html", {open: "never", outputFolder: "./build/playwright/report"}],
84
+ ["blob", {outputFile: "./build/playwright/blob"}],
85
+ ["list", {outputFile: "./build/playwright/results.txt"}],
86
+ ["json", { outputFile: "./build/playwright/results.json" }]
87
+ ],
88
+ outputDir: "./build/playwright/test-results",
89
+ /* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
90
+ use: {
91
+ baseURL: "http://localhost:8080",
92
+ /* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
93
+ trace: "on",
94
+ video: "on",
95
+ launchOptions: {
96
+ headless: isHeadless(),
97
+ },
98
+ },
99
+ projects: [
100
+ {
101
+ name: "chromium",
102
+ use: { ...devices["Desktop Chrome"] },
103
+ },
104
+ ],
105
+ }
106
+
107
+ return defineConfig(mergeConfig(baseConfig, config))
108
+ }
package/src/index.js ADDED
@@ -0,0 +1,3 @@
1
+ const defineConfig = require("./defineConfig")
2
+
3
+ module.exports = {defineConfig}
@@ -1,5 +0,0 @@
1
-
2
-
3
- export const runPlaywright = async() => {
4
-
5
- }