From c56fde89c38507681bfae31bf1d54184fdbad1b7 Mon Sep 17 00:00:00 2001 From: Benjamin Coe Date: Tue, 15 Nov 2016 00:24:20 -0800 Subject: [PATCH] chore: add test coverage --- .babelrc | 14 +++++++++ .gitignore | 4 +++ .travis.yml | 1 + README.md | 2 ++ gulpfile.js | 40 +++++++------------------ lib/document.js | 4 ++- package.json | 41 ++++++++++++++++++++++++-- test/fixtures/basic/pages/head.js | 1 - test/fixtures/basic/pages/link.js | 5 ++++ test/{index.js => integration.test.js} | 25 +++++++++------- test/lib/shallow-equals.test.js | 39 ++++++++++++++++++++++++ 11 files changed, 131 insertions(+), 45 deletions(-) create mode 100644 .babelrc create mode 100644 test/fixtures/basic/pages/link.js rename test/{index.js => integration.test.js} (72%) create mode 100644 test/lib/shallow-equals.test.js diff --git a/.babelrc b/.babelrc new file mode 100644 index 00000000..dabbdc9b --- /dev/null +++ b/.babelrc @@ -0,0 +1,14 @@ +{ + "presets": ["es2015", "react"], + "plugins": [ + "transform-async-to-generator", + "transform-object-rest-spread", + "transform-class-properties", + "transform-runtime" + ], + "env": { + "test": { + "plugins": ["istanbul"] + } + } +} diff --git a/.gitignore b/.gitignore index 81771de9..ddbadf8d 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,7 @@ npm-debug.log # other .next + +# coverage +.nyc_output +coverage diff --git a/.travis.yml b/.travis.yml index 37b5df12..bb0149d5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,3 +6,4 @@ node_js: cache: directories: - node_modules +after_script: npm run coveralls diff --git a/README.md b/README.md index d6295573..2b4be066 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ screen shot 2016-10-25 at 2 37 27 pm [![Build Status](https://travis-ci.org/zeit/next.js.svg?branch=master)](https://travis-ci.org/zeit/next.js) +[![Coverage Status](https://coveralls.io/repos/zeit/next.js/badge.svg?branch=master)](https://coveralls.io/r/zeit/next.js?branch=master) + [![Slack Channel](https://zeit-slackin.now.sh/badge.svg)](https://zeit.chat) Next.js is a minimalistic framework for server-rendered React applications. diff --git a/gulpfile.js b/gulpfile.js index 45a9f5b3..435dc328 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -1,3 +1,4 @@ +const fs = require('fs') const gulp = require('gulp') const babel = require('gulp-babel') const cache = require('gulp-cached') @@ -8,15 +9,7 @@ const sequence = require('run-sequence') const webpack = require('webpack-stream') const del = require('del') -const babelOptions = { - presets: ['es2015', 'react'], - plugins: [ - 'transform-async-to-generator', - 'transform-object-rest-spread', - 'transform-class-properties', - 'transform-runtime' - ] -} +const babelOptions = JSON.parse(fs.readFileSync('.babelrc', 'utf-8')) gulp.task('compile', [ 'compile-bin', @@ -57,14 +50,6 @@ gulp.task('compile-client', () => { .pipe(notify('Compiled client files')) }) -gulp.task('compile-test', () => { - return gulp.src('test/*.js') - .pipe(cache('test')) - .pipe(babel(babelOptions)) - .pipe(gulp.dest('dist/test')) - .pipe(notify('Compiled test files')) -}) - gulp.task('copy', ['copy-pages']) gulp.task('copy-pages', () => { @@ -72,11 +57,6 @@ gulp.task('copy-pages', () => { .pipe(gulp.dest('dist/pages')) }) -gulp.task('copy-test-fixtures', () => { - return gulp.src('test/fixtures/**/*') - .pipe(gulp.dest('dist/test/fixtures')) -}) - gulp.task('compile-bench', () => { return gulp.src('bench/*.js') .pipe(cache('bench')) @@ -153,9 +133,13 @@ gulp.task('build-client', ['compile-lib', 'compile-client'], () => { .pipe(notify('Built release client')) }) -gulp.task('test', ['compile', 'copy', 'compile-test', 'copy-test-fixtures'], () => { - return gulp.src('dist/test/*.js') - .pipe(ava()) +gulp.task('test', () => { + process.env.NODE_ENV = 'test' + return gulp.src('test/**/**.test.js') + .pipe(ava({ + verbose: true, + nyc: true + })) }) gulp.task('bench', ['compile', 'copy', 'compile-bench', 'copy-bench-fixtures'], () => { @@ -209,10 +193,6 @@ gulp.task('clean', () => { return del('dist') }) -gulp.task('clean-test', () => { - return del('dist/test') -}) - gulp.task('default', [ 'compile', 'build', @@ -227,7 +207,7 @@ gulp.task('release', (cb) => { 'build', 'copy', 'test' - ], 'clean-test', cb) + ], cb) }) // avoid logging to the console diff --git a/lib/document.js b/lib/document.js index 9cef4a64..8bfd80a7 100644 --- a/lib/document.js +++ b/lib/document.js @@ -1,6 +1,8 @@ import React from 'react' import htmlescape from 'htmlescape' -import pkg from '../../package.json' +import readPkgUp from 'read-pkg-up' + +const pkg = readPkgUp.sync({normalize: false}).pkg export default ({ head, css, html, data, dev, staticMarkup, cdn }) => { return diff --git a/package.json b/package.json index ae09ca71..17e3b252 100644 --- a/package.json +++ b/package.json @@ -16,13 +16,35 @@ }, "scripts": { "build": "gulp", - "test": "npm run lint && gulp test", + "pretest": "npm run lint", + "test": "gulp test", "lint": "standard && standard bin/*", + "html-report": "nyc report --reporter=html", + "coveralls": "nyc report --reporter=text-lcov | coveralls", "prepublish": "gulp release", "precommit": "npm run lint" }, - "ava": { - "babel": {} + "nyc": { + "require": [ + "babel-register" + ], + "exclude": [ + "gulpfile.js", + "css.js", + "link.js", + "head.js", + "client/**", + "**/pages/**", + "**/coverage/**", + "**/client/**", + "**/test/**", + "**/dist/**", + "**/examples/**", + "**/bench/**" + ], + "all": true, + "sourceMap": false, + "instrument": false }, "standard": { "parser": "babel-eslint" @@ -54,11 +76,14 @@ "react": "15.4.0", "react-dom": "15.4.0", "react-hot-loader": "3.0.0-beta.6", + "read-pkg-up": "2.0.0", "send": "0.14.1", + "sockjs-client": "1.1.1", "strip-ansi": "3.0.1", "url": "0.11.0", "webpack": "1.13.3", "webpack-dev-server": "1.16.2", +<<<<<<< HEAD "sockjs-client": "1.1.1", "write-file-webpack-plugin": "3.4.2" }, @@ -67,6 +92,15 @@ "babel-eslint": "7.1.1", "babel-plugin-transform-remove-strict-mode": "0.0.2", "benchmark": "2.1.2", + "write-file-webpack-plugin": "3.3.0" + }, + "devDependencies": { + "ava": "0.16.0", + "babel-eslint": "7.0.0", + "babel-plugin-istanbul": "3.0.0", + "babel-plugin-transform-remove-strict-mode": "0.0.2", + "benchmark": "2.1.1", + "coveralls": "2.11.15", "gulp": "3.9.1", "gulp-ava": "0.15.0", "gulp-babel": "6.1.2", @@ -74,6 +108,7 @@ "gulp-cached": "1.1.1", "gulp-notify": "2.2.0", "husky": "0.11.9", + "nyc": "9.0.1", "run-sequence": "1.2.2", "standard": "8.5.0", "webpack-stream": "3.2.0" diff --git a/test/fixtures/basic/pages/head.js b/test/fixtures/basic/pages/head.js index 43ded20e..a9b13c87 100644 --- a/test/fixtures/basic/pages/head.js +++ b/test/fixtures/basic/pages/head.js @@ -1,4 +1,3 @@ - import React from 'react' import Head from 'next/head' diff --git a/test/fixtures/basic/pages/link.js b/test/fixtures/basic/pages/link.js new file mode 100644 index 00000000..a5e64a34 --- /dev/null +++ b/test/fixtures/basic/pages/link.js @@ -0,0 +1,5 @@ +import React from 'react' +import Link from 'next/link' +export default () => ( +
Hello World. About
+) diff --git a/test/index.js b/test/integration.test.js similarity index 72% rename from test/index.js rename to test/integration.test.js index ba583ebe..eb7a0648 100644 --- a/test/index.js +++ b/test/integration.test.js @@ -7,35 +7,40 @@ const dir = join(__dirname, 'fixtures', 'basic') test.before(() => build(dir)) -test(async t => { +test('renders a stateless component', async t => { const html = await render('/stateless') t.true(html.includes('')) t.true(html.includes('

My component!

')) }) -test(async t => { - const html = await render('/css') - t.true(html.includes('.css-im3wl1')) - t.true(html.includes('
This is red
')) -}) - -test(async t => { +test('renders a stateful component', async t => { const html = await render('/stateful') t.true(html.includes('

The answer is 42

')) }) -test(async t => { +test('header helper renders header information', async t => { const html = await (render('/head')) t.true(html.includes('')) t.true(html.includes('')) t.true(html.includes('

I can haz meta tags

')) }) -test(async t => { +test('css helper renders styles', async t => { + const html = await render('/css') + t.true(html.includes('.css-im3wl1')) + t.true(html.includes('
This is red
')) +}) + +test('renders properties populated asynchronously', async t => { const html = await render('/async-props') t.true(html.includes('

Diego Milito

')) }) +test('renders a link component', async t => { + const html = await render('/link') + t.true(html.includes('About')) +}) + function render (url, ctx) { return _render(url, ctx, { dir, staticMarkup: true }) } diff --git a/test/lib/shallow-equals.test.js b/test/lib/shallow-equals.test.js new file mode 100644 index 00000000..9e69cdcd --- /dev/null +++ b/test/lib/shallow-equals.test.js @@ -0,0 +1,39 @@ +import test from 'ava' +import shallowEquals from '../../lib/shallow-equals' + +test('returns true if all key/value pairs match', t => { + t.true(shallowEquals({ + a: 1, + b: 2, + c: 99 + }, { + a: 1, + b: 2, + c: 99 + })) +}) + +test('returns false if any key/value pair is different', t => { + t.false(shallowEquals({ + a: 1, + b: 2, + c: 99 + }, { + a: 1, + b: 2, + c: 99, + d: 33 + })) +}) + +test('returns false if nested objects are contained', t => { + t.false(shallowEquals({ + a: 1, + b: 2, + c: {} + }, { + a: 1, + b: 2, + c: {} + })) +})