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

Error with ava: TypeError: callback is not a function #204

Closed
omnidan opened this issue Apr 12, 2016 · 26 comments
Closed

Error with ava: TypeError: callback is not a function #204

omnidan opened this issue Apr 12, 2016 · 26 comments

Comments

@omnidan
Copy link

omnidan commented Apr 12, 2016

I tried the example from README.md, but with ES6:

import mongoose from 'mongoose'
import mockgoose from 'mockgoose'

mockgoose(mongoose).then(function () {
  mongoose.connect('mongodb://example.com/TestingDB', function (err) {
    console.error(err)
  })
}) 

However, this fails with the following error:

  TypeError: callback is not a function
    start (/Users/dan/Development/projects/touchlay/platform/node_modules/mongodb-prebuilt/index.js:79:4)
    Object.start_server (/Users/dan/Development/projects/touchlay/platform/node_modules/mongodb-prebuilt/index.js:65:16)
    /Users/dan/Development/projects/touchlay/platform/node_modules/mockgoose/Mockgoose.js:144:42
    next (/Users/dan/Development/projects/touchlay/platform/node_modules/rimraf/rimraf.js:74:7)
    CB (/Users/dan/Development/projects/touchlay/platform/node_modules/rimraf/rimraf.js:110:9)
    FSReqWrap.oncomplete (/Users/dan/Development/projects/touchlay/platform/node_modules/rimraf/rimraf.js:225:7)
@winfinit
Copy link
Collaborator

what version of node are you using and what OS?

@omnidan
Copy link
Author

omnidan commented Apr 12, 2016

@winfinit

Node.js v5.7.0
darwin 15.3.0 (latest OS X)

@winfinit
Copy link
Collaborator

help me out, when you say you are using it with es6, are you using babel, or you are passing some extra flags to node, or ...

because when i am running your example as is, I am getting:

(function (exports, require, module, __filename, __dirname) { import mongoose from 'mongoose';
                                                              ^^^^^^

SyntaxError: Unexpected token import
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:387:25)

@omnidan
Copy link
Author

omnidan commented Apr 14, 2016

@winfinit I keep forgetting node.js doesn't implement import yet 😅 - I use babel, you can run it with babel-node (that's what I do for testing purposes)

@winfinit
Copy link
Collaborator

@omnidan I am unable to duplicate this issue

here was my setup:

npm install --save-dev babel-cli
npm install babel-preset-es2015 --save-dev
echo '{ "presets": ["es2015"] }' > .babelrc

content of a bug/204.es6:

"use strict"

import mongoose from 'mongoose'
import mockgoose from '../mockgoose'

mockgoose(mongoose).then(function () {
  mongoose.connect('mongodb://example.com/TestingDB', function (err) {
    console.log("err:", err);
    console.log("done");
    mongoose.disconnect();
  })
}) 

running bug/204.es6 script:

$ time node_modules/babel-cli/bin/babel-node.js bugs/204.es6
err: undefined
done

real    0m1.013s
user    0m0.924s
sys 0m0.118s

my system:

$ uname -a
Darwin foo-bar.local 15.4.0 Darwin Kernel Version 15.4.0: Fri Feb 26 22:08:05 PST 2016; root:xnu-3248.40.184~3/RELEASE_X86_64 x86_64
$ node_modules/babel-cli/bin/babel-node.js --version
6.7.5
$ node -v
v5.10.1

@omnidan
Copy link
Author

omnidan commented Apr 17, 2016

I've been sitting for hours at this issue and I still have no idea why this is failing in my environment. Might have to do with ava or supertest but honestly I don't have a clue at all...

@omnidan
Copy link
Author

omnidan commented Apr 17, 2016

I finally managed to get it to work, this is how Mockgoose works fine with ava:

import test from 'ava'
import mongoose from 'mongoose'
import mockgoose from 'mockgoose'

test.before(t => mockgoose(mongoose))
test.afterEach(t => mockgoose.reset(err => {
  if (err) t.fail(err)
}))

@omnidan omnidan closed this as completed Apr 17, 2016
@omnidan omnidan reopened this Apr 17, 2016
@omnidan omnidan changed the title Error with ES6: TypeError: callback is not a function Error with ava: TypeError: callback is not a function Apr 17, 2016
@omnidan
Copy link
Author

omnidan commented Apr 17, 2016

Actually this seems to be an issue when using Mockgoose with the test runner ava, it runs tests in parallel which I think mockgoose doesn't like 😅 It works fine with one test file for me, but when I add another file it fails with the same issue again...

@winfinit
Copy link
Collaborator

winfinit commented Apr 18, 2016

@omnidan thank you for this bug report and for your time pinpointing this issue. I will set myself up with some tests using ava, and will post updates in this issue.

@AbrahamAlcaina
Copy link

AbrahamAlcaina commented Apr 18, 2016

I have the same issue.

import test from 'ava';
import sut from './server';
import request from 'supertest-as-promised';
import mongoose from 'mongoose';
import mockgoose from 'mockgoose';

test.before(async t => {
    console.log('1');
    await mockgoose(mongoose);
    console.log('2');
    const Subscription = mongoose.model('Subscription');
    await Subscription.create({ id: 'found' })
});

test('send message to an existent subscription', async t => {
    const res = await request(sut).post('/api/found/message');
    t.is(res.status, 200);
});

test.after(t => mockgoose.reset(err => {
  if (err) t.fail(err)
}));

Only the 1 is printed

server.js

... 
app.get('/api/:subscription/message', async (req, res) => {
  const subscription = await Subscription.findOne({ id: req.params.subscription });
  if (subscription) {
    return res.send(subscription);
  }
  return res.status(404);
});

@jadamgreen1010-zz
Copy link

Are we sure about this?

I think the issue is here: https://github.com/mccormicka/Mockgoose/blob/master/Mockgoose.js#L144

mongodb-prebuilt takes a callback and doesn't check before trying to call it. If you don't pass a callback to mongod.start_server() then it errors like that.
https://github.com/winfinit/mongodb-prebuilt/blob/125940e0872c69d49d777218284bbedca273c66c/index.js#L35-L67

If you agree I can PR

@omnidan
Copy link
Author

omnidan commented Apr 22, 2016

@jadamgreen1010 Thanks for the hint! Manually adding a callback to start_server has fixed the issue for me. I hope a new version with this fix gets released soon 😁

@jadamgreen1010-zz
Copy link

@omnidan You're welcome :)

I hope so too, although I think it's something that could (maybe should) be fixed on both sides - mongodb-prebuilt could look before it leaps with that callback so we wouldn't have to pass a dummy function just to keep it from erroring.

@omnidan
Copy link
Author

omnidan commented Apr 23, 2016

@jadamgreen1010 it probably makes sense to add the callback either way (at least to check for errors) 😉

@jadamgreen1010-zz
Copy link

@omnidan I'd agree, but it seems (if my memory serves me right) that the cb is only being called when there's an error and not called with success. It looks like the 'startResult' code that start_server returns is the only way handle any case, so the cb would be redundant.

It seems to me...
Simple solution: dummy callback to start_server.
Best solution: Fix start_server in mongodb-prebuilt to always use the callback or returned code or at least allow both.

@jadamgreen1010-zz
Copy link

@winfinit @omnidan I added a hotfix dummy function w/ comments: #208

@pataltechnologies
Copy link

Any idea if this hotfix will be implemented? I've hit a bit of a roadblock with a project that it fixes for me.

@pibi
Copy link
Contributor

pibi commented May 9, 2016

@jadamgreen1010 @SNeugber @winfinit , not sure why you are using a hacky dummy function instead of a proper check. Since mongodb-prebuilt return error or exit codes !==0 as first argument on the callback, we can embed the code for status check right into the callback.

Please check #212

@pibi
Copy link
Contributor

pibi commented May 10, 2016

Ooops, sorry guys, I see the issues now. I made mongodb-js/mongodb-prebuilt#27 to solve the issue definitely.

@mindmelting
Copy link

Just as an FYI, you can get around this issue temporarily by running ava in serial mode --serial

@spearmootz
Copy link

spearmootz commented Jul 7, 2016

this is pretty old so just wanted to confirm this is an issue. im testing with mocha with no success. i looked at the changes in mongodb-js/mongodb-prebuilt#27 and manually put it in the module with no success either

@thomas-lee
Copy link

I am new to mockgoose and using es2015. I don't know if it is a good place to ask question here.

I have already used babel-register and run with mocha.

If i am using es6 import, it will load the app before mocking the mongoose. So i can't mock the mongo. Any solutions?

I can only mock like this in order to mock before connect the db and use memory rather mongo

import mongoose from 'mongoose';
import mockgoose from 'mockgoose';
import request from 'supertest';
import {expect} from 'chai';
// import app from '../../app';
mockgoose(mongoose);
var app = require('../../app');

Thanks

@omnidan
Copy link
Author

omnidan commented Jul 11, 2016

I am still having this issue, even with ava --serial:

  TypeError: callback is not a function
    start (/Users/dan/Development/projects/touchlay/platform/node_modules/mongodb-prebuilt/index.js:80:13)
    Object.start_server (/Users/dan/Development/projects/touchlay/platform/node_modules/mongodb-prebuilt/index.js:66:16)
    /Users/dan/Development/projects/touchlay/platform/node_modules/mockgoose/Mockgoose.js:144:42

@alexfoxy
Copy link

I'm still getting this issue with my tape tests. I can run the tests one at a time but running more than one gives me:

callback(child.status);
            ^

TypeError: callback is not a function
    at start (/Users/alexfox/amplify/noiz-notifier/node_modules/mongodb-prebuilt/index.js:80:13)
    at Object.start_server (/Users/alexfox/amplify/noiz-notifier/node_modules/mongodb-prebuilt/index.js:66:16)
    at /Users/alexfox/amplify/noiz-notifier/node_modules/mockgoose/Mockgoose.js:144:42
    at next (/Users/alexfox/amplify/noiz-notifier/node_modules/rimraf/rimraf.js:74:7)
    at FSReqWrap.CB [as oncomplete] (/Users/alexfox/amplify/noiz-notifier/node_modules/rimraf/rimraf.js:110:9)

Using mockgoose v6.0.3 & mongoose v4.5.4

@ismatim
Copy link

ismatim commented Feb 11, 2017

I moved back to mocha due this bug. I got here from MERN boiler plate. Got this same error when running npm run test.

@winfinit
Copy link
Collaborator

winfinit commented Mar 1, 2017

Hello, I am not monitoring this project anymore, please resubmit it under mockgoose/mockgoose if this is still an issue with new 7.x release

@winfinit winfinit closed this as completed Mar 1, 2017
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