Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Cannot read property 'setUp' of undefined #198

Closed
notcoffeetable opened this issue Mar 22, 2013 · 10 comments
Closed

TypeError: Cannot read property 'setUp' of undefined #198

notcoffeetable opened this issue Mar 22, 2013 · 10 comments

Comments

@notcoffeetable
Copy link

So I'm looking at Caolan Mcmahon's Tutorial
Adding the following code causes the title error

    console.log = function (str) {
        test.equal(str, 'doubled: 24');
    };

The problem line is test.equal(str, 'doubled: 24');.

@wooj2
Copy link

wooj2 commented May 10, 2013

Ahh! ok i figured it out. Looks like Mcmahon's tutorial is old.. and node unit has been updated to change the way they define tests.. so.... you'll need to define test-doubled.js in the manner outlined on the home page of the project:
https://github.com/caolan/nodeunit

More explicitly... the contents of test-doubled.js would look something like this:
var doubled = require('../lib/doubled');
var events = require("events");
module.exports = {
setUp: function(callback) {
callback();
},
tearDown: function(callback){
callback();
},
testCalculate: function (test) {
//fill in body here.
},
readanumber: function(test) {
//fill in body here
},
};

@xiaofenqin
Copy link

My code as followed, and give some error info:
//////////////////////////////////////////////////////////////////////////////
test-doubled

FAILURES: Undone tests (or their setups/teardowns):

  • test1

To fix this, make sure all tests call test.done()

/////////////////////////////////////////////////////////////////////////////////

module.exports = {
setUp: function (callback) {
this._openStdin = process.openStdin;
this._log = console.log;
this._calculate = doubled.calculate;
this._exit = process.exit;

    var ev = this.ev = new events.EventEmitter();
    process.openStdin = function () { return ev; };

},
tearDown: function (callback) {
    // reset all the overidden functions:
    process.openStdin = this._openStdin;
    process.exit = this._exit;
    doubled.calculate = this._calculate;
    console.log = this._log;
},
test1 : function (test) {
    process.exit = test.done;
    console.log = function (str) {
        test.equal(str, 'doubled: 24');
    };
    test.expect(1);
    doubled.read();
    this.ev.emit('data', '12');
}

}

@Nethken
Copy link

Nethken commented May 30, 2013

I'm seeing the same issue, even after rewriting the tests in the for with the setUp and tearDown. It happens after the last unit test has run successfully.

node_modules/nodeunit/bin/nodeunit test

test_app
✔ test

test_config
✔ test

test_references
✔ test

test_v1
✔ testLoad

OK: 5 assertions (30ms)
✔ testLoad

