index.spec.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var chai_1 = require("chai");
  4. var child_process_1 = require("child_process");
  5. var path_1 = require("path");
  6. var semver = require("semver");
  7. var ts = require("typescript");
  8. var proxyquire = require("proxyquire");
  9. var index_1 = require("./index");
  10. var testDir = path_1.join(__dirname, '../tests');
  11. var EXEC_PATH = path_1.join(__dirname, '../dist/bin');
  12. var BIN_EXEC = "node \"" + EXEC_PATH + "\" --project \"" + testDir + "/tsconfig.json\"";
  13. var SOURCE_MAP_REGEXP = /\/\/# sourceMappingURL=data:application\/json;charset=utf\-8;base64,[\w\+]+=*$/;
  14. describe('ts-node', function () {
  15. this.timeout(10000);
  16. it('should export the correct version', function () {
  17. chai_1.expect(index_1.VERSION).to.equal(require('../package.json').version);
  18. });
  19. describe('cli', function () {
  20. this.slow(1000);
  21. it('should execute cli', function (done) {
  22. child_process_1.exec(BIN_EXEC + " tests/hello-world", function (err, stdout) {
  23. chai_1.expect(err).to.equal(null);
  24. chai_1.expect(stdout).to.equal('Hello, world!\n');
  25. return done();
  26. });
  27. });
  28. it('should register via cli', function (done) {
  29. child_process_1.exec("node -r ../register hello-world.ts", {
  30. cwd: testDir
  31. }, function (err, stdout) {
  32. chai_1.expect(err).to.equal(null);
  33. chai_1.expect(stdout).to.equal('Hello, world!\n');
  34. return done();
  35. });
  36. });
  37. it('should execute cli with absolute path', function (done) {
  38. child_process_1.exec(BIN_EXEC + " \"" + path_1.join(testDir, 'hello-world') + "\"", function (err, stdout) {
  39. chai_1.expect(err).to.equal(null);
  40. chai_1.expect(stdout).to.equal('Hello, world!\n');
  41. return done();
  42. });
  43. });
  44. it('should print scripts', function (done) {
  45. child_process_1.exec(BIN_EXEC + " -p \"import { example } from './tests/complex/index';example()\"", function (err, stdout) {
  46. chai_1.expect(err).to.equal(null);
  47. chai_1.expect(stdout).to.equal('example\n');
  48. return done();
  49. });
  50. });
  51. if (semver.gte(ts.version, '1.8.0')) {
  52. it('should allow js', function (done) {
  53. child_process_1.exec([
  54. BIN_EXEC,
  55. '-O "{\\\"allowJs\\\":true}"',
  56. '-p "import { main } from \'./tests/allow-js/run\';main()"'
  57. ].join(' '), function (err, stdout) {
  58. chai_1.expect(err).to.equal(null);
  59. chai_1.expect(stdout).to.equal('hello world\n');
  60. return done();
  61. });
  62. });
  63. it('should include jsx when `allow-js` true', function (done) {
  64. child_process_1.exec([
  65. BIN_EXEC,
  66. '-O "{\\\"allowJs\\\":true}"',
  67. '-p "import { Foo2 } from \'./tests/allow-js/with-jsx\'; Foo2.sayHi()"'
  68. ].join(' '), function (err, stdout) {
  69. chai_1.expect(err).to.equal(null);
  70. chai_1.expect(stdout).to.equal('hello world\n');
  71. return done();
  72. });
  73. });
  74. }
  75. it('should eval code', function (done) {
  76. child_process_1.exec(BIN_EXEC + " -e \"import * as m from './tests/module';console.log(m.example('test'))\"", function (err, stdout) {
  77. chai_1.expect(err).to.equal(null);
  78. chai_1.expect(stdout).to.equal('TEST\n');
  79. return done();
  80. });
  81. });
  82. it('should throw errors', function (done) {
  83. child_process_1.exec(BIN_EXEC + " --typeCheck -e \"import * as m from './tests/module';console.log(m.example(123))\"", function (err) {
  84. if (err === null) {
  85. return done('Command was expected to fail, but it succeeded.');
  86. }
  87. chai_1.expect(err.message).to.match(new RegExp('\\[eval\\]\\.ts \\(1,59\\): Argument of type \'(?:number|123)\' ' +
  88. 'is not assignable to parameter of type \'string\'\\. \\(2345\\)'));
  89. return done();
  90. });
  91. });
  92. it('should be able to ignore diagnostic', function (done) {
  93. child_process_1.exec(BIN_EXEC + " --type-check --ignoreDiagnostics 2345 -e \"import * as m from './tests/module';console.log(m.example(123))\"", function (err) {
  94. if (err === null) {
  95. return done('Command was expected to fail, but it succeeded.');
  96. }
  97. chai_1.expect(err.message).to.match(/TypeError: (?:(?:undefined|foo\.toUpperCase) is not a function|.*has no method \'toUpperCase\')/);
  98. return done();
  99. });
  100. });
  101. it('should work with source maps', function (done) {
  102. child_process_1.exec(BIN_EXEC + " --type-check tests/throw", function (err) {
  103. if (err === null) {
  104. return done('Command was expected to fail, but it succeeded.');
  105. }
  106. chai_1.expect(err.message).to.contain([
  107. path_1.join(__dirname, '../tests/throw.ts') + ":3",
  108. ' bar () { throw new Error(\'this is a demo\') }',
  109. ' ^',
  110. 'Error: this is a demo'
  111. ].join('\n'));
  112. return done();
  113. });
  114. });
  115. it.skip('eval should work with source maps', function (done) {
  116. child_process_1.exec(BIN_EXEC + " --type-check -p \"import './tests/throw'\"", function (err) {
  117. if (err === null) {
  118. return done('Command was expected to fail, but it succeeded.');
  119. }
  120. chai_1.expect(err.message).to.contain([
  121. path_1.join(__dirname, '../tests/throw.ts') + ":3",
  122. ' bar () { throw new Error(\'this is a demo\') }',
  123. ' ^'
  124. ].join('\n'));
  125. return done();
  126. });
  127. });
  128. it('should use transpile mode by default', function (done) {
  129. child_process_1.exec(BIN_EXEC + " -p \"x\"", function (err) {
  130. if (err === null) {
  131. return done('Command was expected to fail, but it succeeded.');
  132. }
  133. chai_1.expect(err.message).to.contain('ReferenceError: x is not defined');
  134. return done();
  135. });
  136. });
  137. it('should pipe into `ts-node` and evaluate', function (done) {
  138. var cp = child_process_1.exec(BIN_EXEC, function (err, stdout) {
  139. chai_1.expect(err).to.equal(null);
  140. chai_1.expect(stdout).to.equal('hello\n');
  141. return done();
  142. });
  143. cp.stdin.end("console.log('hello')");
  144. });
  145. it('should pipe into `ts-node`', function (done) {
  146. var cp = child_process_1.exec(BIN_EXEC + " -p", function (err, stdout) {
  147. chai_1.expect(err).to.equal(null);
  148. chai_1.expect(stdout).to.equal('true\n');
  149. return done();
  150. });
  151. cp.stdin.end('true');
  152. });
  153. it('should pipe into an eval script', function (done) {
  154. var cp = child_process_1.exec(BIN_EXEC + " --fast -p 'process.stdin.isTTY'", function (err, stdout) {
  155. chai_1.expect(err).to.equal(null);
  156. chai_1.expect(stdout).to.equal('undefined\n');
  157. return done();
  158. });
  159. cp.stdin.end('true');
  160. });
  161. it('should support require flags', function (done) {
  162. child_process_1.exec(BIN_EXEC + " -r ./tests/hello-world -p \"console.log('success')\"", function (err, stdout) {
  163. chai_1.expect(err).to.equal(null);
  164. chai_1.expect(stdout).to.equal('Hello, world!\nsuccess\nundefined\n');
  165. return done();
  166. });
  167. });
  168. it('should support require from node modules', function (done) {
  169. child_process_1.exec(BIN_EXEC + " -r typescript -e \"console.log('success')\"", function (err, stdout) {
  170. chai_1.expect(err).to.equal(null);
  171. chai_1.expect(stdout).to.equal('success\n');
  172. return done();
  173. });
  174. });
  175. it.skip('should use source maps with react tsx', function (done) {
  176. child_process_1.exec(BIN_EXEC + " -r ./tests/emit-compiled.ts tests/jsx-react.tsx", function (err, stdout) {
  177. chai_1.expect(err).to.equal(null);
  178. chai_1.expect(stdout).to.equal('todo');
  179. return done();
  180. });
  181. });
  182. });
  183. describe('register', function () {
  184. index_1.register({
  185. project: path_1.join(testDir, 'tsconfig.json'),
  186. compilerOptions: {
  187. jsx: 'preserve'
  188. }
  189. });
  190. it('should be able to require typescript', function () {
  191. var m = require('../tests/module');
  192. chai_1.expect(m.example('foo')).to.equal('FOO');
  193. });
  194. it('should compile through js and ts', function () {
  195. var m = require('../tests/complex');
  196. chai_1.expect(m.example()).to.equal('example');
  197. });
  198. it('should work with proxyquire', function () {
  199. var m = proxyquire('../tests/complex', {
  200. './example': 'hello'
  201. });
  202. chai_1.expect(m.example()).to.equal('hello');
  203. });
  204. it('should use source maps', function (done) {
  205. try {
  206. require('../tests/throw');
  207. }
  208. catch (error) {
  209. chai_1.expect(error.stack).to.contain([
  210. 'Error: this is a demo',
  211. " at Foo.bar (" + path_1.join(__dirname, '../tests/throw.ts') + ":3:18)"
  212. ].join('\n'));
  213. done();
  214. }
  215. });
  216. describe('JSX preserve', function () {
  217. var old = require.extensions['.tsx'];
  218. var compiled;
  219. before(function () {
  220. var _this = this;
  221. require.extensions['.tsx'] = function (m, fileName) {
  222. var _compile = m._compile;
  223. m._compile = function (code, fileName) {
  224. compiled = code;
  225. return _compile.call(_this, code, fileName);
  226. };
  227. return old(m, fileName);
  228. };
  229. });
  230. after(function () {
  231. require.extensions['.tsx'] = old;
  232. });
  233. it('should use source maps', function (done) {
  234. try {
  235. require('../tests/with-jsx.tsx');
  236. }
  237. catch (error) {
  238. chai_1.expect(error.stack).to.contain('SyntaxError: Unexpected token <\n');
  239. }
  240. chai_1.expect(compiled).to.match(SOURCE_MAP_REGEXP);
  241. done();
  242. });
  243. });
  244. });
  245. });
  246. //# sourceMappingURL=index.spec.js.map