I want to test my code with Jest and when I call my function in code get a error in this.$axios
TypeError: this.$axios is not a function
How can I fix it with mock or other way?
Related
I'm trying to figure out how to correctly mock the #mailchimp/mailchimp_transactional npm module in my tests.
I'm getting caught up in how it shows to import mailchimp from the documentation, with an immediate call to the constructor with the API key
const mailchimp = require('#mailchimp/mailchimp_transactional')(API_KEY);
According to the Jest documentation, I'd normally mock it with something like
jest.mock('#mailchimp/mailchimp_transactional');
But I'm not sure how to then create the instance with a fake API key? I keep getting an undefined result from trying to call the mocked version of mailchimp. Would I need to mock a constructor() function as well?
I think it should be something like this:
jest.mock(
'#mailchimp/mailchimp_transactional',
() => API_KEY => ({ object that mocks mailchimp })
);
I am new to NestJS and have written a basic unit test for my service/controller using an example from a couple of sites including the official NestJS website and even before I try to run this code,
it('should be defined', async () => {
const result:MyEntity[] = [{"test1":"value1"},{"test1":"value2"}];
jest.spyOn(service, 'findAll').mockImplementation(() => result);
expect(await controller.findAll()).toBe(result);
});
I see the following error.
Type 'MyEntity[]' is missing the following properties from type 'Promise<MyEntity[]>': then, catch, [Symbol.toStringTag], finallyts(2739)
index.d.ts(1163, 33): The expected type comes from the return type of this signature.
I am returning a Promise from my controller but somehow the compiler is expecting a try catch finally somewhere but I don't know where this should go.
If service.findAll normally returns a promise, you should make the mock return a promise as well. You can do this with jest.spyOn(service, 'findAll').mockResolvedValue(result). Now in your test you can do expect(controller.findAll()).resovles.toEqual(result) to make the method resolve properly and test the result.
I'm getting the following error when running Jest tests:
/Users/hugo/src/feedback/www/assets/app/node_modules/superagent/lib/node/index.js: /Users/hugo/src/feedback/www/assets/app/node_modules/superagent/node_modules/debug/node.js: Cannot read property 'buffer' of undefined
If Jest automatically mocks out dependencies, shouldn't any file that requires superagent just get the mock of superagent? All the functions I have that even make an http request using it have been mocked out to just return test data without making the request. I don't understand why I'm getting the error.
UPDATE
I tried the manual mock found here. It still gives me the same error.
I am new in angular JS
I want test angular-seed sample, e2e test run ok, but i can not test unit test (directive, ...), i get this sample from github, and not change this, but have this problem
i get this error in firebug : ReferenceError: module is not defined
i seen this link but not resolve my problem
Testing Angular Service gives error: No module: ngResource
please help, thanks
Publish your code snippet. That might help me getting the issue.
Anyway, you can instantiate your module in beforeEach method,
beforeEach(function(){module("myApp"); // myApp is your module});
This should be defined before injecting controller. Hope, this might help
I'm just getting started testing my Backbone app with Sinon and Jasmine. I have a view that look something like (coffeescript):
initialize: ->
#collection.on 'reset', #render, this
render: ->
if #collection.fetched
# do stuff
else
#$el.append "<h3>Loading...</h3>"
#collection.fetch()
this
I want to test this with an unfetched collection, but I'm not sure how to fake an ajax call within my code (obviously can easily be done in the spec). I realize that I could just pass in a pre-fetched collection, but I'm curious -- is it possible with Sinon to override the fetch function to return a fake response?
Thank you for any help.
Under the hood, Backbone uses jQuery's $.ajax method, so you can stub that out. We use this to catch accidental calls in our Jasmine specs:
$.ajax = -> throw "ajaxShouldBeStubbedOutError: #{JSON.stringify arguments}"
And then you can stub over that if you want to fake an AJAX call and its response:
spyOn($,'ajax').andCallFake (options) =>
if options.url is "/correct"
options.success {"data":"yay"}