jestjs

Test if a component is rendered with the right props with react-testing-library

时光毁灭记忆、已成空白 提交于 2020-12-27 09:00:08
问题 I have some components that are rendering another component (FetchNextPageButton) that is already tested in isolation, like these ones: const News = () => ( <div> <h1>News</h1> ... <FetchNextPageButton query={NEWS_QUERY} path="viewer.news" /> </div> ) const Jobs = () => ( <div> <h1>Jobs</h1> ... <FetchNextPageButton query={JOBS_QUERY} path="viewer.jobs" /> </div> ) const Posts = () => ( <div> <h1>Posts</h1> ... <FetchNextPageButton query={POSTS_QUERY} path="viewer.posts" /> </div> ) The thing

How do I test a jest console.log

 ̄綄美尐妖づ 提交于 2020-12-27 08:51:26
问题 I'm using create-react-app and trying to write a jest test that checks the output of a console.log . My function to test is: export const log = logMsg => console.log(logMsg); My test is : it('console.log the text "hello"', () => { console.log = jest.fn('hello'); expect(logMsg).toBe('hello'); }); Here is my error FAIL src/utils/general.test.js ● console.log the text hello expect(received).toBe(expected) Expected value to be (using ===): "hello" Received: undefined Difference: Comparing two

How do you test for the non-existence of an element using jest and react-testing-library?

こ雲淡風輕ζ 提交于 2020-12-27 07:50:30
问题 I have a component library that I'm writing unit tests for using Jest and react-testing-library. Based on certain props or events I want to verify that certain elements aren't being rendered. getByText , getByTestId , etc throw and error in react-testing-library if the element isn't found causing the test to fail before the expect function fires. How do you test for something not existing in jest using react-testing-library? 回答1: From DOM Testing-library Docs - Appearance and Disappearance

Testing TextInput Component in react-native

a 夏天 提交于 2020-12-26 08:23:22
问题 I have some problems with testing TextInput changes in react-native with jest and enzyme . My component that handles user input basically looks like this (simplified): class Search extends React.PureComponent { onSearchTextChange = input => { // do something awesome }; render() { return ( <View> <TextInput onChangeText={debounce(this.onSearchTextChange, 800)} /> </View> ); } } I want to test the text input behaviour. This is what the test looks like right now: it('should render a text input

Testing TextInput Component in react-native

那年仲夏 提交于 2020-12-26 08:22:30
问题 I have some problems with testing TextInput changes in react-native with jest and enzyme . My component that handles user input basically looks like this (simplified): class Search extends React.PureComponent { onSearchTextChange = input => { // do something awesome }; render() { return ( <View> <TextInput onChangeText={debounce(this.onSearchTextChange, 800)} /> </View> ); } } I want to test the text input behaviour. This is what the test looks like right now: it('should render a text input

Mock a library in jest with Typescript

半城伤御伤魂 提交于 2020-12-16 05:24:19
问题 I want to mock a GCP bucket but Typescript is yelling because of typings. Here's an extract of a Class I want to test: private storage = new Storage({ projectId: 'PROJECT_NAME', keyFilename: env.gcpKeyFilename, }); get bucket() { return this.storage.bucket('fundee-assets'); } private async _downloadFromBucket(name) { const file = this.bucket.file(`${name}`); const destination = `${name}`.split('/').pop(); await file.download({ destination, validation: false }); return destination; } I wan't

Mock a library in jest with Typescript

↘锁芯ラ 提交于 2020-12-16 05:18:50
问题 I want to mock a GCP bucket but Typescript is yelling because of typings. Here's an extract of a Class I want to test: private storage = new Storage({ projectId: 'PROJECT_NAME', keyFilename: env.gcpKeyFilename, }); get bucket() { return this.storage.bucket('fundee-assets'); } private async _downloadFromBucket(name) { const file = this.bucket.file(`${name}`); const destination = `${name}`.split('/').pop(); await file.download({ destination, validation: false }); return destination; } I wan't

Mock a library in jest with Typescript

点点圈 提交于 2020-12-16 05:17:12
问题 I want to mock a GCP bucket but Typescript is yelling because of typings. Here's an extract of a Class I want to test: private storage = new Storage({ projectId: 'PROJECT_NAME', keyFilename: env.gcpKeyFilename, }); get bucket() { return this.storage.bucket('fundee-assets'); } private async _downloadFromBucket(name) { const file = this.bucket.file(`${name}`); const destination = `${name}`.split('/').pop(); await file.download({ destination, validation: false }); return destination; } I wan't

Testing react app with jest and enzyme token problem

谁都会走 提交于 2020-12-15 07:19:40
问题 I have a react app in which I have added unit testing, but one of my tests fails, Here is my test file, I want to test action creators getUser . When I run npm test I get the following error, FAIL UnitTests resources/js/tests/actions/index.test.js ● Test suite failed to run TypeError: Cannot read property 'getAttribute' of null 8 | 'Accept': 'application/json', 9 | 'Content-Type': 'application/json', > 10 | 'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').getAttribute(

Testing react app with jest and enzyme token problem

偶尔善良 提交于 2020-12-15 07:19:16
问题 I have a react app in which I have added unit testing, but one of my tests fails, Here is my test file, I want to test action creators getUser . When I run npm test I get the following error, FAIL UnitTests resources/js/tests/actions/index.test.js ● Test suite failed to run TypeError: Cannot read property 'getAttribute' of null 8 | 'Accept': 'application/json', 9 | 'Content-Type': 'application/json', > 10 | 'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').getAttribute(