Closed bjfletcher closed 9 years ago
your example.js
file doesn't seem to be exporting anything. i may be wrong, but shouldn't you have a module.exports
statement or some other way of exporting the object to be consumed in your var e = require('../example');
from within your example-test.js
?
Hi @awei01, appreciate your time/effort on this, really do. This issue was happening in a bigger, more complicated AngularJS program, and definitely not because of the exports. I quickly cut it down to this test case and as you saw it was a bad job. I'll go back to the AngularJS program and see if I can put together a better test case. I'll close this and re-open if I get around to doing this. Thanks. :)
sure, no prob. if you reopen this issue, i should get a github alert. i'll check it out if/when you update it. maybe i can help you track it down.
@awei01 You're very kind. :) I have just gone and made a better test case. Please pull the latest from:
Sure, no problem. So, I'm not quite sure what your Authenticator.js
module is trying to do, but in its current state, you're just exporting an empty object. I'll just make some assumptions and try to point you in the right direction. I haven't looked at the body-parser
module in depth and have no idea what you're trying to implement, but hopefully my fictitious example will be of some help to get you started. Keep in mind following code is untested and uses patterns that I generally use, but may not be the ideal implementation for your scenario.
// lib/Authenticator.js
var BodyParser = require('body-parser');
module.exports = {
doSomethingOnApp: function(app) {
app.use(BodyParser.json());
return app.isAuthenticated();
}
}
// __tests__/Authenticator.spec.js
jest.dontMock('../lib/Authenticator');
describe('Authenticator module', function() {
var BodyParser, Authenticator;
beforeEach(function() {
BodyParser = require('body-parser');
Authenticator = require('../lib/Authenticator');
});
it('when .doSomethingOnApp() called with app, should call .use() on app with BodyParser.json(), call .isAuthenticated() on app and return its result', function() {
BodyParser.json.mockReturnValue('json response');
var fakeApp = { use: jest.genMockFn(), isAuthenticated: jest.genMockFn().mockReturnValue('foo') };
var result = Authenticator.doSomethingOnApp(fakeApp);
expect(BodyParser.json).toBeCalled();
expect(fakeApp.use).lastCalledWith('json response');
expect(fakeApp.isAuthenticated).lastCalledWith();
expect(result).toBe('foo');
});
});
Hi @awei01, with that example I get the following which looks like a more elaborate error than the earlier one:
[snipped]
Using Jest CLI v0.2.1
FAIL __tests__/Authenticator.js (0.051s)
● Authenticator module › it when .doSomethingOnApp() called with app, should call .use() on app with BodyParser.json(), call .isAuthenticated() on app and return its result
- TypeError: /Users/ben/projects/jest-issue/node_modules/body-parser/index.js: Cannot call method 'function' of undefined
at /Users/ben/projects/jest-issue/node_modules/body-parser/index.js:29:46
[snipped]
If you npm install
you will install the body-parser
which is a component used for ExpressJS apps.
The reason Authenticator.js
is empty in my test case is because this body-parser
I noticed was the problem. :/ If I cut out all the irrelevant code, it comes down to this module.
I'm hoping that there's something we can fix on the Jest
side rather than on the body-parser
side - although admittedly body-parser
really shouldn't be using function
as a method name.
Ok now i'm following. I created a PR on your repo with some possible fixes.
Thank you for reporting this issue and appreciate your patience. We've notified the core team for an update on this issue. We're looking for a response within the next 30 days or the issue may be closed.
This seems like it was resolved.
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.
I'm experiencing an issue with Jest and here is a simple test case that I have made.
You can also clone this simple test case from here: https://github.com/bjfletcher/jest-issue
EDITED
(Old test case removed. Please see the aforementioned repo for a better test case.)
Error: