-
-
Notifications
You must be signed in to change notification settings - Fork 537
/
Copy pathindex.spec.ts
208 lines (168 loc) · 5.88 KB
/
index.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import { expect } from 'chai'
import { exec } from 'child_process'
import { join } from 'path'
import semver = require('semver')
import ts = require('typescript')
import proxyquire = require('proxyquire')
import { register, VERSION } from './index'
const testDir = join(__dirname, '../tests')
const EXEC_PATH = join(__dirname, '../dist/bin')
const BIN_EXEC = `node ${EXEC_PATH}`
describe('ts-node', function () {
this.timeout(10000)
it('should export the correct version', function () {
expect(VERSION).to.equal(require('../package.json').version)
})
describe('cli', function () {
it('should execute cli', function (done) {
exec(`${BIN_EXEC} tests/hello-world`, function (err, stdout) {
expect(err).to.not.exist
expect(stdout).to.equal('Hello, world!\n')
return done()
})
})
it('should execute cli with absolute path', function (done) {
exec(`${BIN_EXEC} "${join(testDir, 'hello-world')}"`, function (err, stdout) {
expect(err).to.not.exist
expect(stdout).to.equal('Hello, world!\n')
return done()
})
})
it('should print scripts', function (done) {
exec(`${BIN_EXEC} -p "import { example } from './tests/complex/index';example()"`, function (err, stdout) {
expect(err).to.not.exist
expect(stdout).to.equal('example\n')
return done()
})
})
if (semver.gte(ts.version, '1.8.0')) {
it('should allow js', function (done) {
exec(
[
BIN_EXEC,
'-O "{\\\"allowJs\\\":true}"',
'-p "import { main } from \'./tests/allow-js/run\';main()"'
].join(' '),
function (err, stdout) {
expect(err).to.not.exist
expect(stdout).to.equal('hello world\n')
return done()
}
)
})
}
it('should eval code', function (done) {
exec(
`${BIN_EXEC} -e "import * as m from './tests/module';console.log(m.example('test'))"`,
function (err, stdout) {
expect(err).to.not.exist
expect(stdout).to.equal('TEST\n')
return done()
}
)
})
it('should throw errors', function (done) {
exec(`${BIN_EXEC} -e "import * as m from './tests/module';console.log(m.example(123))"`, function (err) {
expect(err.message).to.contain(
'[eval].ts (1,59): Argument of type \'number\' is not assignable to parameter of type \'string\'. (2345)'
)
return done()
})
})
it('should be able to ignore errors', function (done) {
exec(
`${BIN_EXEC} --ignoreWarnings 2345 -e "import * as m from './tests/module';console.log(m.example(123))"`,
function (err) {
expect(err.message).to.match(
/TypeError: (?:(?:undefined|foo\.toUpperCase) is not a function|.*has no method \'toUpperCase\')/
)
return done()
}
)
})
it('should work with source maps', function (done) {
exec(`${BIN_EXEC} tests/throw`, function (err) {
expect(err.message).to.contain([
`${join(__dirname, '../tests/throw.ts')}:3`,
' bar () { throw new Error(\'this is a demo\') }',
' ^',
'Error: this is a demo'
].join('\n'))
return done()
})
})
it('eval should work with source maps', function (done) {
exec(`${BIN_EXEC} -p "import './tests/throw'"`, function (err) {
expect(err.message).to.contain([
`${join(__dirname, '../tests/throw.ts')}:3`,
' bar () { throw new Error(\'this is a demo\') }',
' ^',
'Error: this is a demo'
].join('\n'))
return done()
})
})
it('should ignore all warnings', function (done) {
exec(`${BIN_EXEC} -D -p "x"`, function (err) {
expect(err.message).to.contain('ReferenceError: x is not defined')
return done()
})
})
it('should pipe into `ts-node` and evaluate', function (done) {
exec(`echo "console.log('hello')" | ${BIN_EXEC}`, function (err, stdout) {
expect(err).to.not.exist
expect(stdout).to.equal('hello\n')
return done()
})
})
it('should pipe into `ts-node`', function (done) {
exec(`echo "true" | ${BIN_EXEC} -p`, function (err, stdout) {
expect(err).to.not.exist
expect(stdout).to.equal('true\n')
return done()
})
})
it('should pipe into an eval script', function (done) {
exec(`echo "true" | ${BIN_EXEC} -p '(process.stdin as any).isTTY'`, function (err, stdout) {
expect(err).to.not.exist
expect(stdout).to.equal('undefined\n')
return done()
})
})
it('should support require flags', function (done) {
exec(`${BIN_EXEC} -r ./tests/hello-world -p "console.log('success')"`, function (err, stdout) {
expect(err).to.not.exist
expect(stdout).to.equal('Hello, world!\nsuccess\nundefined\n')
return done()
})
})
})
describe('register', function () {
register({ project: join(testDir, '..') })
it('should be able to require typescript', function () {
const m = require('../tests/module')
expect(m.example('foo')).to.equal('FOO')
})
it('should compile through js and ts', function () {
const m = require('../tests/complex')
expect(m.example()).to.equal('example')
})
it('should work with proxyquire', function () {
const m = proxyquire('../tests/complex', {
'./example': 'hello'
})
expect(m.example()).to.equal('hello')
})
it('should use source maps', function (done) {
try {
require('../tests/throw')
} catch (error) {
expect(error.stack).to.contain([
'Error: this is a demo',
` at Foo.bar (${join(__dirname, '../tests/throw.ts')}:3:18)`
].join('\n'))
done()
}
})
})
})