jestjs

Mock function with jest that contains a callback as an argument which returns a promise

血红的双手。 提交于 2021-01-29 07:47:42
问题 I'm integrating with a 3rd party script and have come across a scenario which I don't know how to test properly. The 3rd party script adds an object to the window with a few methods. The second argument of the create method is a callback function which returns a promise that is either rejected or resolved. I need to be able to mock that second argument with either a reject or resolved status so that my unit test can cover whether the UI is updated with either the error or success message. How

Mock function with jest that contains a callback as an argument which returns a promise

别来无恙 提交于 2021-01-29 07:32:36
问题 I'm integrating with a 3rd party script and have come across a scenario which I don't know how to test properly. The 3rd party script adds an object to the window with a few methods. The second argument of the create method is a callback function which returns a promise that is either rejected or resolved. I need to be able to mock that second argument with either a reject or resolved status so that my unit test can cover whether the UI is updated with either the error or success message. How

Jest test runs - Cannot find module error

梦想的初衷 提交于 2021-01-29 07:01:24
问题 I have been working on a React Typescript repo and have been running into an annoying issue where in jest is not able to resolve imports relative to root dir. Cannot find module '~lib/dates' from 'utils.ts' And this how the import looks like in the component / utils import { abc } from '~lib/dates'; // this fails to run If I change this to a relative path jest test runs works as expected import { abc } from '../../lib/dates'; // this runs as expected The same path work for some other

Jest test runs - Cannot find module error

ⅰ亾dé卋堺 提交于 2021-01-29 06:54:48
问题 I have been working on a React Typescript repo and have been running into an annoying issue where in jest is not able to resolve imports relative to root dir. Cannot find module '~lib/dates' from 'utils.ts' And this how the import looks like in the component / utils import { abc } from '~lib/dates'; // this fails to run If I change this to a relative path jest test runs works as expected import { abc } from '../../lib/dates'; // this runs as expected The same path work for some other

Jest mock method of base ES6 class (super method) when testing extended class

梦想的初衷 提交于 2021-01-29 06:44:42
问题 I am having issues when testing that the original method (from the base class), is called with some parameters, when testing an extended class. The class that I want to test is: // ApiDataSource.js import { RESTDataSource } from "apollo-datasource-rest"; export default class ApiDataSource extends RESTDataSource { constructor() { super(); this.baseURL = 'test'; } //We add the authorization params to the get method async get(path, params = {}, init = {}) { return super.get( path, { ...params,

Multiple server instance attempts while running jest

夙愿已清 提交于 2021-01-29 06:33:15
问题 Okay, So I've been writing tests for my node.js app using jest and supertest, for every test suite after the first, I'm getting an error Error: listen EADDRINUSE: address already in use :::3000 , I believe this is because it attempts to start the server on every test file (I have multiple test files *.test.js in /tests ) The top part before tests are described in each test file looks something like this const request = require("supertest"); const app = require("../index.js"); // the express

Jest not executing test case inside forEach function

落爺英雄遲暮 提交于 2021-01-29 05:27:30
问题 I am trying to execute test case in a loop inside a forEach block, but variable which i am iterating always throws undefined. In beforeAll block i am using await to get a data from database and I am assigning array of objects to a variable so that i can iterate the array and execute the test for each object. but when i try to do that it always throws TypeError: Cannot read property 'forEach' of undefined , can anyone say what i am doing wrong here? sample.test.ts describe('Test suite ', () =>

Mock 'fs' module in jest

你说的曾经没有我的故事 提交于 2021-01-29 04:13:39
问题 I'm trying to mock the fs module like so: jest.mock('fs'); And as seen in this post - Mock fs function with jest I should be able to treat the functions inside the module as jest.fn() and use - fs.existsSync.mockReturnValue(false); for instance. That however does not seem to work and typescript gives me a bunch of errors. All I want to do is assert a couple of functions like mkdirSync to have been called times or with some parameters and I seem to be running into this error - 'The "path"

How to mock a module with typescript namespace?

最后都变了- 提交于 2021-01-29 02:51:55
问题 This is the function under test: import * as firebase from 'firebase'; function signInWithGoogle() { var provider = new firebase.auth.GoogleAuthProvider(); firebase.auth().signInWithRedirect(provider); } firebase has below type definition: declare namespace firebase.auth and function auth(app?: firebase.app.App): firebase.auth.Auth; jest.mock('firebase', () => { // Don't know how to mock, what should be returned? }) I need mock firebase.auth.GoogleAuthProvider() , firebase.auth() and

Mock a specific class from a node module in jest

做~自己de王妃 提交于 2021-01-29 02:06:07
问题 I would like to mock just the Socket class from the net node module (Docs). I have a class that looks something like this... import { Socket } from 'net'; class Foo { protected socket: Socket; constructor() { this.socket = new Socket(); } connect() { const connPromise = new Promise<undefined>(resolve => { this.socket.connect(80, '192.168.1.1', () => { // Do some stuff with local state resolve(); }); }); return connPromise; } } I am unsure how to mock the Socket class so I can provide the mock