How to write test case for vscode.window.showInformationMessage()? - node.js

in vs code extension, I need to write test case that on execution of the command, the information message display and the text on write assetion for the information message but unable to see anywhere how to do that.
here is the extension code
export function activate(context: vscode.ExtensionContext) {
console.log('Congratulations, your extension "css-color-collector" is now active!');
const command = 'css-color-collector.init';
const commandHandler = () => {
vscode.window.showInformationMessage('Hello World from css color collector!');
};
let disposableInit = vscode.commands.registerCommand(command, commandHandler);
context.subscriptions.push(disposableInit)
}
and here I want to write test case for above
import * as assert from 'assert';
import * as vscode from 'vscode';
suite('Extension Test Suite', () => {
suite('extension setup', () => {
test('display message on init command', () => {
vscode.commands.executeCommand('css-color-collector.init');
// How to validate the message content here?
});
});
});

You can pass callback returning your text into the commnad this way:
const commandHandler = (callback: (t: string) => void) =>
{
const text = 'Hello World from css color collector!';
vscode.window.showInformationMessage(text);
callback(text);
};
and then get it:
vscode.commands.executeCommand('css-color-collector.init', t =>
{
console.log(t);
});
another way is returning your text from command:
const commandHandler = async () =>
{
const text = 'Hello World from css color collector!';
// put 'await' before this if you want wait for message been closed
vscode.window.showInformationMessage(text);
return text;
};
// and then
const result = await vscode.commands.executeCommand('css-color-collector.init');
console.log(result);

Related

how to mock react-query useQuery in jest

I'm trying to mock out axios that is inside an async function that is being wrapped in useQuery:
import { useQuery, QueryKey } from 'react-query'
export const fetchWithAxios = async () => {
...
...
...
const response = await someAxiosCall()
...
return data
}
export const useFetchWithQuery = () => useQuery(key, fetchWithAxios, {
refetchInterval: false,
refetchOnReconnect: true,
refetchOnWindowFocus: true,
retry: 1,
})
and I want to use moxios
moxios.stubRequest('/some-url', {
status: 200,
response: fakeInputData,
})
useFetchWithQuery()
moxios.wait(function () {
done()
})
but I'm getting all sorts of issues with missing context, store, etc which I'm iterested in mocking out completely.
Don't mock useQuery, mock Axios!
The pattern you should follow in order to test your usages of useQuery should look something like this:
const fetchWithAxios = (axios, ...parameters) => {
const data = axios.someAxiosCall(parameters);
return data;
}
export const useFetchWithQuery = (...parameters) => {
const axios = useAxios();
return useQuery(key, fetchWithAxios(axios, ...parameters), {
// options
})
}
Where does useAxios come from? You need to write a context to pass an axios instance through the application.
This will allow your tests to look something like this in the end:
const { result, waitFor, waitForNextUpdate } = renderHook(() => useFetchWithQuery(..., {
wrapper: makeWrapper(withQueryClient, withAxios(mockedAxios)),
});
await waitFor(() => expect(result.current.isFetching).toBeFalsy());

jest.spyOn not calling mocked implementation but rather actual function instead

I'm trying to write a unit test for a function that calls some helper functions in the same file. I'm using jest.spyOn to mock away those helper functions as it appears that it can do that.
myModule.js
export const getUserName = () => {
return "mjordan"
}
export const getItem = () => {
return 'basketball'
}
export const getUserWithItem = () => {
const userName = getUserName()
const item = getItem()
return userName + " " + item
}
myModule.test.js
import * as myModule from 'path/to/module'
describe('getUserWithItem', () => {
beforeEach(() => {
jest.restoreAllMocks()
})
it('Returns user with item', () => {
jest.spyOn(myModule, 'getUserName').mockImplementation(() => 'tigerwoods')
jest.spyOn(myModule, 'getItem').mockImplementation(() => 'golf ball')
const result = myModule.getUserWithItem()
expect(result).toEqual("tigerwoods golf ball")
})
})
However, the jest.spyOn function does not appear to be mocking the implementation of the spied on functions but the test is calling the original functions instead which results in an output of mjordan basketball instead.
Am I missing something with how spyOn is supposed to work?
The easiest way I have found to do what you want is to explicitly call the exported version of the function in your module, i.e.
export const getUserName = () => {
return "mjordan"
}
export const getItem = () => {
return 'basketball'
}
export const getUserWithItem = () => {
const userName = exports.getUserName()
const item = exports.getItem()
return userName + " " + item
}

How to cover arrow functions in unit testing in angular?

this.service = () => { -- statements -- }
The above statement is to be tested using jasmine unit testing in angular.
Can i get some suggestions for that ?
it("should service call",()=>{ // i want to call the arrow function here like component.service.? what to use in place of '?'. })
It is a function so you call it immediately:
example:
interface Service {
fun: () => string;
}
class Component {
constructor() {
this.service = () => {
return {
fun: () => 'hello'
};
};
}
service: () => Service;
}
Calling it:
const component = new Component();
const service = component.service();
const message = service.fun();
// or in one line:
const message = new Component().service().fun();
Typescript playground example

mock a toPromise function in jest - got .toPromise is not a function

I have an httpService from nestjs/common
and I am using like the following:
const response = await this.httpService.post(`${this.api}/${action}`, data).toPromise();
and in my jest spec file ( unit testing) . i am trying to mock this service
httpServiceMock = {
post: jest.fn()
};
it('should start', async () => {
const serviceResult = await service.start(data);
});
and I have got this error :
TypeError: this.httpService.post(...).toPromise is not a function
I am also trying to add a promise result like :
const promise = Promise.resolve('result');
httpServiceMock.post.mockResolvedValue(promise);
tried also :
it('should start', async () => {
const mockObservable = Promise.resolve({
toPromise: () => {
console.log('toPromise called');
}
})
httpServiceMock.post.mockImplementation(() => mockObservable);
const serviceResult = await service.start();
});
My question is how can I mock the promise and return a response or exception
The return value httpService.post needs to return an Observable<AxiosResponse<T>> which includes a property toPromise, which is a function. Your mock returns a resolved promise, whereas it needs to return a fake Observable.
The Observable is returned immediately, so the post implementation can just return a raw value, but the toPromise needs to return a promise.
Return the correct shaped object to get rid of this error:
const mockObservable = {
toPromise: () => Promise.resolve('result')
}
httpServiceMock.post.mockImplementation(() => mockObservable);
I had a similar problem that could not be solved by accepted answer. So I bring here another solution just in case it could help someone else.
If you have jasmine, just use jasmine.createSpyObj(). If not, here is what I needed to do :
First, I implemented a jasmine.createSpyObj() equivalent (based on this answer with little modifications) :
export class TestUtilsService {
static createSpyObj (baseName:string, methodNames:string[]): SpyObject {
let obj: any = {};
for (let i = 0; i < methodNames.length; i++) {
obj[methodNames[i]] = jest.fn();
}
return {[baseName]:()=>obj};
};
}
export class SpyObject {
[key: string]: ()=>{[key:string]:jest.Mock} ;
}
Then I used it in my unit test :
const spyHttpClient: SpyObject = TestUtilsService.createSpyObj('get',['toPromise']);
Add it in test module providers :
{provide: HttpClient, useValue: spyHttpClient}
Finally, mock the toPromise implementation in order to return a mocked response :
const mockedResponse = {...};
spyHttpClient.get().toPromise.mockImplementationOnce(()=>Promise.resolve(mockedResponse));
await service.myRealMethodThatCallsHttpClient();
expect(service.someUpdatedProp).toBeTruthy();
Please notice parenthesis after method get.
"A Jar of Clays" solution didn't work for me (I got mockImplementation is not a function), but this worked:
const mockPromise = {
toPromise: () => Promise.resolve(ical)
}
mockHttpService.get = jest.fn( () => {return mockPromise});

testing an actual returning value on javascript using jest

I have the following js function:
const modelUtils = {
modelingObj(obj, stringVal = ‘RE’) {
let newObj;
//my logic for setting value of newObj
return newObj;
};
export default modelUtils;
I want to test and see that based on a specific params I get a particular result, the issue is I’m always returning back an empty object.
Test.js
import modelUtils from '../modelUtils';
jest.unmock('../modelUtils');
describe(' testing modelUtils', () => {
let test;
const mockData = {
myProperty: [],
};
describe('testing modelingObj function', () => {
it('For my first test’, () => {
test = modelUtils.mockData, ‘TR’);
expect(test).toEqual({ myProperty: [] });
});
});
});

Resources