Friday, 18 March 2022

Node - Execute command asynchronously and read results on completion

I have a suite of tests written with Playwright. I am trying to execute these tests from a separate suite of tests. In this separate suite of tests, I need to examine the results of the original results. This leads me to the following directory structure:

/
  /checks
    checks1.spec.js
  /tests
    tests1.spec.js
    tests2.spec.js
  playwright.config.js

My files look like this:

playwright.config.js

// @ts-check
const { devices } = require('@playwright/test');
    
/**
 * @see https://playwright.dev/docs/test-configuration
 * @type {import('@playwright/test').PlaywrightTestConfig}
 */
const config = {
  testDir: '.',
  timeout: 30 * 1000,
  expect: {
    timeout: 5000
  },
  forbidOnly: !!process.env.CI,
  retries: process.env.CI ? 2 : 0,
  workers: process.env.CI ? 1 : undefined,
  reporter: [
    ['html', { outputFolder: 'reports' } ]
  ],
  use: {
    actionTimeout: 0,
    trace: 'on-first-retry',
  },

  /* Configure projects for major browsers */
  projects: [
    {
      name: 'chromium',
      use: {
        ...devices['Desktop Chrome'],
      },
    }
  ]
};

module.exports = config;

tests1.spec.js

const { test, expect } = require('@playwright/test');

test.describe('Field Tests', () => {
  test('Should be required', async({ page }) => {
    await page.goto('http://localhost:8080');
    await expect(true).toBe(true);
  });
});

checks1.spec.js

const { exec } = require('child_process');
const { test, expect } = require('@playwright/test');

const command = 'npx playwright test --reporter=json ./tests/tests1.spec.js';

test.describe('Field', () => {
    test(`Require state`, async () => {
      const { stdout, stderr } = await exec(command);
            
      // Execute the test and get the results
      const buffer = child_process.execSync(command);
      const json = JSON.parse(buffer.toString()); 
      const results = json.suites[0].suites[0].specs;
        
      const status = results[0].tests[0].results[0].status;
      expect(status).toBe('passed');
    });
});

When I run npx playwright test --reporter=json ./tests/test1.spec.js, I receive the JSON output as I would expect. However, when I run npx playwright test checks, I receive a Socket instead of the JSON. How do I run the command and wait for the JSON to be returned? Thank you.



from Node - Execute command asynchronously and read results on completion

No comments:

Post a Comment