How to check for class inheritance in Coffeescript

2019-09-08 16:04发布

How do I check for the class of an object in a mocha spec in Coffeescript?

I have tried the following:

# foo.coffee
class Foo
module.exports = new Foo()

# foo_spec.coffee
should  = require 'should'
{ Foo } = require 'foo'
foo = new Foo
foo.should.be.an.instanceOf(Foo)

However, I receive ReferenceError Foo is not defined

1条回答
\"骚年 ilove
2楼-- · 2019-09-08 16:49

I believe this to be the easiest approach:

# foo.coffee
class Foo
module.exports = new Foo()
module.exports.Foo = Foo # IMPORTANT, exports the actual class Foo


# foo_spec.coffee
should  = require 'should'
{ Foo } = require 'foo' # Requires said class Foo
foo = new Foo
foo.should.be.an.instanceOf(Foo)
查看更多
登录 后发表回答