Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Ability to run jest test from node script #3848

Closed
CzBuCHi opened this issue Jun 18, 2017 · 12 comments
Closed

[Feature] Ability to run jest test from node script #3848

CzBuCHi opened this issue Jun 18, 2017 · 12 comments

Comments

@CzBuCHi
Copy link
Contributor

CzBuCHi commented Jun 18, 2017

Love to see official support for running jest test from node without need to use jest-cli 'hack'.

Basically code bellow except require('jest-cli/build/cli/runCLI'); (this feels like poking around places, that i shouldn't)

// generated
const options = {
    testNamePattern: '1',
    testPathPattern: 'su[bm]',
    projects: [__dirname],
    silent: true,
    json:true,
};

const runCLI = require('jest-cli/build/cli/runCLI');
runCLI(options, options.projects, result => {
  // results is object and not json string
});

Usage: automated test runner / 3rd party editor integration

@cpojer
Copy link
Member

cpojer commented Jun 18, 2017 via email

@thymikee
Copy link
Collaborator

@CzBuCHi
Copy link
Contributor Author

CzBuCHi commented Jun 19, 2017

Thanks, i was blind and didnt see that when i was poking around in sources ...

@thymikee
Copy link
Collaborator

Happens, have fun hacking! :)

@CzBuCHi
Copy link
Contributor Author

CzBuCHi commented Jun 19, 2017

@thymikee after bunch of trials and errors i still canot run jest tests from node process...
it looks like require('jest-cli/build/cli/runCLI') function is killing process without calling process exit event ...

minimal code to get the idea, what im trying to do:

main.ts - executed in context of vscode extension

import { fork, ForkOptions } from 'child_process';
function runTests(options, port) {
  // options example:
  //{
  //  testNamePattern: '^Utility adds 1 \+ 2 to equal 3$',
  //  testPathPattern: 'utility\.test',
  //  projects: ['some/path/to/project'],
  //  silent: true,
  //  json: true,
  //};
  
  const forkArgs = [];
  if (debug) {
    forkArgs.push('--debug=' + port);
  }
  
  // fork child node process so i can attach debugger to it ...
  const process = fork("test.js", forkArgs, { cwd: options.projects[0], silent: true });
  
  if (debug) {
    // attach debugger to port `port`
    // vscode.commands.executeCommand('vscode.startDebug', {...});
  }
  
  return new Promise((resolve, reject) => {
    let results: any;
    let stdout: string[] = [];
    let stderr: string[] = [];
    
    process.on('message', data => { results = data; });
    process.stdout.on('data', data => stdout.push(data));    
    process.stderr.on('data', data => stderr.push(data));
    
    process.on('exit', code => {
      if (code !== 0) {
        reject(stdout.join('') + '\r\n' + stderr.join(''));
      } else {
        resolve({ results, stdout: stdout.join('') });
      }
    });
    
    if (debug) {
      // give debugger some time to properly attach itself before running tests ...        
      setTimeout(() => {
        process.send(options);
      }, 1000);
    } else {
      process.send(options);
    }
  });
}

test.js

const runCLI = require('jest-cli/build/cli/runCLI');
process.on('message', options => {
  try {
    // debugger breakpoint hits here and show correct `options` object
    runCLI(options, options.projects, result => {
      // never called
      process.send(result);
      process.exit(0);
    });
  } catch (error) {
    // never called
    process.send(error);
    process.exit(-1);
  }
});

interestingly, when i run code bellow directly from command line (node script.js) it will succeed:

const runCLI = require('jest-cli/build/cli/runCLI');

const options = {
  testNamePattern: '^Utility adds 1 \+ 2 to equal 3$',
  testPathPattern: 'utility\.test',
  projects: ['some/path/to/project'],
  silent: true,
  json: true,
};

runCLI(options, options.projects, result => {
  console.log(JSON.stringify(result));
});

@thymikee
Copy link
Collaborator

Maybe you're hitting this: #3737?

@CzBuCHi
Copy link
Contributor Author

CzBuCHi commented Jun 20, 2017

@thymikee yes ... and no :) ... im completelly bypassing jest.run() - from code it looks like that it is parsing cmd args and sending them to jestCLI .... with already have onComplete callback.

@thymikee
Copy link
Collaborator

Maybe your tests are broken somewhere, have you tried with forceExit option?

@CzBuCHi
Copy link
Contributor Author

CzBuCHi commented Jun 20, 2017

@thymikee i heard u first time :)

test file, that im using looks like this: (im working on how to run and debug jest tests from vscode, not on tests itself)

const utility = require('../utility');
// content of '../utility':
//module.exports = {
//    add: function(a, b) { 
//        return a + b;
//    }
//};

describe('Utility', function() {
    it('adds 1 + 2 to equal 3', function() {
        expect(utility.add(1, 2)).toBe(3);
    });

    it('adds 1 + -1 to not equal 2', function() {
        expect(utility.add(1, -1)).not.toBe(2);
    });
});

@thymikee
Copy link
Collaborator

Sorry about that, low mobile connectivity issues :D.

Ok, so it looks like something with forking a process. Internally we do it like this for our integration tests: https://github.com/facebook/jest/blob/master/integration_tests/runJest.js
You may also want to see how vscode-jest handles it: https://github.com/orta/vscode-jest

@seanpoulter
Copy link
Contributor

You've got me curious about your integrations tests there @thymikee. With some vscode-jest users saying it took too long to get the full JSON results, is there any internal demand to stream results using a custom reporter? I've been toying with the idea of streaming to an IPC socket.

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants