jestjs

How can I make jest run with the same config as react-script test on a create-react-app?

我与影子孤独终老i 提交于 2021-02-04 19:23:12
问题 I know I can run npm test which runs react-script test and it works, it successfully run the tests. But I'm interested in figuring out how to run jest directly with the same configuration react-script uses. Hopefully without having to replicate the configuration or ejecting the app. I started reading the source code of react-scripts but so far I couldn't figure it out. The reasons for wanting this are: My CRA project is part of a bigger project, and I could just run jest on the top level and

Use variable expressions in test.each Jest

懵懂的女人 提交于 2021-02-04 19:14:09
问题 Below is my code snippet: describe('Upper Describe,()=>{ let value; beforeEach(()=>{ value=require('testModule').value; }); it.each([ `${value}`, ])('test something',(value)=>{ console.log(value); }); }); Here the value comes to be undefined . My guess is it is because as the describe blocks get loaded at the starting so are the values for it.each . Can anyone please help me with a workaround to get the variable values inside it.each array? Thanks in advance!! 回答1: Instead of passing the

Testing anonymous function equality with Jest

故事扮演 提交于 2021-02-04 11:49:39
问题 Is there a way to test anonymous function equality with jest@20 ? I am trying to pass a test similar to: const foo = i => j => {return i*j} const bar = () => {baz:foo(2), boz:1} describe('Test anonymous function equality',()=>{ it('+++ foo', () => { const obj = foo(2) expect(obj).toBe(foo(2)) }); it('+++ bar', () => { const obj = bar() expect(obj).toEqual({baz:foo(2), boz:1}) }); }); which currently yields: ● >>>Test anonymous function equality › +++ foo expect(received).toBe(expected)

Jest mocking TypeScript class “No overload expects 1 type arguments”

一笑奈何 提交于 2021-02-04 05:09:24
问题 I'm trying to mock a TypeScript class with Jest and I'm obviously doing something because receive the following error: error TS2743: No overload expects 1 type arguments, but overloads do exist that expect either 0 or 2 type arguments. This is my code: Foo.ts export default class Foo { bar(): number { return Math.random() } } Foo.test.ts import Foo from './Foo' describe('Foo', () => { it("should pass", () => { const MockFoo = jest.fn<Foo>(() => ({ bar: jest.fn(() => { return 123 }), })) }) })

Jest mocking TypeScript class “No overload expects 1 type arguments”

为君一笑 提交于 2021-02-04 05:04:51
问题 I'm trying to mock a TypeScript class with Jest and I'm obviously doing something because receive the following error: error TS2743: No overload expects 1 type arguments, but overloads do exist that expect either 0 or 2 type arguments. This is my code: Foo.ts export default class Foo { bar(): number { return Math.random() } } Foo.test.ts import Foo from './Foo' describe('Foo', () => { it("should pass", () => { const MockFoo = jest.fn<Foo>(() => ({ bar: jest.fn(() => { return 123 }), })) }) })

How to mock module in different ways in different tests in the same test file in Jest?

我怕爱的太早我们不能终老 提交于 2021-01-29 22:19:13
问题 Currently I have this: jest.mock('my/hook', () => () => false) I want my custom React hook module to return false in every test by default, but in a few tests I want it to return true. The hook is implemented essentially like this: function useMyHook(key) { switch (key) { case 'foo': case 'bar': return true default: return false } } I am using the hook several times in my component, once for the foo key and once for the bar key. I want it to return false for both keys by default. But for a

How to test async actions that returns promise?

两盒软妹~` 提交于 2021-01-29 22:07:27
问题 fetchMock.get(`http://localhost:8080/data/v1/shopping-cart.json#/`).then(res => { console.log('response ', res); }); I am passing http://localhost:8080/data/v1/shopping-cart.json#/ as a base URL to the fetchMock.get() method and getting the following output: fetch-mock: Invalid parameters passed to fetch-mock What could be a reason? 回答1: First, you actually misuse fetchMock() . Check their docs on details. It's expected you to pass url and callback as part of .get() : fetchMock.get('http:/

How to mock module in different ways in different tests in the same test file in Jest?

喜你入骨 提交于 2021-01-29 21:56:41
问题 Currently I have this: jest.mock('my/hook', () => () => false) I want my custom React hook module to return false in every test by default, but in a few tests I want it to return true. The hook is implemented essentially like this: function useMyHook(key) { switch (key) { case 'foo': case 'bar': return true default: return false } } I am using the hook several times in my component, once for the foo key and once for the bar key. I want it to return false for both keys by default. But for a

Jest cannot read property 'import' of undefined

雨燕双飞 提交于 2021-01-29 20:40:23
问题 I have the following code to load a dependency asynchronously: declare global { interface Window { System: System.Module } } const fooService = window.System.import('@internal/foo-service').then(module => module.FooService) // ^ Jest trips up here async function func1() { (await fooService).doBar() … } async function func2() { (await fooService).doBar2() … } This works fine thanks to Bergi but Jest is tripping up over window.System.import and gives me the error Cannot read property 'import'

Testing form input in jest and enzyme

十年热恋 提交于 2021-01-29 19:40:54
问题 I've been working with tests in react, and I'm in a trouble with testing form inputs. FormInput component in register <FormInput data-test="form-input" className={`${errors.username && "input-error"}`} name="username" type="text" ariaLabel="Username" placeholder="Username" value={values.username} onChange={handleChange} onBlur={handleBlur} icon={ <FaUser size={"3rem"} color={theme.colors.formInputBorderColor} /> } /> This is FormInput component export const FormInput = React.memo(({name,type