From c458fd93091bc7e9eeff161f930de26f03a37281 Mon Sep 17 00:00:00 2001 From: Nathan Houle Date: Thu, 2 Jun 2016 12:30:23 -0700 Subject: [PATCH] Modernize test harness; remove Duo support, add Browserify support --- .eslintrc | 14 +-- .gitignore | 5 +- Contributing.md => CONTRIBUTING.md | 0 History.md => HISTORY.md | 7 ++ License.md => LICENSE | 0 Makefile | 143 +++++++++++++---------------- Readme.md => README.md | 2 +- circle.yml | 26 +++++- component.json | 21 ----- karma.conf.ci.js | 101 ++++++++++++++++++++ karma.conf.js | 53 +++++++++++ lib/index.js | 21 +++-- package.json | 59 ++++++++---- test/.eslintrc | 4 +- test/index.html | 36 -------- test/index.test.js | 24 +++-- 16 files changed, 321 insertions(+), 195 deletions(-) rename Contributing.md => CONTRIBUTING.md (100%) rename History.md => HISTORY.md (74%) rename License.md => LICENSE (100%) rename Readme.md => README.md (89%) delete mode 100644 component.json create mode 100644 karma.conf.ci.js create mode 100644 karma.conf.js delete mode 100644 test/index.html diff --git a/.eslintrc b/.eslintrc index 1026c14..45688a7 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,15 +1,3 @@ { - "extends": "segment/browser", - - "rules": { - "global-strict": 0, - "max-len": 0, - "strict": 1 - }, - - "globals": { - "exports": true, - "module": true, - "require": true - } + "extends": "@segment/eslint-config/browser/legacy" } diff --git a/.gitignore b/.gitignore index 68492d8..62562b7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,2 @@ -*.log -.DS_Store -build.js -components +coverage node_modules diff --git a/Contributing.md b/CONTRIBUTING.md similarity index 100% rename from Contributing.md rename to CONTRIBUTING.md diff --git a/History.md b/HISTORY.md similarity index 74% rename from History.md rename to HISTORY.md index 2cfaaf0..ff1ca76 100644 --- a/History.md +++ b/HISTORY.md @@ -1,3 +1,10 @@ +2.0.0 / ????-??-?? +================== + + * Remove Duo compatibility + * Add CI setup (coverage, linting, cross-browser compatibility, etc.) + * Update eslint configuration + 1.0.5 / 2016-05-07 ================== diff --git a/License.md b/LICENSE similarity index 100% rename from License.md rename to LICENSE diff --git a/Makefile b/Makefile index cd77440..86eb29b 100644 --- a/Makefile +++ b/Makefile @@ -1,93 +1,80 @@ -# -# Binaries. -# - -DUO = node_modules/.bin/duo -DUOT = node_modules/.bin/duo-test -ESLINT = node_modules/.bin/eslint - -# -# Files. -# - -SRCS_DIR = lib -SRCS = $(shell find $(SRCS_DIR) -type f -name "*.js") -TESTS_DIR = test -TESTS = $(shell find $(TESTS_DIR) -type f -name '*.test.js') - -# -# Task config. -# - -BROWSER ?= chrome - -PORT ?= 0 - -DUOT_ARGS = \ - --reporter spec \ - --port $(PORT) \ - --commands "make build" - -# -# Chore tasks. -# - -# Install node dependencies. +## +# Binaries +## + +ESLINT := node_modules/.bin/eslint +KARMA := node_modules/.bin/karma + +## +# Files +## + +LIBS = $(shell find lib -type f -name "*.js") +TESTS = $(shell find test -type f -name "*.test.js") +SUPPORT = $(wildcard karma.conf*.js) +ALL_FILES = $(LIBS) $(TESTS) $(SUPPORT) + +## +# Program options/flags +## + +# A list of options to pass to Karma +# Overriding this overwrites all options specified in this file (e.g. BROWSERS) +KARMA_FLAGS ?= + +# A list of Karma browser launchers to run +# http://karma-runner.github.io/0.13/config/browsers.html +BROWSERS ?= +ifdef BROWSERS +KARMA_FLAGS += --browsers $(BROWSERS) +endif + +ifdef CI +KARMA_CONF ?= karma.conf.ci.js +else +KARMA_CONF ?= karma.conf.js +endif + +# Mocha flags. +GREP ?= . + +## +# Tasks +## + +# Install node modules. node_modules: package.json $(wildcard node_modules/*/package.json) @npm install + @touch $@ + +# Install dependencies. +install: node_modules # Remove temporary files and build artifacts. clean: - rm -rf build.js + rm -rf *.log coverage .PHONY: clean # Remove temporary files, build artifacts, and vendor dependencies. distclean: clean - rm -rf components node_modules + rm -rf node_modules .PHONY: distclean -# -# Build tasks. -# - -# Build all integrations, tests, and dependencies together for testing. -build.js: node_modules component.json $(SRCS) $(TESTS) - @$(DUO) --stdout --development $(TESTS) > $@ - -# Build shortcut. -build: build.js -.DEFAULT_GOAL = build - -# -# Test tasks. -# - -# Lint JavaScript source. -lint: node_modules - @$(ESLINT) $(SRCS) $(TESTS) +# Lint JavaScript source files. +lint: install + @$(ESLINT) $(ALL_FILES) .PHONY: lint -# Test locally in PhantomJS. -test-phantomjs: node_modules build.js - @$(DUOT) phantomjs $(TESTS_DIR) args: \ - --path node_modules/.bin/phantomjs -.PHONY: test +# Attempt to fix linting errors. +fmt: install + @$(ESLINT) --fix $(ALL_FILES) +.PHONY: fmt + +# Run browser unit tests in a browser. +test-browser: install + @$(KARMA) start $(KARMA_FLAGS) $(KARMA_CONF) -# Test locally in the browser. -test-browser: node_modules build.js - @$(DUOT) browser --commands "make build" $(TESTS_DIR) -.PHONY: test-browser - -# Test in Sauce Labs. Note that you must set the SAUCE_USERNAME and -# SAUCE_ACCESS_KEY environment variables using your Sauce Labs credentials. -test-sauce: node_modules build.js - @$(DUOT) saucelabs $(TESTS_DIR) \ - --name analytics.js-integrations \ - --browsers $(BROWSER) \ - --user $(SAUCE_USERNAME) \ - --key $(SAUCE_ACCESS_KEY) -.PHONY: test-sauce - -# Test shortcut. -test: lint test-phantomjs +# Default test target. +test: lint test-browser .PHONY: test +.DEFAULT_GOAL = test diff --git a/Readme.md b/README.md similarity index 89% rename from Readme.md rename to README.md index f756549..f8d8c35 100644 --- a/Readme.md +++ b/README.md @@ -4,7 +4,7 @@ Intercom integration for [Analytics.js][]. ## License -Released under the [MIT license](License.md). +Released under the [MIT license](LICENSE). [Analytics.js]: https://segment.com/docs/libraries/analytics.js/ diff --git a/circle.yml b/circle.yml index 19ba508..382a5b6 100644 --- a/circle.yml +++ b/circle.yml @@ -1,11 +1,29 @@ machine: node: - version: 0.12 + version: 4 + environment: + NPM_CONFIG_PROGRESS: false + NPM_CONFIG_SPIN: false + TEST_REPORTS_DIR: $CIRCLE_TEST_REPORTS + dependencies: pre: - - echo "github.com,192.30.252.*,192.30.253.*,192.30.254.*,192.30.255.* ssh-rsa $(ssh-keyscan -t rsa github.com | cut -d ' ' -f 3-)" >> ~/.ssh/known_hosts - - npm install -g npm@'>=2.7.0' - - make + - npm config set "//registry.npmjs.org/:_authToken" $NPM_AUTH + - npm -g install codecov + override: + - make install + test: override: - make test + post: + - cp -R coverage $CIRCLE_ARTIFACTS/ + - codecov + +deployment: + publish: + owner: segmentio + # Works on e.g. `1.0.0-alpha.1` + tag: /[0-9]+(\.[0-9]+)*(-.+)?/ + commands: + - npm publish . diff --git a/component.json b/component.json deleted file mode 100644 index 4fe7593..0000000 --- a/component.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "name": "analytics.js-integration-intercom", - "repo": "segment-integrations/analytics.js-integration-intercom", - "description": "The Intercom analytics.js integration.", - "version": "1.0.5", - "license": "MIT", - "main": "lib/index.js", - "dependencies": { - "segmentio/alias": "0.2.1", - "segmentio/convert-dates": "0.1.0", - "avetisk/defaults": "0.0.1", - "segmentio/obj-case": "0.2.1", - "segmentio/analytics.js-integration": "^1.1.0", - "ianstormtaylor/is": "0.1.0" - }, - "development": { - "segmentio/analytics.js-core": "^2.12.0", - "segmentio/clear-env": "0.2.x", - "segmentio/analytics.js-integration-tester": "^1.5.0" - } -} diff --git a/karma.conf.ci.js b/karma.conf.ci.js new file mode 100644 index 0000000..aff0fe5 --- /dev/null +++ b/karma.conf.ci.js @@ -0,0 +1,101 @@ +/* eslint-env node */ +'use strict'; + +var baseConfig = require('./karma.conf'); + +var customLaunchers = { + sl_chrome_latest: { + base: 'SauceLabs', + browserName: 'chrome', + platform: 'linux', + version: 'latest' + }, + sl_chrome_latest_1: { + base: 'SauceLabs', + browserName: 'chrome', + platform: 'linux', + version: 'latest-1' + }, + sl_firefox_latest: { + base: 'SauceLabs', + browserName: 'firefox', + platform: 'linux', + version: 'latest' + }, + sl_firefox_latest_1: { + base: 'SauceLabs', + browserName: 'firefox', + platform: 'linux', + version: 'latest-1' + }, + sl_safari_9: { + base: 'SauceLabs', + browserName: 'safari', + version: '9.0' + }, + // FIXME(ndhoule): Bad IE7/8 support in testing packages make these fail + // sl_ie_7: { + // base: 'SauceLabs', + // browserName: 'internet explorer', + // version: '7' + // }, + // sl_ie_8: { + // base: 'SauceLabs', + // browserName: 'internet explorer', + // version: '8' + // }, + sl_ie_9: { + base: 'SauceLabs', + browserName: 'internet explorer', + version: '9' + }, + sl_ie_10: { + base: 'SauceLabs', + browserName: 'internet explorer', + version: '10' + }, + sl_ie_11: { + base: 'SauceLabs', + browserName: 'internet explorer', + version: '11' + }, + sl_edge_latest: { + base: 'SauceLabs', + browserName: 'microsoftedge' + } +}; + +module.exports = function(config) { + baseConfig(config); + + if (!process.env.SAUCE_USERNAME || !process.env.SAUCE_ACCESS_KEY) { + throw new Error('SAUCE_USERNAME and SAUCE_ACCESS_KEY environment variables are required but are missing'); + } + + config.set({ + browserDisconnectTolerance: 1, + + singleRun: true, + + reporters: ['progress', 'junit', 'coverage'], + + browsers: ['PhantomJS'].concat(Object.keys(customLaunchers)), + + customLaunchers: customLaunchers, + + junitReporter: { + outputDir: process.env.TEST_REPORTS_DIR, + suite: require('./package.json').name + }, + + sauceLabs: { + testName: require('./package.json').name + }, + + coverageReporter: { + reporters: [ + { type: 'lcov' } + ] + } + }); +}; diff --git a/karma.conf.js b/karma.conf.js new file mode 100644 index 0000000..f607e2a --- /dev/null +++ b/karma.conf.js @@ -0,0 +1,53 @@ +/* eslint-env node */ +'use strict'; + +module.exports = function(config) { + config.set({ + files: [ + 'test/**/*.test.js' + ], + browserDisconnectTolerance: 5, + browserDisconnectTimeout: 100000, + browserNoActivityTimeout: 100000, + + browsers: ['PhantomJS'], + + frameworks: ['browserify', 'mocha'], + + reporters: ['spec', 'coverage'], + + preprocessors: { + 'test/**/*.js': 'browserify' + }, + + client: { + mocha: { + grep: process.env.GREP, + reporter: 'html', + timeout: 60000 + } + }, + + browserify: { + debug: true, + transform: [ + [ + 'browserify-istanbul', + { + instrumenterConfig: { + embedSource: true + } + } + ] + ] + }, + + coverageReporter: { + reporters: [ + { type: 'text' }, + { type: 'html' }, + { type: 'json' } + ] + } + }); +}; diff --git a/lib/index.js b/lib/index.js index 64a90fc..bcf0be7 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,14 +1,14 @@ +'use strict'; /** * Module dependencies. */ -var alias = require('alias'); -var convertDates = require('convert-dates'); -var defaults = require('defaults'); +var convertDates = require('@segment/convert-dates'); +var defaults = require('@ndhoule/defaults'); var del = require('obj-case').del; -var integration = require('analytics.js-integration'); -var is = require('is'); +var integration = require('@segment/analytics.js-integration'); +var is = require('is-type'); /** * Expose `Intercom` integration. @@ -47,7 +47,7 @@ Intercom.prototype.initialize = function() { */ Intercom.prototype.loaded = function() { - return is.fn(window.Intercom); + return typeof window.Intercom === 'function'; }; /** @@ -127,9 +127,12 @@ Intercom.prototype.identify = function(identify) { */ Intercom.prototype.group = function(group) { - var props = group.properties(); - props = alias(props, { createdAt: 'created' }); - props = alias(props, { created: 'created_at' }); + // using .traits here since group.properties() doesn't take alias object + var props = group.traits({ + createdAt: 'created', + created: 'created_at' + }); + props = convertDates(props, formatDate); var id = group.groupId(); if (id) props.id = id; api('update', { company: props }); diff --git a/package.json b/package.json index 1cb9bb0..a1705a4 100644 --- a/package.json +++ b/package.json @@ -1,32 +1,57 @@ { - "name": "analytics.js-integration-intercom", + "name": "@segment/analytics.js-integration-intercom", "description": "The Intercom analytics.js integration.", - "author": "Segment ", - "license": "MIT", + "version": "2.0.0", + "keywords": [ + "analytics.js", + "analytics.js-integration", + "segment", + "intercom" + ], "main": "lib/index.js", "scripts": { "test": "make test" }, - "engines": { - "node": ">=0.12.0", - "npm": ">=2.7.0" - }, "repository": { - "url": "https://github.com/segment-integrations/analytics.js-integration-intercom.git", - "type": "git" + "type": "git", + "url": "git+https://github.com/segment-integrations/analytics.js-integration-intercom.git" }, + "author": "Segment ", + "license": "SEE LICENSE IN LICENSE", "bugs": { "url": "https://github.com/segment-integrations/analytics.js-integration-intercom/issues" }, - "homepage": "https://segment.com/docs/integrations/intercom/", - "dependencies": {}, + "homepage": "https://github.com/segment-integrations/analytics.js-integration-intercom#readme", + "dependencies": { + "@ndhoule/defaults": "^2.0.1", + "@segment/analytics.js-integration": "^2.1.0", + "@segment/convert-dates": "^1.0.0", + "is-type": "0.0.1", + "obj-case": "^0.2.0" + }, "devDependencies": { - "duo": "0.12.x", - "duo-test": "0.2.x", - "eslint": "0.x", - "eslint-config-segment": "^1.0.10", + "@segment/analytics.js-core": "^3.0.0", + "@segment/analytics.js-integration-tester": "^2.0.0", + "@segment/clear-env": "^2.0.0", + "@segment/eslint-config": "^3.1.1", + "browserify": "^13.0.0", + "browserify-istanbul": "^2.0.0", + "eslint": "^2.9.0", + "eslint-plugin-mocha": "^2.2.0", + "eslint-plugin-require-path-exists": "^1.1.5", + "istanbul": "^0.4.3", + "karma": "^0.13.22", + "karma-browserify": "^5.0.4", + "karma-chrome-launcher": "^1.0.1", + "karma-coverage": "^1.0.0", + "karma-junit-reporter": "^1.0.0", + "karma-mocha": "^1.0.1", + "karma-phantomjs-launcher": "^1.0.0", + "karma-sauce-launcher": "^1.0.0", + "karma-spec-reporter": "0.0.26", "mocha": "^2.2.5", - "mocha-phantomjs": "segmentio/mocha-phantomjs#master", - "phantomjs": "^1.9.17" + "npm-check": "^5.2.1", + "phantomjs-prebuilt": "^2.1.7", + "watchify": "^3.7.0" } } diff --git a/test/.eslintrc b/test/.eslintrc index 7eeefc3..bab7e83 100644 --- a/test/.eslintrc +++ b/test/.eslintrc @@ -1,5 +1,3 @@ { - "env": { - "mocha": true - } + "extends": "@segment/eslint-config/mocha" } diff --git a/test/index.html b/test/index.html deleted file mode 100644 index c3a1a23..0000000 --- a/test/index.html +++ /dev/null @@ -1,36 +0,0 @@ - - - - integrations tests - - - -
- - - - - - diff --git a/test/index.test.js b/test/index.test.js index e124b81..720c72a 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -1,8 +1,9 @@ +'use strict'; -var Analytics = require('analytics.js-core').constructor; -var integration = require('analytics.js-integration'); -var sandbox = require('clear-env'); -var tester = require('analytics.js-integration-tester'); +var Analytics = require('@segment/analytics.js-core').constructor; +var integration = require('@segment/analytics.js-integration'); +var sandbox = require('@segment/clear-env'); +var tester = require('@segment/analytics.js-integration-tester'); var Intercom = require('../lib/'); describe('Intercom', function() { @@ -20,11 +21,12 @@ describe('Intercom', function() { analytics.add(intercom); }); - afterEach(function() { + afterEach(function(done) { analytics.restore(); analytics.reset(); intercom.reset(); sandbox(); + done(); }); it('should have the right settings', function() { @@ -311,23 +313,27 @@ describe('Intercom', function() { }); it('should work with .created_at', function() { - analytics.group('id', { name: 'Name', createdAt: 'Jan 1, 2000 3:32:33 PM' }); + var date = Date.now(); + date = Math.floor(date / 1000); + analytics.group('id', { name: 'Name', createdAt: date }); analytics.called(window.Intercom, 'update', { company: { id: 'id', name: 'Name', - created_at: 'Jan 1, 2000 3:32:33 PM' + created_at: date } }); }); it('should work with .created', function() { - analytics.group('id', { name: 'Name', created: 'Jan 1, 2000 3:32:33 PM' }); + var date = Date.now(); + date = Math.floor(date / 1000); + analytics.group('id', { name: 'Name', created: date }); analytics.called(window.Intercom, 'update', { company: { id: 'id', name: 'Name', - created_at: 'Jan 1, 2000 3:32:33 PM' + created_at: date } }); });