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

How to feed .js files to TypeScript when the allowJs flag is provided? #123

Closed
kruncher opened this issue May 29, 2016 · 9 comments
Closed

Comments

@kruncher
Copy link

Looking at the source for ts-node I see that it filters against the extensions .ts and .tsx.

In my case I would like it to also treat the .js extension in this way but only within a certain path of my node application since I do not want any .js scripts from within node_modules to be put through the TypeScript compiler in this way.

Is there a way to do this that I am perhaps missing?

Many thanks!

@blakeembrey
Copy link
Member

No, it's not possible today. It would be a nice addition.

@kruncher
Copy link
Author

👍 Awesome stuff! thanks :)

@blakeembrey
Copy link
Member

@kruncher If you get a chance, I'd love for someone to test it before I release 😄

@kruncher
Copy link
Author

@blakeembrey I am trying to get this to work but having difficulties with passing the --allowJs argument to the command line. Here are things that I have tried:

λ mocha --require ts-node/register --allowJs
  error: unknown option `--allowJs'
λ mocha --require ts-node/register --project ./test/tsconfig.json
  error: unknown option `--project'
λ mocha --require ts-node/register --compilerOptions '{ "allowJs": true }'
  Error: cannot resolve path (or pattern) 'allowJs:'

I suspect that I am not properly understanding the instructions on the readme page.

@blakeembrey
Copy link
Member

@kruncher You can not pass options into a Mocha register like that. It expects a file, so you're just passing options to mocha instead. The only workarounds using that syntax would be to use environment variables or set it in tsconfig.json.

@kruncher
Copy link
Author

kruncher commented Jun 13, 2016

@blakeembrey I created a script file to register ts-node, startup mocha and then start the test running process and it seems to be working 😃

There was one slight false alarm:

TSError: ⨯ Unable to compile TypeScript
Cannot write file '/abc/test/Test.js' because it would overwrite input file. (5055)
Cannot write file '/abc/test/main.js' because it would overwrite input file. (5055)
test\main.js: Emit skipped

I was able to take the batteries out by specifying a path for outDir for the compilerOptions when registering ts-node. No output was generated but it does seem like an unnecessary error message.

It would be really awesome if there were a way to set up ts-node to work with mocha without having to create the bootstrap; perhaps with the environment variables.

Here is the bootstrap script that I threw together to try this out:

// /abc/scripts/test.js

require('ts-node').register({
  compilerOptions: {
    outDir: "../test-js",
    allowJs: true
  }
});

// from: https://github.com/mochajs/mocha/wiki/Using-mocha-programmatically
var Mocha = require('mocha'),
  fs = require('fs'),
  path = require('path');

// Instantiate a Mocha instance.
var mocha = new Mocha();

var testDir = path.join(__dirname, '../test');

// Add each .js file to the mocha instance
fs.readdirSync(testDir).filter(function(file){
  // Only keep the .js files
  return file.substr(-3) === '.js';

}).forEach(function(file){
  mocha.addFile(
    path.join(testDir, file)
  );
});

// Run the tests.
mocha.run(function(failures){
  process.on('exit', function () {
    process.exit(failures);  // exit with non-zero status if there were failures
  });
});

@blakeembrey
Copy link
Member

@kruncher Thanks for the catch, I've run into this on another module before. I think we just need to fake the outDir for now, I'll patch this ASAP.

@blakeembrey
Copy link
Member

@kruncher Did you want to try https://github.com/TypeStrong/ts-node/releases/tag/v0.9.1?

@kruncher
Copy link
Author

@blakeembrey Awesome stuff; the outDir fix works great 😃

Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants