Posts

Showing posts with the label jestjs

Jest + Enzyme + React Native: How to test content of tag?

Image
Clash Royale CLAN TAG #URR8PPP Jest + Enzyme + React Native: How to test content of <Text /> tag? I want to unit test with Jest and Enzyme if my <Text /> tag correctly receives props.header as text. <Text /> props.header Usually I was able to test the content of the <Text /> tag like this: <Text /> it("should render a label", () => { expect(wrapper.find(Text).contains("submit")).toBe(true); }); But as soon as I pass an object this is no longer possible. Let me show you: const createTestProps = props => ({ header: SOME_CONSTANT, ...props }); ... let wrapper; let props; beforeEach(() => { props = createTestProps(); wrapper = shallow(<MyList {...props} loaded={false} />); }); it("should render a header", () => { expect(wrapper.find(Text).contains(props.header)).toBe(true); }); This fails with the following error message: ● MyList › rendering › still loading › should render a head...

How do I test a single file using Jest?

Image
Clash Royale CLAN TAG #URR8PPP How do I test a single file using Jest? I am able to test multiple files using Jest, but I cannot figure out how to test a single file. I have: npm install jest-cli --save-dev package.json Running npm test works as expected (currently it runs 14 tests). npm test How do I test a single file, e.g. test app/foo/__tests__/bar.spec.js ? app/foo/__tests__/bar.spec.js I have tried running npm test app/foo/__tests__/bar.spec.js (from project root), but I get the following error: npm test app/foo/__tests__/bar.spec.js npm ERR! Error: ENOENT, open '<path to project>/node_modules/app/foo/__tests__/bar.spec.js/package.json' Thanks 6 Answers 6 If npm test works, it means jest is installed globally. In order to run a specific test, you'll need to use the jest command- npm test will not work. npm test jest npm test So you can run jest directly with jest bar.spe...