index.spec.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 import empty files', function (done) {
  83. child_process_1.exec(BIN_EXEC + " -e \"import './tests/empty'\"", function (err, stdout) {
  84. chai_1.expect(err).to.equal(null);
  85. chai_1.expect(stdout).to.equal('');
  86. return done();
  87. });
  88. });
  89. it('should throw errors', function (done) {
  90. child_process_1.exec(BIN_EXEC + " -e \"import * as m from './tests/module';console.log(m.example(123))\"", function (err) {
  91. if (err === null) {
  92. return done('Command was expected to fail, but it succeeded.');
  93. }
  94. chai_1.expect(err.message).to.match(new RegExp('TS2345: Argument of type \'(?:number|123)\' ' +
  95. 'is not assignable to parameter of type \'string\'\\.'));
  96. return done();
  97. });
  98. });
  99. it('should be able to ignore diagnostic', function (done) {
  100. child_process_1.exec(BIN_EXEC + " --ignoreDiagnostics 2345 -e \"import * as m from './tests/module';console.log(m.example(123))\"", function (err) {
  101. if (err === null) {
  102. return done('Command was expected to fail, but it succeeded.');
  103. }
  104. chai_1.expect(err.message).to.match(/TypeError: (?:(?:undefined|foo\.toUpperCase) is not a function|.*has no method \'toUpperCase\')/);
  105. return done();
  106. });
  107. });
  108. it('should work with source maps', function (done) {
  109. child_process_1.exec(BIN_EXEC + " tests/throw", function (err) {
  110. if (err === null) {
  111. return done('Command was expected to fail, but it succeeded.');
  112. }
  113. chai_1.expect(err.message).to.contain([
  114. path_1.join(__dirname, '../tests/throw.ts') + ":3",
  115. ' bar () { throw new Error(\'this is a demo\') }',
  116. ' ^',
  117. 'Error: this is a demo'
  118. ].join('\n'));
  119. return done();
  120. });
  121. });
  122. it.skip('eval should work with source maps', function (done) {
  123. child_process_1.exec(BIN_EXEC + " -p \"import './tests/throw'\"", function (err) {
  124. if (err === null) {
  125. return done('Command was expected to fail, but it succeeded.');
  126. }
  127. chai_1.expect(err.message).to.contain([
  128. path_1.join(__dirname, '../tests/throw.ts') + ":3",
  129. ' bar () { throw new Error(\'this is a demo\') }',
  130. ' ^'
  131. ].join('\n'));
  132. return done();
  133. });
  134. });
  135. it('should support transpile only mode', function (done) {
  136. child_process_1.exec(BIN_EXEC + " --transpileOnly -p \"x\"", function (err) {
  137. if (err === null) {
  138. return done('Command was expected to fail, but it succeeded.');
  139. }
  140. chai_1.expect(err.message).to.contain('ReferenceError: x is not defined');
  141. return done();
  142. });
  143. });
  144. it('should pipe into `ts-node` and evaluate', function (done) {
  145. var cp = child_process_1.exec(BIN_EXEC, function (err, stdout) {
  146. chai_1.expect(err).to.equal(null);
  147. chai_1.expect(stdout).to.equal('hello\n');
  148. return done();
  149. });
  150. cp.stdin.end("console.log('hello')");
  151. });
  152. it('should pipe into `ts-node`', function (done) {
  153. var cp = child_process_1.exec(BIN_EXEC + " -p", function (err, stdout) {
  154. chai_1.expect(err).to.equal(null);
  155. chai_1.expect(stdout).to.equal('true\n');
  156. return done();
  157. });
  158. cp.stdin.end('true');
  159. });
  160. it('should pipe into an eval script', function (done) {
  161. var cp = child_process_1.exec(BIN_EXEC + " --fast -p 'process.stdin.isTTY'", function (err, stdout) {
  162. chai_1.expect(err).to.equal(null);
  163. chai_1.expect(stdout).to.equal('undefined\n');
  164. return done();
  165. });
  166. cp.stdin.end('true');
  167. });
  168. it('should support require flags', function (done) {
  169. child_process_1.exec(BIN_EXEC + " -r ./tests/hello-world -p \"console.log('success')\"", function (err, stdout) {
  170. chai_1.expect(err).to.equal(null);
  171. chai_1.expect(stdout).to.equal('Hello, world!\nsuccess\nundefined\n');
  172. return done();
  173. });
  174. });
  175. it('should support require from node modules', function (done) {
  176. child_process_1.exec(BIN_EXEC + " -r typescript -e \"console.log('success')\"", function (err, stdout) {
  177. chai_1.expect(err).to.equal(null);
  178. chai_1.expect(stdout).to.equal('success\n');
  179. return done();
  180. });
  181. });
  182. it.skip('should use source maps with react tsx', function (done) {
  183. child_process_1.exec(BIN_EXEC + " -r ./tests/emit-compiled.ts tests/jsx-react.tsx", function (err, stdout) {
  184. chai_1.expect(err).to.equal(null);
  185. chai_1.expect(stdout).to.equal('todo');
  186. return done();
  187. });
  188. });
  189. it('should allow custom typings', function (done) {
  190. child_process_1.exec(BIN_EXEC + " tests/custom-types", function (err, stdout) {
  191. chai_1.expect(err).to.match(/Error: Cannot find module 'does-not-exist'/);
  192. return done();
  193. });
  194. });
  195. });
  196. describe('register', function () {
  197. index_1.register({
  198. project: path_1.join(testDir, 'tsconfig.json'),
  199. compilerOptions: {
  200. jsx: 'preserve'
  201. }
  202. });
  203. it('should be able to require typescript', function () {
  204. var m = require('../tests/module');
  205. chai_1.expect(m.example('foo')).to.equal('FOO');
  206. });
  207. it('should compile through js and ts', function () {
  208. var m = require('../tests/complex');
  209. chai_1.expect(m.example()).to.equal('example');
  210. });
  211. it('should work with proxyquire', function () {
  212. var m = proxyquire('../tests/complex', {
  213. './example': 'hello'
  214. });
  215. chai_1.expect(m.example()).to.equal('hello');
  216. });
  217. it('should use source maps', function (done) {
  218. try {
  219. require('../tests/throw');
  220. }
  221. catch (error) {
  222. chai_1.expect(error.stack).to.contain([
  223. 'Error: this is a demo',
  224. " at Foo.bar (" + path_1.join(__dirname, '../tests/throw.ts') + ":3:18)"
  225. ].join('\n'));
  226. done();
  227. }
  228. });
  229. describe('JSX preserve', function () {
  230. var old = require.extensions['.tsx'];
  231. var compiled;
  232. before(function () {
  233. var _this = this;
  234. require.extensions['.tsx'] = function (m, fileName) {
  235. var _compile = m._compile;
  236. m._compile = function (code, fileName) {
  237. compiled = code;
  238. return _compile.call(_this, code, fileName);
  239. };
  240. return old(m, fileName);
  241. };
  242. });
  243. after(function () {
  244. require.extensions['.tsx'] = old;
  245. });
  246. it('should use source maps', function (done) {
  247. try {
  248. require('../tests/with-jsx.tsx');
  249. }
  250. catch (error) {
  251. chai_1.expect(error.stack).to.contain('SyntaxError: Unexpected token <\n');
  252. }
  253. chai_1.expect(compiled).to.match(SOURCE_MAP_REGEXP);
  254. done();
  255. });
  256. });
  257. });
  258. });
  259. //# sourceMappingURL=index.spec.js.map