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