jestjs

Nest JS - Issue writing Jest Test Case for a function returning Observable Axios Response

只愿长相守 提交于 2021-01-05 06:18:04
问题 I am fairly new to NestJS + Typescript + RxJs tech stack. I am trying to write a unit test case using Jest for one of my functions but not sure if doing it correctly. component.service.ts public fetchComponents(queryParams) { const url = this.prepareUrl(queryParams); const data$ = this.httpService.get(url); return data$ .pipe(map(({ data }) => data)); } component.sevice.spec.ts Test case works and passes describe('fetchComponents', () => { const query = { limit: 10, offset: 0 }; const result:

Jest encountered an unexpected token with react-native

十年热恋 提交于 2021-01-02 06:02:10
问题 So I'm trying to write tests on my React Native app with Jest and TypeScript. While using old babel version everything worked fine, but because of some project problems we had to upgrade babel to 7.0.0. After that I couldn't make it work. Any help is appreciated Jest encountered an unexpected token This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript. By default, if Jest sees a Babel config, it will use that to transform your files,

How to mock react custom hook returned value?

爱⌒轻易说出口 提交于 2020-12-31 06:23:10
问题 Here is my custom hook: export function useClientRect() { const [scrollH, setScrollH] = useState(0); const [clientH, setClientH] = useState(0); const ref = useCallback(node => { if (node !== null) { setScrollH(node.scrollHeight); setClientH(node.clientHeight); } }, []); return [scrollH, clientH, ref]; } } I want each time that it is called, it return my values. like: jest.mock('useClientRect', () => [300, 200, () => {}]); How can I achieve this? 回答1: Load the hook as a module. Then mock the

Where to put common data in jest tests

孤街醉人 提交于 2020-12-30 04:51:20
问题 I am not sure how to organize code for jest testing. I have all my tests under __tests__ and all my mocks under __mocks__ . Now I have some data I want to share between tests: they are not a mock of an existing function, they are just some javascript object I'd like to use in different files. Should I create a __data__ directory? Or put them under __mocks__ anyway? Or in the __tests__ directory without putting -test in the file name? 回答1: The short answer is anywhere you want. JavaScript has

How to write Jest transformIgnorePatterns

老子叫甜甜 提交于 2020-12-29 09:18:27
问题 I have this excpetion Jest encountered an unexpected token This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript. By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules". Here's what you can do: • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config. • If you need a custom transformation specify a "transform" option in

How to fix Error: Not implemented: navigation (except hash changes)

橙三吉。 提交于 2020-12-29 09:16:26
问题 I am implementing unit test for a file that contain window.location.href and I need to check it. My jest version is 22.0.4 . Everything is fine when I run my test on node version >=10 But I get this error when I run it on v8.9.3 console.error node_modules/jsdom/lib/jsdom/virtual-console.js:29 Error: Not implemented: navigation (except hash changes) I have no idea about it. I have searched on many page to find out the solution or any hint about this to figure out what happened here. [UPDATE] -

“ReferenceError: document is not defined” when trying to test a create-react-app project

醉酒当歌 提交于 2020-12-29 08:50:08
问题 This is my __tests__/App.js file: import React from 'react'; import ReactDOM from 'react-dom'; import App from '../src/containers/App'; it('renders without crashing', () => { const div = document.createElement('div'); ReactDOM.render(<App />, div); }); // sanity check it('one is one', () => { expect(1).toEqual(1) }); And this is the output I get when running yarn test : FAIL __tests__/App.js ● renders without crashing ReferenceError: document is not defined at Object.<anonymous>.it (__tests__

Jest: tests can't fail within setImmediate or process.nextTick callback

对着背影说爱祢 提交于 2020-12-29 06:28:59
问题 I'm trying to write a test for a React component that needs to complete an asynchronous action in its componentWillMount method. componentWillMount calls a function, passed as a prop, which returns a promise, and I mock this function in my test. This works fine, but if a test fails in a call to setImmediate or process.nextTick , the exception isn't handled by Jest and it exits prematurely. Below, you can see I even try to catch this exception, to no avail. How can I use something like

Jest: tests can't fail within setImmediate or process.nextTick callback

跟風遠走 提交于 2020-12-29 06:27:08
问题 I'm trying to write a test for a React component that needs to complete an asynchronous action in its componentWillMount method. componentWillMount calls a function, passed as a prop, which returns a promise, and I mock this function in my test. This works fine, but if a test fails in a call to setImmediate or process.nextTick , the exception isn't handled by Jest and it exits prematurely. Below, you can see I even try to catch this exception, to no avail. How can I use something like

Jest beforeAll() share between multiple test files

纵饮孤独 提交于 2020-12-29 02:42:48
问题 I have a Node.js project that I'm testing using Jest. I have several test files that have the same setup requirement. Previously, all these tests were in one file, so I just had a beforeAll(...) that performed the common setup. Now, with the tests split into multiple files, it seems like I have to copy/paste that beforeAll(...) code into each of the files. That seems inelegant - is there a better way to do this, ideally where I can just write my beforeAll(...) /setup logic once, and "require"