How to test a React component with RouteComponentProps?

To answer your last question, the recommended approach is to use <MemoryRouter>< *your component here* ></MemoryRouter> in your tests. Typescript does not pick up that this component will pass the required props to your component, as such I assume it not to be a type safe approach. This is for React Router v4 and doesn’t … Read more

How can I test stdin and stdout?

Use dependency injection. Coupling it with generics and monomorphism, you don’t lose any performance: use std::io::{self, BufRead, Write}; fn prompt<R, W>(mut reader: R, mut writer: W, question: &str) -> String where R: BufRead, W: Write, { write!(&mut writer, “{}”, question).expect(“Unable to write”); let mut s = String::new(); reader.read_line(&mut s).expect(“Unable to read”); s } #[test] fn … Read more

Testing asynchronous function with mocha

You have to specify the callback done as the argument to the function which is provided to mocha – in this case the it() function. Like so: describe(‘api’, function() { it(‘should load a user’, function(done) { // added “done” as parameter assert.doesNotThrow(function() { doRequest(options, function(res) { assert.equal(res, ‘{Object … }’); // will not fail assert.doesNotThrow … Read more

what is the best way to mock window.sessionStorage in jest

Here is the solution only use jestjs and typescript, nothing more. index.ts: export function getUserInfo() { const userInfo = window.sessionStorage.getItem(‘userInfo’); if (userInfo) { return JSON.parse(userInfo); } return {}; } index.spec.ts: import { getUserInfo } from ‘./’; const localStorageMock = (() => { let store = {}; return { getItem(key) { return store[key] || null; }, … Read more

EasyMock void method

You’re close. You just need to call the method on your mock before calling expectLastCall() So you expectation would look like this: userService.addUser(newUser1); EasyMock.expectLastCall(); EasyMock.replay(dbMapper); userService.addUser(newUser1); This works because the mock object is in Record mode before the call to replay(), so any calls to it will perform default behaviour (return null/do nothing) and will … Read more