index.spec.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. "use strict";
  2. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  3. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  4. return new (P || (P = Promise))(function (resolve, reject) {
  5. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  6. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  7. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  8. step((generator = generator.apply(thisArg, _arguments || [])).next());
  9. });
  10. };
  11. Object.defineProperty(exports, "__esModule", { value: true });
  12. const chai_1 = require("chai");
  13. const child_process_1 = require("child_process");
  14. const path_1 = require("path");
  15. const semver = require("semver");
  16. const ts = require("typescript");
  17. const proxyquire = require("proxyquire");
  18. const index_1 = require("./index");
  19. const fs_1 = require("fs");
  20. const promisify = require("util.promisify");
  21. const execP = promisify(child_process_1.exec);
  22. const TEST_DIR = path_1.join(__dirname, '../tests');
  23. const PROJECT = path_1.join(TEST_DIR, 'tsconfig.json');
  24. const BIN_PATH = path_1.join(TEST_DIR, 'node_modules/.bin/ts-node');
  25. const BIN_SCRIPT_PATH = path_1.join(TEST_DIR, 'node_modules/.bin/ts-node-script');
  26. const SOURCE_MAP_REGEXP = /\/\/# sourceMappingURL=data:application\/json;charset=utf\-8;base64,[\w\+]+=*$/;
  27. // Pack and install ts-node locally, necessary to test package "exports"
  28. before(function () {
  29. return __awaiter(this, void 0, void 0, function* () {
  30. this.timeout(30000);
  31. yield execP(`npm install`, { cwd: TEST_DIR });
  32. const packageLockPath = path_1.join(TEST_DIR, 'package-lock.json');
  33. fs_1.existsSync(packageLockPath) && fs_1.unlinkSync(packageLockPath);
  34. });
  35. });
  36. describe('ts-node', function () {
  37. const cmd = `"${BIN_PATH}" --project "${PROJECT}"`;
  38. this.timeout(10000);
  39. it('should export the correct version', function () {
  40. chai_1.expect(index_1.VERSION).to.equal(require('../package.json').version);
  41. });
  42. describe('cli', function () {
  43. this.slow(1000);
  44. it('should execute cli', function (done) {
  45. child_process_1.exec(`${cmd} tests/hello-world`, function (err, stdout) {
  46. chai_1.expect(err).to.equal(null);
  47. chai_1.expect(stdout).to.equal('Hello, world!\n');
  48. return done();
  49. });
  50. });
  51. it('should register via cli', function (done) {
  52. child_process_1.exec(`node -r ts-node/register hello-world.ts`, {
  53. cwd: TEST_DIR
  54. }, function (err, stdout) {
  55. chai_1.expect(err).to.equal(null);
  56. chai_1.expect(stdout).to.equal('Hello, world!\n');
  57. return done();
  58. });
  59. });
  60. it('should execute cli with absolute path', function (done) {
  61. child_process_1.exec(`${cmd} "${path_1.join(TEST_DIR, 'hello-world')}"`, function (err, stdout) {
  62. chai_1.expect(err).to.equal(null);
  63. chai_1.expect(stdout).to.equal('Hello, world!\n');
  64. return done();
  65. });
  66. });
  67. it('should print scripts', function (done) {
  68. child_process_1.exec(`${cmd} -pe "import { example } from './tests/complex/index';example()"`, function (err, stdout) {
  69. chai_1.expect(err).to.equal(null);
  70. chai_1.expect(stdout).to.equal('example\n');
  71. return done();
  72. });
  73. });
  74. it('should provide registered information globally', function (done) {
  75. child_process_1.exec(`${cmd} tests/env`, function (err, stdout) {
  76. chai_1.expect(err).to.equal(null);
  77. chai_1.expect(stdout).to.equal('object\n');
  78. return done();
  79. });
  80. });
  81. it('should provide registered information on register', function (done) {
  82. child_process_1.exec(`node -r ts-node/register env.ts`, {
  83. cwd: TEST_DIR
  84. }, function (err, stdout) {
  85. chai_1.expect(err).to.equal(null);
  86. chai_1.expect(stdout).to.equal('object\n');
  87. return done();
  88. });
  89. });
  90. if (semver.gte(ts.version, '1.8.0')) {
  91. it('should allow js', function (done) {
  92. child_process_1.exec([
  93. cmd,
  94. '-O "{\\\"allowJs\\\":true}"',
  95. '-pe "import { main } from \'./tests/allow-js/run\';main()"'
  96. ].join(' '), function (err, stdout) {
  97. chai_1.expect(err).to.equal(null);
  98. chai_1.expect(stdout).to.equal('hello world\n');
  99. return done();
  100. });
  101. });
  102. it('should include jsx when `allow-js` true', function (done) {
  103. child_process_1.exec([
  104. cmd,
  105. '-O "{\\\"allowJs\\\":true}"',
  106. '-pe "import { Foo2 } from \'./tests/allow-js/with-jsx\'; Foo2.sayHi()"'
  107. ].join(' '), function (err, stdout) {
  108. chai_1.expect(err).to.equal(null);
  109. chai_1.expect(stdout).to.equal('hello world\n');
  110. return done();
  111. });
  112. });
  113. }
  114. it('should eval code', function (done) {
  115. child_process_1.exec(`${cmd} -e "import * as m from './tests/module';console.log(m.example('test'))"`, function (err, stdout) {
  116. chai_1.expect(err).to.equal(null);
  117. chai_1.expect(stdout).to.equal('TEST\n');
  118. return done();
  119. });
  120. });
  121. it('should import empty files', function (done) {
  122. child_process_1.exec(`${cmd} -e "import './tests/empty'"`, function (err, stdout) {
  123. chai_1.expect(err).to.equal(null);
  124. chai_1.expect(stdout).to.equal('');
  125. return done();
  126. });
  127. });
  128. it('should throw errors', function (done) {
  129. child_process_1.exec(`${cmd} -e "import * as m from './tests/module';console.log(m.example(123))"`, 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.match(new RegExp('TS2345: Argument of type \'(?:number|123)\' ' +
  134. 'is not assignable to parameter of type \'string\'\\.'));
  135. return done();
  136. });
  137. });
  138. it('should be able to ignore diagnostic', function (done) {
  139. child_process_1.exec(`${cmd} --ignore-diagnostics 2345 -e "import * as m from './tests/module';console.log(m.example(123))"`, function (err) {
  140. if (err === null) {
  141. return done('Command was expected to fail, but it succeeded.');
  142. }
  143. chai_1.expect(err.message).to.match(/TypeError: (?:(?:undefined|foo\.toUpperCase) is not a function|.*has no method \'toUpperCase\')/);
  144. return done();
  145. });
  146. });
  147. it('should work with source maps', function (done) {
  148. child_process_1.exec(`${cmd} tests/throw`, function (err) {
  149. if (err === null) {
  150. return done('Command was expected to fail, but it succeeded.');
  151. }
  152. chai_1.expect(err.message).to.contain([
  153. `${path_1.join(__dirname, '../tests/throw.ts')}:100`,
  154. ' bar () { throw new Error(\'this is a demo\') }',
  155. ' ^',
  156. 'Error: this is a demo'
  157. ].join('\n'));
  158. return done();
  159. });
  160. });
  161. it('eval should work with source maps', function (done) {
  162. child_process_1.exec(`${cmd} -pe "import './tests/throw'"`, function (err) {
  163. if (err === null) {
  164. return done('Command was expected to fail, but it succeeded.');
  165. }
  166. chai_1.expect(err.message).to.contain([
  167. `${path_1.join(__dirname, '../tests/throw.ts')}:100`,
  168. ' bar () { throw new Error(\'this is a demo\') }',
  169. ' ^'
  170. ].join('\n'));
  171. return done();
  172. });
  173. });
  174. it('should support transpile only mode', function (done) {
  175. child_process_1.exec(`${cmd} --transpile-only -pe "x"`, function (err) {
  176. if (err === null) {
  177. return done('Command was expected to fail, but it succeeded.');
  178. }
  179. chai_1.expect(err.message).to.contain('ReferenceError: x is not defined');
  180. return done();
  181. });
  182. });
  183. it('should throw error even in transpileOnly mode', function (done) {
  184. child_process_1.exec(`${cmd} --transpile-only -pe "console."`, function (err) {
  185. if (err === null) {
  186. return done('Command was expected to fail, but it succeeded.');
  187. }
  188. chai_1.expect(err.message).to.contain('error TS1003: Identifier expected');
  189. return done();
  190. });
  191. });
  192. it('should pipe into `ts-node` and evaluate', function (done) {
  193. const cp = child_process_1.exec(cmd, function (err, stdout) {
  194. chai_1.expect(err).to.equal(null);
  195. chai_1.expect(stdout).to.equal('hello\n');
  196. return done();
  197. });
  198. cp.stdin.end("console.log('hello')");
  199. });
  200. it('should pipe into `ts-node`', function (done) {
  201. const cp = child_process_1.exec(`${cmd} -p`, function (err, stdout) {
  202. chai_1.expect(err).to.equal(null);
  203. chai_1.expect(stdout).to.equal('true\n');
  204. return done();
  205. });
  206. cp.stdin.end('true');
  207. });
  208. it('should pipe into an eval script', function (done) {
  209. const cp = child_process_1.exec(`${cmd} --transpile-only -pe "process.stdin.isTTY"`, function (err, stdout) {
  210. chai_1.expect(err).to.equal(null);
  211. chai_1.expect(stdout).to.equal('undefined\n');
  212. return done();
  213. });
  214. cp.stdin.end('true');
  215. });
  216. it('should run REPL when --interactive passed and stdin is not a TTY', function (done) {
  217. const cp = child_process_1.exec(`${cmd} --interactive`, function (err, stdout) {
  218. chai_1.expect(err).to.equal(null);
  219. chai_1.expect(stdout).to.equal('> 123\n' +
  220. 'undefined\n' +
  221. '> ');
  222. return done();
  223. });
  224. cp.stdin.end('console.log("123")\n');
  225. });
  226. it('should support require flags', function (done) {
  227. child_process_1.exec(`${cmd} -r ./tests/hello-world -pe "console.log('success')"`, function (err, stdout) {
  228. chai_1.expect(err).to.equal(null);
  229. chai_1.expect(stdout).to.equal('Hello, world!\nsuccess\nundefined\n');
  230. return done();
  231. });
  232. });
  233. it('should support require from node modules', function (done) {
  234. child_process_1.exec(`${cmd} -r typescript -e "console.log('success')"`, function (err, stdout) {
  235. chai_1.expect(err).to.equal(null);
  236. chai_1.expect(stdout).to.equal('success\n');
  237. return done();
  238. });
  239. });
  240. it.skip('should use source maps with react tsx', function (done) {
  241. child_process_1.exec(`${cmd} -r ./tests/emit-compiled.ts tests/jsx-react.tsx`, function (err, stdout) {
  242. chai_1.expect(err).to.equal(null);
  243. chai_1.expect(stdout).to.equal('todo');
  244. return done();
  245. });
  246. });
  247. it('should allow custom typings', function (done) {
  248. child_process_1.exec(`${cmd} tests/custom-types`, function (err, stdout) {
  249. chai_1.expect(err).to.match(/Error: Cannot find module 'does-not-exist'/);
  250. return done();
  251. });
  252. });
  253. it('should preserve `ts-node` context with child process', function (done) {
  254. child_process_1.exec(`${cmd} tests/child-process`, function (err, stdout) {
  255. chai_1.expect(err).to.equal(null);
  256. chai_1.expect(stdout).to.equal('Hello, world!\n');
  257. return done();
  258. });
  259. });
  260. it('should import js before ts by default', function (done) {
  261. child_process_1.exec(`${cmd} tests/import-order/compiled`, function (err, stdout) {
  262. chai_1.expect(err).to.equal(null);
  263. chai_1.expect(stdout).to.equal('Hello, JavaScript!\n');
  264. return done();
  265. });
  266. });
  267. it('should import ts before js when --prefer-ts-exts flag is present', function (done) {
  268. child_process_1.exec(`${cmd} --prefer-ts-exts tests/import-order/compiled`, function (err, stdout) {
  269. chai_1.expect(err).to.equal(null);
  270. chai_1.expect(stdout).to.equal('Hello, TypeScript!\n');
  271. return done();
  272. });
  273. });
  274. it('should import ts before js when TS_NODE_PREFER_TS_EXTS env is present', function (done) {
  275. child_process_1.exec(`${cmd} tests/import-order/compiled`, { env: Object.assign(Object.assign({}, process.env), { TS_NODE_PREFER_TS_EXTS: 'true' }) }, function (err, stdout) {
  276. chai_1.expect(err).to.equal(null);
  277. chai_1.expect(stdout).to.equal('Hello, TypeScript!\n');
  278. return done();
  279. });
  280. });
  281. it('should ignore .d.ts files', function (done) {
  282. child_process_1.exec(`${cmd} tests/import-order/importer`, function (err, stdout) {
  283. chai_1.expect(err).to.equal(null);
  284. chai_1.expect(stdout).to.equal('Hello, World!\n');
  285. return done();
  286. });
  287. });
  288. describe('issue #884', function () {
  289. it('should compile', function (done) {
  290. // TODO disabled because it consistently fails on Windows on TS 2.7
  291. if (process.platform === 'win32' && semver.satisfies(ts.version, '2.7')) {
  292. this.skip();
  293. }
  294. else {
  295. child_process_1.exec(`"${BIN_PATH}" --project tests/issue-884/tsconfig.json tests/issue-884`, function (err, stdout) {
  296. chai_1.expect(err).to.equal(null);
  297. chai_1.expect(stdout).to.equal('');
  298. return done();
  299. });
  300. }
  301. });
  302. });
  303. describe('issue #986', function () {
  304. it('should not compile', function (done) {
  305. child_process_1.exec(`"${BIN_PATH}" --project tests/issue-986/tsconfig.json tests/issue-986`, function (err, stdout, stderr) {
  306. chai_1.expect(err).not.to.equal(null);
  307. chai_1.expect(stderr).to.contain('Cannot find name \'TEST\''); // TypeScript error.
  308. chai_1.expect(stdout).to.equal('');
  309. return done();
  310. });
  311. });
  312. it('should compile with `--files`', function (done) {
  313. child_process_1.exec(`"${BIN_PATH}" --files --project tests/issue-986/tsconfig.json tests/issue-986`, function (err, stdout, stderr) {
  314. chai_1.expect(err).not.to.equal(null);
  315. chai_1.expect(stderr).to.contain('ReferenceError: TEST is not defined'); // Runtime error.
  316. chai_1.expect(stdout).to.equal('');
  317. return done();
  318. });
  319. });
  320. });
  321. if (semver.gte(ts.version, '2.7.0')) {
  322. it('should support script mode', function (done) {
  323. child_process_1.exec(`${BIN_SCRIPT_PATH} tests/scope/a/log`, function (err, stdout) {
  324. chai_1.expect(err).to.equal(null);
  325. chai_1.expect(stdout).to.equal('.ts\n');
  326. return done();
  327. });
  328. });
  329. it('should read tsconfig relative to realpath, not symlink, in scriptMode', function (done) {
  330. if (fs_1.statSync(path_1.join(TEST_DIR, 'main-realpath/symlink/symlink.tsx')).isSymbolicLink()) {
  331. child_process_1.exec(`${BIN_SCRIPT_PATH} tests/main-realpath/symlink/symlink.tsx`, function (err, stdout) {
  332. chai_1.expect(err).to.equal(null);
  333. chai_1.expect(stdout).to.equal('');
  334. return done();
  335. });
  336. }
  337. else {
  338. this.skip();
  339. }
  340. });
  341. }
  342. describe('should read ts-node options from tsconfig.json', function () {
  343. const BIN_EXEC = `"${BIN_PATH}" --project tests/tsconfig-options/tsconfig.json`;
  344. it('should override compiler options from env', function (done) {
  345. child_process_1.exec(`${BIN_EXEC} tests/tsconfig-options/log-options.js`, {
  346. env: Object.assign(Object.assign({}, process.env), { TS_NODE_COMPILER_OPTIONS: '{"typeRoots": ["env-typeroots"]}' })
  347. }, function (err, stdout) {
  348. chai_1.expect(err).to.equal(null);
  349. const { config } = JSON.parse(stdout);
  350. chai_1.expect(config.options.typeRoots).to.deep.equal([path_1.join(__dirname, '../tests/tsconfig-options/env-typeroots').replace(/\\/g, '/')]);
  351. return done();
  352. });
  353. });
  354. it('should use options from `tsconfig.json`', function (done) {
  355. child_process_1.exec(`${BIN_EXEC} tests/tsconfig-options/log-options.js`, function (err, stdout) {
  356. chai_1.expect(err).to.equal(null);
  357. const { options, config } = JSON.parse(stdout);
  358. chai_1.expect(config.options.typeRoots).to.deep.equal([path_1.join(__dirname, '../tests/tsconfig-options/tsconfig-typeroots').replace(/\\/g, '/')]);
  359. chai_1.expect(config.options.types).to.deep.equal(['tsconfig-tsnode-types']);
  360. chai_1.expect(options.pretty).to.equal(undefined);
  361. chai_1.expect(options.skipIgnore).to.equal(false);
  362. chai_1.expect(options.transpileOnly).to.equal(true);
  363. return done();
  364. });
  365. });
  366. it('should have flags override `tsconfig.json`', function (done) {
  367. child_process_1.exec(`${BIN_EXEC} --skip-ignore --compiler-options "{\\"types\\":[\\"flags-types\\"]}" tests/tsconfig-options/log-options.js`, function (err, stdout) {
  368. chai_1.expect(err).to.equal(null);
  369. const { options, config } = JSON.parse(stdout);
  370. chai_1.expect(config.options.typeRoots).to.deep.equal([path_1.join(__dirname, '../tests/tsconfig-options/tsconfig-typeroots').replace(/\\/g, '/')]);
  371. chai_1.expect(config.options.types).to.deep.equal(['flags-types']);
  372. chai_1.expect(options.pretty).to.equal(undefined);
  373. chai_1.expect(options.skipIgnore).to.equal(true);
  374. chai_1.expect(options.transpileOnly).to.equal(true);
  375. return done();
  376. });
  377. });
  378. it('should have `tsconfig.json` override environment', function (done) {
  379. child_process_1.exec(`${BIN_EXEC} tests/tsconfig-options/log-options.js`, {
  380. env: Object.assign(Object.assign({}, process.env), { TS_NODE_PRETTY: 'true', TS_NODE_SKIP_IGNORE: 'true' })
  381. }, function (err, stdout) {
  382. chai_1.expect(err).to.equal(null);
  383. const { options, config } = JSON.parse(stdout);
  384. chai_1.expect(config.options.typeRoots).to.deep.equal([path_1.join(__dirname, '../tests/tsconfig-options/tsconfig-typeroots').replace(/\\/g, '/')]);
  385. chai_1.expect(config.options.types).to.deep.equal(['tsconfig-tsnode-types']);
  386. chai_1.expect(options.pretty).to.equal(true);
  387. chai_1.expect(options.skipIgnore).to.equal(false);
  388. chai_1.expect(options.transpileOnly).to.equal(true);
  389. return done();
  390. });
  391. });
  392. });
  393. describe('compiler host', function () {
  394. it('should execute cli', function (done) {
  395. child_process_1.exec(`${cmd} --compiler-host tests/hello-world`, function (err, stdout) {
  396. chai_1.expect(err).to.equal(null);
  397. chai_1.expect(stdout).to.equal('Hello, world!\n');
  398. return done();
  399. });
  400. });
  401. it('should give ts error for invalid node_modules', function (done) {
  402. child_process_1.exec(`${cmd} --compiler-host --skip-ignore tests/from-node-modules/from-node-modules`, function (err, stdout) {
  403. if (err === null)
  404. return done('Expected an error');
  405. chai_1.expect(err.message).to.contain('Unable to compile file from external library');
  406. return done();
  407. });
  408. });
  409. });
  410. });
  411. describe('register', function () {
  412. const registered = index_1.register({
  413. project: PROJECT,
  414. compilerOptions: {
  415. jsx: 'preserve'
  416. }
  417. });
  418. const moduleTestPath = require.resolve('../tests/module');
  419. afterEach(() => {
  420. // Re-enable project after every test.
  421. registered.enabled(true);
  422. });
  423. it('should be able to require typescript', function () {
  424. const m = require(moduleTestPath);
  425. chai_1.expect(m.example('foo')).to.equal('FOO');
  426. });
  427. it('should support dynamically disabling', function () {
  428. delete require.cache[moduleTestPath];
  429. chai_1.expect(registered.enabled(false)).to.equal(false);
  430. chai_1.expect(() => require(moduleTestPath)).to.throw(/Unexpected token/);
  431. delete require.cache[moduleTestPath];
  432. chai_1.expect(registered.enabled()).to.equal(false);
  433. chai_1.expect(() => require(moduleTestPath)).to.throw(/Unexpected token/);
  434. delete require.cache[moduleTestPath];
  435. chai_1.expect(registered.enabled(true)).to.equal(true);
  436. chai_1.expect(() => require(moduleTestPath)).to.not.throw();
  437. delete require.cache[moduleTestPath];
  438. chai_1.expect(registered.enabled()).to.equal(true);
  439. chai_1.expect(() => require(moduleTestPath)).to.not.throw();
  440. });
  441. if (semver.gte(ts.version, '2.7.0')) {
  442. it('should support compiler scopes', function () {
  443. const calls = [];
  444. registered.enabled(false);
  445. const compilers = [
  446. index_1.register({ dir: path_1.join(TEST_DIR, 'scope/a'), scope: true }),
  447. index_1.register({ dir: path_1.join(TEST_DIR, 'scope/b'), scope: true })
  448. ];
  449. compilers.forEach(c => {
  450. const old = c.compile;
  451. c.compile = (code, fileName, lineOffset) => {
  452. calls.push(fileName);
  453. return old(code, fileName, lineOffset);
  454. };
  455. });
  456. try {
  457. chai_1.expect(require('../tests/scope/a').ext).to.equal('.ts');
  458. chai_1.expect(require('../tests/scope/b').ext).to.equal('.ts');
  459. }
  460. finally {
  461. compilers.forEach(c => c.enabled(false));
  462. }
  463. chai_1.expect(calls).to.deep.equal([
  464. path_1.join(TEST_DIR, 'scope/a/index.ts'),
  465. path_1.join(TEST_DIR, 'scope/b/index.ts')
  466. ]);
  467. delete require.cache[moduleTestPath];
  468. chai_1.expect(() => require(moduleTestPath)).to.throw();
  469. });
  470. }
  471. it('should compile through js and ts', function () {
  472. const m = require('../tests/complex');
  473. chai_1.expect(m.example()).to.equal('example');
  474. });
  475. it('should work with proxyquire', function () {
  476. const m = proxyquire('../tests/complex', {
  477. './example': 'hello'
  478. });
  479. chai_1.expect(m.example()).to.equal('hello');
  480. });
  481. it('should work with `require.cache`', function () {
  482. const { example1, example2 } = require('../tests/require-cache');
  483. chai_1.expect(example1).to.not.equal(example2);
  484. });
  485. it('should use source maps', function (done) {
  486. try {
  487. require('../tests/throw');
  488. }
  489. catch (error) {
  490. chai_1.expect(error.stack).to.contain([
  491. 'Error: this is a demo',
  492. ` at Foo.bar (${path_1.join(__dirname, '../tests/throw.ts')}:100:18)`
  493. ].join('\n'));
  494. done();
  495. }
  496. });
  497. describe('JSX preserve', () => {
  498. let old = require.extensions['.tsx']; // tslint:disable-line
  499. let compiled;
  500. before(function () {
  501. require.extensions['.tsx'] = (m, fileName) => {
  502. const _compile = m._compile;
  503. m._compile = (code, fileName) => {
  504. compiled = code;
  505. return _compile.call(this, code, fileName);
  506. };
  507. return old(m, fileName);
  508. };
  509. });
  510. after(function () {
  511. require.extensions['.tsx'] = old; // tslint:disable-line
  512. });
  513. it('should use source maps', function (done) {
  514. try {
  515. require('../tests/with-jsx.tsx');
  516. }
  517. catch (error) {
  518. chai_1.expect(error.stack).to.contain('SyntaxError: Unexpected token');
  519. }
  520. chai_1.expect(compiled).to.match(SOURCE_MAP_REGEXP);
  521. done();
  522. });
  523. });
  524. });
  525. describe('create', () => {
  526. it('should create generic compiler instances', () => {
  527. const service = index_1.create({ compilerOptions: { target: 'es5' }, skipProject: true });
  528. const output = service.compile('const x = 10', 'test.ts');
  529. chai_1.expect(output).to.contain('var x = 10;');
  530. });
  531. });
  532. if (semver.gte(process.version, '13.0.0')) {
  533. describe('esm', () => {
  534. this.slow(1000);
  535. const cmd = `node --loader ../../esm.mjs`;
  536. it('should compile and execute as ESM', (done) => {
  537. child_process_1.exec(`${cmd} index.ts`, { cwd: path_1.join(__dirname, '../tests/esm') }, function (err, stdout) {
  538. chai_1.expect(err).to.equal(null);
  539. chai_1.expect(stdout).to.equal('foo bar baz biff\n');
  540. return done();
  541. });
  542. });
  543. it('supports --experimental-specifier-resolution=node', (done) => {
  544. child_process_1.exec(`${cmd} --experimental-specifier-resolution=node index.ts`, { cwd: path_1.join(__dirname, '../tests/esm-node-resolver') }, function (err, stdout) {
  545. chai_1.expect(err).to.equal(null);
  546. chai_1.expect(stdout).to.equal('foo bar baz biff\n');
  547. return done();
  548. });
  549. });
  550. });
  551. }
  552. });
  553. //# sourceMappingURL=index.spec.js.map