-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Comments
If you require jest or jest-cli, it has runCLI as exported function.
…________________________________
From: Marek Buchar <[email protected]>
Sent: Sunday, June 18, 2017 11:59:18 AM
To: facebook/jest
Cc: Subscribed
Subject: [facebook/jest] [Feature] Ability to run jest test from node script (#3848)
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
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub<#3848>, or mute the thread<https://github.com/notifications/unsubscribe-auth/AAA0KNhd1gAJ5PAaLWsCj0JBt9l5Ds41ks5sFPT2gaJpZM4N9dHf>.
|
You can see what's exported here: https://github.com/facebook/jest/blob/v20.0.4/packages/jest-cli/src/jest.js |
Thanks, i was blind and didnt see that when i was poking around in sources ... |
Happens, have fun hacking! :) |
@thymikee after bunch of trials and errors i still canot run jest tests from node process... 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 ( 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));
}); |
Maybe you're hitting this: #3737? |
@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. |
Maybe your tests are broken somewhere, have you tried with forceExit option? |
@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);
});
}); |
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've got me curious about your integrations tests there @thymikee. With some |
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. |
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)Usage: automated test runner / 3rd party editor integration
The text was updated successfully, but these errors were encountered: