jscodeshift icon indicating copy to clipboard operation
jscodeshift copied to clipboard

Tests fail without any message if there are type errors

Open danielo515 opened this issue 3 years ago • 0 comments

Hello. The typescript support is awesome, but it has a small drawback when testing: if your TS code has any type error, the tests will fail without any explanation nor error.

It should be as simple as wrapping the require module in a try-catch block and output the message before exiting, something like this:

function runTest(dirName, transformName, options, testFilePrefix, testOptions = {}) {
  if (!testFilePrefix) {
    testFilePrefix = transformName;
  }

  const extension = extensionForParser(testOptions.parser)
  const fixtureDir = path.join(dirName, '..', '__testfixtures__');
  const inputPath = path.join(fixtureDir, testFilePrefix + `.input.${extension}`);
  const source = fs.readFileSync(inputPath, 'utf8');
  const expectedOutput = fs.readFileSync(
    path.join(fixtureDir, testFilePrefix + `.output.${extension}`),
    'utf8'
  );
  // Assumes transform is one level up from __tests__ directory
  let module;
  try {
    module = require(path.join(dirName, '..', transformName));
  } catch(e){
    console.log('Error importing module',e)
  }
    runInlineTest(module, options, {
      path: inputPath,
      source
    }, expectedOutput, testOptions);
}
exports.runTest = runTest;

danielo515 avatar Nov 15 '21 12:11 danielo515