From f2f75395fd1ccd284ced50f29e0e3bcff017bebc Mon Sep 17 00:00:00 2001 From: kazuya kawaguchi Date: Tue, 28 Mar 2017 03:19:43 +0900 Subject: [PATCH] :boom: breaking(test): switch jasmine to mocha --- template/config/karma.base.conf.js | 2 +- template/config/webpack.dev.conf.js | 6 +- template/config/webpack.dev.plugin.js | 29 ---------- template/config/webpack.runner.template.html | 15 ----- template/package.json | 9 +-- template/test/.eslintrc | 5 +- template/test/helpers/wait-for-update.js | 58 +++++++++++++++++++- template/test/unit/add.test.js | 2 +- template/test/unit/index.html | 10 ++++ 9 files changed, 80 insertions(+), 56 deletions(-) delete mode 100644 template/config/webpack.dev.plugin.js delete mode 100644 template/config/webpack.runner.template.html create mode 100644 template/test/unit/index.html diff --git a/template/config/karma.base.conf.js b/template/config/karma.base.conf.js index c3df4bb..0ae45eb 100644 --- a/template/config/karma.base.conf.js +++ b/template/config/karma.base.conf.js @@ -25,7 +25,7 @@ module.exports = { ], exclude: [ ], - frameworks: ['jasmine'], + frameworks: ['mocha', 'phantomjs-shim'], preprocessors: { '../test/unit/index.js': ['webpack', 'sourcemap'] }, diff --git a/template/config/webpack.dev.conf.js b/template/config/webpack.dev.conf.js index 9854ae8..c47e745 100644 --- a/template/config/webpack.dev.conf.js +++ b/template/config/webpack.dev.conf.js @@ -1,9 +1,8 @@ const path = require('path') const webpack = require('webpack') -const JasmineWebpackPlugin = require('./webpack.dev.plugin') module.exports = { - entry: './test/unit/index.js', + entry: 'mocha-loader!./test/unit/index.js', output: { path: path.resolve(__dirname, '/test/unit'), filename: 'tests.js', @@ -21,8 +20,7 @@ module.exports = { 'process.env': { NODE_ENV: '"development"' } - }), - new JasmineWebpackPlugin() + }) ], devtool: '#eval-source-map' } diff --git a/template/config/webpack.dev.plugin.js b/template/config/webpack.dev.plugin.js deleted file mode 100644 index 843b1e5..0000000 --- a/template/config/webpack.dev.plugin.js +++ /dev/null @@ -1,29 +0,0 @@ -const path = require('path') -const HtmlWebpackPlugin = require('html-webpack-plugin') -const jasmineCore = require('jasmine-core') - -const jasmineFiles = jasmineCore.files -const jasminePath = resolveJasmineDir(jasmineFiles.path) -const jasmineBootDir = resolveJasmineDir(jasmineFiles.bootDir) -const jasmineJsFiles = resolveJasmineFiles(jasminePath, jasmineFiles.jsFiles) -const jasmineCssFiles = resolveJasmineFiles(jasminePath, jasmineFiles.cssFiles) -const jasmineBootFiles = resolveJasmineFiles(jasmineBootDir, jasmineFiles.bootFiles) - -function JasmineWebpackPlugin (options = {}) { - return new HtmlWebpackPlugin({ - title: '{{ name }} test runner', - filename: options.filename || 'index.html', - template: './config/webpack.runner.template.html', - jasmineJsFiles: jasmineJsFiles.concat(jasmineBootFiles), - jasmineCssFiles - }) -} - -function resolveJasmineDir (dirname) { - return dirname.replace(process.cwd(), '').replace(/^\//, '') -} - -function resolveJasmineFiles (dirname, files) { - return files.map(file => { return path.join(dirname, file) }) -} -module.exports = JasmineWebpackPlugin diff --git a/template/config/webpack.runner.template.html b/template/config/webpack.runner.template.html deleted file mode 100644 index ef6fe4b..0000000 --- a/template/config/webpack.runner.template.html +++ /dev/null @@ -1,15 +0,0 @@ - - - - - <%= htmlWebpackPlugin.options.title %> - <% for (var i = 0; i < htmlWebpackPlugin.options.jasmineCssFiles.length; i++) { %> - - <% } %> - <% for (var i = 0; i < htmlWebpackPlugin.options.jasmineJsFiles.length; i++) { %> - - <% } %> - - - - diff --git a/template/package.json b/template/package.json index 042a069..1384d2e 100644 --- a/template/package.json +++ b/template/package.json @@ -37,8 +37,8 @@ "gitbook-cli": "^2.3.0", {{/gitbook}} "html-webpack-plugin": "^2.19.0", - "jasmine": "^2.5.3", - "jasmine-core": "^2.5.2", + "mocha": "^3.2.0", + "mocha-loader": "^1.1.1", "karma": "^1.4.1", "karma-chrome-launcher": "^2.0.0", "karma-coverage": "^1.1.1", @@ -46,9 +46,10 @@ "karma-coveralls": "^1.1.2", {{/if_eq}} "karma-firefox-launcher": "^1.0.0", - "karma-jasmine": "^1.1.0", + "karma-mocha": "^1.3.0", "karma-mocha-reporter": "^2.2.2", "karma-phantomjs-launcher": "^1.0.2", + "karma-phantomjs-shim": "^1.4.0", "karma-safari-launcher": "^1.0.0", {{#sauce}} "karma-sauce-launcher": "^1.1.0", @@ -113,7 +114,7 @@ {{#if_eq coverageConfig "codecov"}} "coverage": "cat ./coverage/lcov.info", {{/if_eq}} - "dev": "BABEL_ENV=test webpack-dev-server --inline --hot --open --config config/webpack.dev.conf.js", + "dev": "BABEL_ENV=test webpack-dev-server --inline --hot --open --content-base ./test/unit/ --config config/webpack.dev.conf.js", {{#gitbook}} "docs": "gitbook serve ./gitbook ./docs", "docs:build": "node config/version.js && gitbook build ./gitbook ./docs", diff --git a/template/test/.eslintrc b/template/test/.eslintrc index 93c1af2..fe48725 100644 --- a/template/test/.eslintrc +++ b/template/test/.eslintrc @@ -1,9 +1,12 @@ { "env": { - "jasmine": true + "browser": true, + "mocha": true }, "globals": { "waitForUpdate": true, + "nextTick": true, + "delay": true, "assert": true, "Vue": true } diff --git a/template/test/helpers/wait-for-update.js b/template/test/helpers/wait-for-update.js index d691f46..07b1a18 100644 --- a/template/test/helpers/wait-for-update.js +++ b/template/test/helpers/wait-for-update.js @@ -1,6 +1,7 @@ import Vue from 'vue' -// helper for async assertions. +// helper for jasmine async assertions. +// // Use like this: // // vm.a = 123 @@ -66,3 +67,58 @@ window.waitForUpdate = initialCb => { function timeout (n) { return next => setTimeout(next, n) } + +// helper for mocha async assertions. +// nextTick().then(() => { +// +// Automatically waits for nextTick +// }).then(() => { +// return a promise or value to skip the wait +// }) +function nextTick () { + const jobs = [] + let done + + const chainer = { + then (cb) { + jobs.push(cb) + return chainer + } + } + + function shift (...args) { + const job = jobs.shift() + let result + try { + result = job(...args) + } catch (e) { + jobs.length = 0 + done(e) + } + + // wait for nextTick + if (result !== undefined) { + if (result.then) { + result.then(shift) + } else { + shift(result) + } + } else if (jobs.length) { + requestAnimationFrame(() => Vue.nextTick(shift)) + } + } + + // First time + Vue.nextTick(() => { + done = jobs[jobs.length - 1] + if (done.toString().slice(0, 14) !== 'function (err)') { + throw new Error('waitForUpdate chain is missing .then(done)') + } + shift() + }) + + return chainer +} + +window.nextTick = nextTick +window.delay = time => new Promise(resolve => setTimeout(resolve, time)) diff --git a/template/test/unit/add.test.js b/template/test/unit/add.test.js index 8485b3e..24e2417 100644 --- a/template/test/unit/add.test.js +++ b/template/test/unit/add.test.js @@ -9,7 +9,7 @@ describe('$add', () => { describe('1 + 1', () => { it('should be 2', done => { - waitForUpdate(() => { + nextTick(() => { assert(vm.$add(1, 1) === 2, 'You should be implemented!!') }).then(done) }) diff --git a/template/test/unit/index.html b/template/test/unit/index.html new file mode 100644 index 0000000..f903354 --- /dev/null +++ b/template/test/unit/index.html @@ -0,0 +1,10 @@ + + + + {{ name }} tests + + + + + +