project/nodetest/node_modules/nodeunit/lib/core.js:284
if (group.setUp) {
^
TypeError: Cannot read property 'setUp' of undefined
at wrapGroup (project/nodetest/node_modules/nodeunit/lib/core.js:284:14)
...

@terryweiss
Copy link

I saw this issue today on a set of tests that had previously been working. In the event that this helps someone else, it was an issue with a badly formed callback. The error message is misleading. What I had done was something like this:

function A(callback){
// code...
if (someCondition){
callback(); //<- problem here, should have been return callback()
}
callback();
}

Then in the test:

exports.myTest(test){
A(function(){
test.done()
})
}

This issue is that test.done() was called twice due to that upstream callback being a problem. That second call to test.done() was causing nodeunit to look for a test that doesn't exist and failing on the call to "group". But it's not clear from the error what is happening. I found it by using mocha to run the same test and it correctly reported "done() called more than once" which is what I think nodeunit needs to do.

t.

@x8k2
Copy link

x8k2 commented Jun 3, 2013

I have the same problem as @Nethken

The problem actually lies in the fact that nodeunit seems to use console.log for its output as well.
This means that you need to revert console.log back to its former state in the tearDown function.

A rough working example:

var postgres = require('../postgres.js')
var events = require('events')
old_console_log = console.log

module.exports = {
  setUp: function(callback){
    old_console_log("in setUp")
    return callback();
  },
  tearDown: function(callback){
    console.log = old_console_log
    old_console_log("in tearDown")
    return callback();
  },
  readANumber: function(test){
    var ev = new events.EventEmitter();
    process.openStdin = function() { return ev; }
    process.exit = test.done;


    console.log = function(str){
      old_console_log("Asserting: " + str)
      test.equal(str, 'sumting');
    }

    postgres.read();
    ev.emit('data', 'sumting');
  }
}

@SaraAssi
Copy link

SaraAssi commented Aug 4, 2013

you can use console.info() or console.warn() instead of console.log() which cause the problem

@fabiocicerchia
Copy link

Any progress on this?

@caolan
Copy link
Owner

caolan commented Mar 28, 2014

This is a misleading error message you sometimes get when callbacks are hit multiple times or in odd ways. I'm closing this but feel free to open a new issue to improve the error message.

@caolan caolan closed this as completed Mar 28, 2014
@shadowmint
Copy link

For anyone else who finds this, 'when callbacks are hit multiple times or in odd ways' probably means; if you have multiple callbacks that invoke test.done() asynchronously.

For example:

  // Exact match
  {
    var query = store.query('value').only('1');
    store.collect(query).then((records) => {
      test.equals(records.length(), 2);
      test.equals(records.all()[0].value, '1');
      test.equals(records.all()[1].value, '1');
      test.done();
    });
  }

  // Upper bound
  {
    var query = store.query('value').upperBound('1');
    store.collect(query).then((records) => {
      console.log('Items?', records.all());
      test.equals(records.length(), 2);
      test.equals(records.all()[0].value, '1');
      test.equals(records.all()[1].value, '1');
      test.done(); // <--- woops! This should be a separate test...
    });
  }

Will sometimes trigger it, sometimes not depending on race conditions; you can resolve by ensuring that all the promises are resolved and then calling test.done(), not calling it multiple times.

(It would be very useful is test.done() error'd with a meaningful error message if it was called outside of a test scope)

@ngallo
Copy link

ngallo commented Oct 9, 2014

A null check is missing in the core.js file.

After running nodeunit webserver/tests/ I was having the following error and I was struggling to understand what the issue was.


core_auth_tokenmanager_tests
✔ core/auth/tokenManager - createNewTokenWithInvalidParameterTest1

/usr/local/lib/node_modules/nodeunit/lib/core.js:285
if (group.setUp) {
^
TypeError: Cannot read property 'setUp' of null
at wrapGroup (/usr/local/lib/node_modules/nodeunit/lib/core.js:285:14)
at wrapGroup (/usr/local/lib/node_modules/nodeunit/lib/core.js:306:24)
at wrapGroup (/usr/local/lib/node_modules/nodeunit/lib/core.js:306:24)
at wrapGroup (/usr/local/lib/node_modules/nodeunit/lib/core.js:306:24)
at Object.exports.runModule (/usr/local/lib/node_modules/nodeunit/lib/core.js:146:11)
at /usr/local/lib/node_modules/nodeunit/lib/nodeunit.js:75:21
at /usr/local/lib/node_modules/nodeunit/deps/async.js:513:13
at iterate (/usr/local/lib/node_modules/nodeunit/deps/async.js:123:13)
at /usr/local/lib/node_modules/nodeunit/deps/async.js:134:25
at /usr/local/lib/node_modules/nodeunit/deps/async.js:515:17


so I have added a null check to the file /usr/local/lib/node_modules/nodeunit/lib/core.js before of the line 285


//Added line
if(!group) return;

And once I have run nodeunit webserver/tests/ I didn't get the exception and I got the name of the file which is throwing the exception (so I have realised that my error was caused by a file (which is not a unit test) that I have created in the same folder of the unit tests :- ) )


core_auth_tokenmanager_tests
✔ core/auth/tokenManager - createNewTokenWithInvalidParameterTest1

testAppConfig

FAILURES: Undone tests (or their setups/teardowns):

- auth - cryptoCookieKey1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests