From d435873610d2352647ec49ab19ffff43359949b6 Mon Sep 17 00:00:00 2001 From: Christoph Reiter <reiter.christoph@gmail.com> Date: Thu, 1 Aug 2019 12:06:24 +0200 Subject: [PATCH] Add unit test support --- packages/notification/.gitlab-ci.yml | 3 ++- packages/notification/README.md | 3 +++ packages/notification/karma.conf.js | 23 +++++++++++++++++++++++ packages/notification/package.json | 12 +++++++++++- packages/notification/rollup.config.js | 6 ++++-- packages/notification/test/unit.js | 19 +++++++++++++++++++ 6 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 packages/notification/karma.conf.js create mode 100644 packages/notification/test/unit.js diff --git a/packages/notification/.gitlab-ci.yml b/packages/notification/.gitlab-ci.yml index e7b6c123..9cadb871 100644 --- a/packages/notification/.gitlab-ci.yml +++ b/packages/notification/.gitlab-ci.yml @@ -14,6 +14,7 @@ test: stage: test script: - apt update - - apt install -y npm + - apt install -y npm chromium - npm install - npm run build + - npm test diff --git a/packages/notification/README.md b/packages/notification/README.md index 65ed4e0f..c5261354 100644 --- a/packages/notification/README.md +++ b/packages/notification/README.md @@ -39,6 +39,9 @@ npm install # constantly build dist/bundle.js and run a local web-server on port 8002 npm run watch-local + +# run tests +npm test ``` Jump to <http://localhost:8002> and you should get a demo page. diff --git a/packages/notification/karma.conf.js b/packages/notification/karma.conf.js new file mode 100644 index 00000000..901e0780 --- /dev/null +++ b/packages/notification/karma.conf.js @@ -0,0 +1,23 @@ +// Trick to use the auto-downloaded puppeteer chrome binary +process.env.CHROME_BIN = require('puppeteer').executablePath(); + +module.exports = function(config) { + config.set({ + basePath: 'dist', + frameworks: ['mocha', 'chai'], + files: [ + './bundle.js', + {pattern: './**/*', included: false, watched: true, served: true}, + ], + autoWatch: true, + browsers: ['ChromeHeadlessNoSandbox'], + customLaunchers: { + ChromeHeadlessNoSandbox: { + base: 'ChromeHeadless', + flags: ['--no-sandbox'] + } + }, + singleRun: false, + logLevel: config.LOG_ERROR + }); +} diff --git a/packages/notification/package.json b/packages/notification/package.json index 866dfde2..0df207fc 100644 --- a/packages/notification/package.json +++ b/packages/notification/package.json @@ -3,7 +3,14 @@ "version": "1.0.0", "main": "src/index.js", "devDependencies": { + "karma": "^4.2.0", + "karma-chai": "^0.1.0", + "karma-chrome-launcher": "^3.0.0", + "karma-mocha": "^1.3.0", "node-sass": "^4.12.0", + "puppeteer": "^1.15.0", + "mocha": "^6.2.0", + "chai": "^4.2.0", "rollup": "^1.11.3", "rollup-plugin-commonjs": "^9.3.4", "rollup-plugin-copy": "^2.0.1", @@ -12,6 +19,7 @@ "rollup-plugin-serve": "^1.0.1", "rollup-plugin-terser": "^4.0.4", "rollup-plugin-json": "^4.0.0", + "rollup-plugin-multi-entry": "^2.1.0", "rollup-plugin-replace": "^2.2.0", "i18next-scanner": "^2.10.2", "vpu-common": "file:./vendor/common" @@ -28,9 +36,11 @@ "build-dev": "rollup -c --environment BUILD:development", "build-prod": "rollup -c --environment BUILD:production", "build-demo": "rollup -c --environment BUILD:demo", + "build-test": "rollup -c --environment BUILD:test", "i18next": "i18next-scanner", "watch": "npm run watch-local", "watch-local": "rollup -c --watch", - "watch-dev": "rollup -c --watch --environment BUILD:development" + "watch-dev": "rollup -c --watch --environment BUILD:development", + "test": "npm run build-test && karma start --singleRun" } } diff --git a/packages/notification/rollup.config.js b/packages/notification/rollup.config.js index d03df8d3..d133faf4 100644 --- a/packages/notification/rollup.config.js +++ b/packages/notification/rollup.config.js @@ -6,17 +6,19 @@ import {terser} from "rollup-plugin-terser"; import json from 'rollup-plugin-json'; import replace from "rollup-plugin-replace"; import serve from 'rollup-plugin-serve'; +import multiEntry from 'rollup-plugin-multi-entry'; const build = (typeof process.env.BUILD !== 'undefined') ? process.env.BUILD : 'local'; console.log("build: " + build); export default { - input: 'src/demo.js', + input: (build != 'test') ? 'src/demo.js' : 'test/**/*.js', output: { file: 'dist/bundle.js', format: 'esm' }, plugins: [ + multiEntry(), resolve(), commonjs(), json(), @@ -28,7 +30,7 @@ export default { minimize: false, plugins: [] }), - (build !== 'local') ? terser() : false, + (build !== 'local' && build !== 'test') ? terser() : false, copy({ targets: [ 'assets/index.html', diff --git a/packages/notification/test/unit.js b/packages/notification/test/unit.js new file mode 100644 index 00000000..e5bc6f75 --- /dev/null +++ b/packages/notification/test/unit.js @@ -0,0 +1,19 @@ +import '../src/vpu-notification'; + +describe('vpu-notification basics', () => { + let node; + + beforeEach(async () => { + node = document.createElement('vpu-notification'); + document.body.appendChild(node); + await node.updateComplete; + }); + + afterEach(() => { + node.remove(); + }); + + it('should render', () => { + expect(node).to.have.property('shadowRoot'); + }); +}); -- GitLab