From 95719c956ede1e375f6f92f27b71d54837e5856d Mon Sep 17 00:00:00 2001 From: Pavithra Kodmad Date: Wed, 26 Oct 2016 15:47:02 +0530 Subject: [PATCH] Adds jest configuration npm run test shows failed results (#60) --- examples/with-jest/__tests__/index.test.js | 12 +++++++++++ examples/with-jest/package.json | 17 +++++++++++++++ examples/with-jest/pages/index.js | 6 ++++++ examples/with-jest/readme.md | 25 ++++++++++++++++++++++ 4 files changed, 60 insertions(+) create mode 100644 examples/with-jest/__tests__/index.test.js create mode 100644 examples/with-jest/package.json create mode 100644 examples/with-jest/pages/index.js create mode 100644 examples/with-jest/readme.md diff --git a/examples/with-jest/__tests__/index.test.js b/examples/with-jest/__tests__/index.test.js new file mode 100644 index 00000000..dd3c6ef0 --- /dev/null +++ b/examples/with-jest/__tests__/index.test.js @@ -0,0 +1,12 @@ + +import React from 'react'; +import {shallow} from 'enzyme'; +import App from '../pages/index.js'; + +it('App shows "Hello world!"', () => { + const app = shallow( + + ); + + expect(app.find('p').text()).toEqual('Hello world!'); +}); \ No newline at end of file diff --git a/examples/with-jest/package.json b/examples/with-jest/package.json new file mode 100644 index 00000000..cc0fe759 --- /dev/null +++ b/examples/with-jest/package.json @@ -0,0 +1,17 @@ +{ + "name": "my-app", + "dependencies": { + "next": "^1.0.0" + }, + "scripts": { + "test": "jest", + "dev": "next", + "build": "next build", + "start": "next start" + }, + "devDependencies": { + "babel-jest": "^16.0.0", + "enzyme": "^2.5.1", + "jest": "^16.0.2" + } +} diff --git a/examples/with-jest/pages/index.js b/examples/with-jest/pages/index.js new file mode 100644 index 00000000..4da9fdf7 --- /dev/null +++ b/examples/with-jest/pages/index.js @@ -0,0 +1,6 @@ +import React from 'react' +export default () => ( +
+

Hello world!

+
+) \ No newline at end of file diff --git a/examples/with-jest/readme.md b/examples/with-jest/readme.md new file mode 100644 index 00000000..ffb591cd --- /dev/null +++ b/examples/with-jest/readme.md @@ -0,0 +1,25 @@ +## Add testing to your `next` app using `jest` + +[`jest`](https://facebook.github.io/jest/) is a testing framework for `react`. In this example we show how to use `jest` to do DOM-testing for react applications in `next` + +npm install --save-dev jest babel-jest enzyme + + * `jest` - The testing framework + * `babel-jest` - Babel preprocessor for test files + * `enzyme` - Mock render the elements + +Add test script to the [recommended `package.json`](https://github.com/zeit/next.js#production-deployment) + +__package.json__ + +```javascript +... +"scripts": { + "test": "jest", + ... +} +... + +``` + +`npm run test`