UNPKG

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