Skip to content

Commit

Permalink
Add more complex examples
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed Jul 15, 2015
1 parent 9ec2079 commit 962e4ca
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 4 deletions.
58 changes: 56 additions & 2 deletions src/typescript-node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,72 @@ const BIN_PATH = join(__dirname, '../dist/bin/ts-node')
const compiler = register()

describe('ts-node', function () {
this.timeout(5000)

it('should execute cli', function (done) {
exec(`node ${BIN_PATH} ${join(__dirname, '../tests/hello-world')}`, function (err, stdout) {
exec(`node ${BIN_PATH} tests/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(`node ${BIN_PATH} -p "import { example } from './tests/complex/index';example()"`, function (err, stdout) {
expect(err).to.not.exist
expect(stdout).to.equal('example\n')

return done()
})
})

it('should eval code', function (done) {
exec(`node ${BIN_PATH} -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(`node ${BIN_PATH} -e "import * as m from './tests/module';console.log(m.example(123))"`, function (err, stdout) {
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(`node ${BIN_PATH} --ignoreWarnings 2345 -e "import * as m from './tests/module';console.log(m.example(123))"`, function (err, stdout) {
expect(err.message).to.contain('TypeError: foo.toUpperCase is not a function')

return done()
})
})

it('should work with source maps', function (done) {
exec(`node ${BIN_PATH} tests/throw`, function (err, stdout) {
expect(err.message).to.contain([
' bar () { throw new Error(\'this is a demo\') }',
' ^',
'Error: this is a demo'
].join('\n'))

return done()
})
})

it('should be able to require typescript', function () {
var m = require('../tests/module')

expect(m.example()).to.be.true
expect(m.example('foo')).to.equal('FOO')
})

it('should compile through js and ts', function () {
var m = require('../tests/complex')

expect(m.example()).to.equal('example')
})
})
1 change: 1 addition & 0 deletions tests/complex/example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./foo').text
1 change: 1 addition & 0 deletions tests/complex/foo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const text = 'example'
3 changes: 3 additions & 0 deletions tests/complex/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function example () {
return require('./example')
}
4 changes: 2 additions & 2 deletions tests/module.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export function example (foo: string): boolean {
return true
export function example (foo: string) {
return foo.toUpperCase()
}
5 changes: 5 additions & 0 deletions tests/throw.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Foo {
constructor () { this.bar() }
bar () { throw new Error('this is a demo') }
}
new Foo()

0 comments on commit 962e4ca

Please sign in to comment.