jestjs

How to test class constructor in Jest

|▌冷眼眸甩不掉的悲伤 提交于 2021-01-22 04:29:28
问题 Let's say I have a class like following: class SomeClass { constructor(a, b) { this.a = a; this.b = b; } } How can I test through Jest that constructor was initialized the right way? Say... this.a = a and this.b = b and not vice versa? I know that I can execute toBeCalledWith but that won't let me check the constructor's logic. I was also thinking about making mockImplementation but in this case it seems pointless as I will rewrite the logic, or I may not be aware of all the nuances of

How to use Mocha and Jest with TypeScript without conflicts?

时光总嘲笑我的痴心妄想 提交于 2021-01-21 07:04:50
问题 I'm trying to install Mocha and Jest with types on one project. We use strict typecheck, so I get errors related to conflicting globals type. I've tried to create ambiguous module declaration, defining only Mocha in types at tsconfig . I've been trying to remove Jest's declaration - but that would partially help. Disabling strict typecheck or lib check is not an option. I expected to work it properly, but instead got the next errors. node_modules/@types/jest/index.d.ts(29,13): error TS2403:

How to use Mocha and Jest with TypeScript without conflicts?

筅森魡賤 提交于 2021-01-21 07:03:11
问题 I'm trying to install Mocha and Jest with types on one project. We use strict typecheck, so I get errors related to conflicting globals type. I've tried to create ambiguous module declaration, defining only Mocha in types at tsconfig . I've been trying to remove Jest's declaration - but that would partially help. Disabling strict typecheck or lib check is not an option. I expected to work it properly, but instead got the next errors. node_modules/@types/jest/index.d.ts(29,13): error TS2403:

How to test a unhandledRejection / uncaughtException handler with jest

送分小仙女□ 提交于 2021-01-21 03:52:31
问题 I have handlers for unhandledRejection s and uncaughtException s: bin.js ['unhandledRejection', 'uncaughtException'].forEach(event => { process.on(event, err => logger.error(err)); }); Now I want to test them with jest : bin.test.js const bin = require('../bin'); test('catches unhandled rejections', async () => { const error = new Error('mock error'); await Promise.reject(error); expect(logger.error).toHaveBeenCalledWith(error); }); test('catches uncaught exceptions', () => { const error =

unit test raises error because of .getContext() is not implemented

人盡茶涼 提交于 2021-01-20 18:59:47
问题 I am writing tests using Jest for components that use canvas elements. I keep getting an error when I run my tests that looks like this. Error: Not implemented: HTMLCanvasElement.prototype.getContext (without installing the canvas npm package) From my understanding Jest uses jsdom for its testing and that jsdom is compatible with canvas if you install the canvas or canvas-prebuilt packages. I have tried installing each of these packages and neither of them have resolved the error. The only

unit test raises error because of .getContext() is not implemented

梦想的初衷 提交于 2021-01-20 18:50:27
问题 I am writing tests using Jest for components that use canvas elements. I keep getting an error when I run my tests that looks like this. Error: Not implemented: HTMLCanvasElement.prototype.getContext (without installing the canvas npm package) From my understanding Jest uses jsdom for its testing and that jsdom is compatible with canvas if you install the canvas or canvas-prebuilt packages. I have tried installing each of these packages and neither of them have resolved the error. The only

Recommended approach for route-based tests within routes of react-router

谁都会走 提交于 2021-01-20 12:33:25
问题 I'm using react-testing-library within a project of mine and am trying to write tests that validate in-app routing. e.g. testing that a button on the AccessDenied page brings you back to the Home page. I've been able to write these sorts of tests successfully for my App component because it defines all of the app routes. But if AccessDenied is one of those routes , how do I need to set up my tests to validate a button clicked there will route my back to Home? Here is a contrived example: App

Recommended approach for route-based tests within routes of react-router

和自甴很熟 提交于 2021-01-20 12:30:08
问题 I'm using react-testing-library within a project of mine and am trying to write tests that validate in-app routing. e.g. testing that a button on the AccessDenied page brings you back to the Home page. I've been able to write these sorts of tests successfully for my App component because it defines all of the app routes. But if AccessDenied is one of those routes , how do I need to set up my tests to validate a button clicked there will route my back to Home? Here is a contrived example: App

How do I compare two collections in Jest ignoring element order?

喜你入骨 提交于 2021-01-20 04:17:27
问题 When writing a unit test in Jest, how can I test that an array contains exactly the expected values in any order ? In Chai , I can write: const value = [1, 2, 3]; expect(value).to.have.members([2, 1, 3]); What's the equivalent syntax in Jest? 回答1: Another way is to use the custom matcher .toIncludeSameMembers() from jest-community/jest-extended. Example given from the README test('passes when arrays match in a different order', () => { expect([1, 2, 3]).toIncludeSameMembers([3, 1, 2]); expect

How do I compare two collections in Jest ignoring element order?

≯℡__Kan透↙ 提交于 2021-01-20 04:15:57
问题 When writing a unit test in Jest, how can I test that an array contains exactly the expected values in any order ? In Chai , I can write: const value = [1, 2, 3]; expect(value).to.have.members([2, 1, 3]); What's the equivalent syntax in Jest? 回答1: Another way is to use the custom matcher .toIncludeSameMembers() from jest-community/jest-extended. Example given from the README test('passes when arrays match in a different order', () => { expect([1, 2, 3]).toIncludeSameMembers([3, 1, 2]); expect