1

I use this demo for learn how to use mocha

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new.target

here is my code, (with no error)

// JavaScript source code

var should = require('should');
var chai = require('chai');
var expect = require('chai').expect;
var assert = require('chai').assert;

describe('new.target', function () {

    describe('new.target in function calls', function () {

        function Foo() {
            if (!new.target) throw 'Foo() must be called with new';
        }

        it('Foo instantiated with new', function () {
            (new Foo()).should.be.an.instanceof(Foo);
        });

        it('Foo() must be called with new', function () {
            //Foo().should.throw('Foo() must be called with new');
            assert.throws(Foo, 'Foo() must be called with new');
        });

    });

});

but Foo().should.throw('Foo() must be called with new'); always fail and Foo().should.throw(/Foo\(\) must be called with new/); fail too

or I should just don't use should?

https://github.com/tj/should.js#throw-and-throwerror

How to I do without add this code every time? and add to npm test

var should = require('should');
var chai = require('chai');
var expect = require('chai').expect;
var assert = require('chai').assert;
bluelovers
  • 1,035
  • 2
  • 10
  • 22
  • It passed for me. Which node version you using? `new.target` is supported from the box by 5.9.0 on which it's passed. When I tried it with some 4.x release it's failed – llamerr Mar 24 '16 at 18:16
  • Same. Running this exact code works for me -- all tests pass. – rtf Mar 24 '16 at 18:47
  • i use node 5.8.0, i know `new.target` is supported, i just use it for learn mocha, when i use `Foo().should.throw('Foo() must be called with new');` it will show error msg at console – bluelovers Mar 24 '16 at 20:13
  • Possible duplicate of http://stackoverflow.com/questions/36216868/how-to-test-for-thrown-error-with-chai-should ? You don't actually should call the function that should throw, but just pass it (omit the braces ;) ) – David Losert Mar 25 '16 at 12:22

0 Answers0