-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(infrastructure): Set up testing
* `npm test` lints files, runs karma, reports coverages, checks coverage thresholds * `npm run test:watch` runs karma in auto-watch mode, with source maps for both source and test files. * [karma](https://karma-runner.github.io/1.0/index.html) is used for running tests. * [tape](https://github.com/substack/tape) is used as the actual test runner. * [bel](https://github.com/shama/bel) is used for easy DOM fixtures. * [testdouble](https://github.com/testdouble/testdouble.js) is used for mocking/doubles. * [isparta](https://github.com/douglasduteil/isparta) is used to instrument source files for coverage. * [istanbul](https://github.com/gotwarlost/istanbul) is used to check and report coverage. resolves #4465
- Loading branch information
1 parent
3203fe7
commit 1aa5162
Showing
6 changed files
with
183 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
node_modules | ||
.DS_Store | ||
/build | ||
/coverage | ||
packages/*/dist | ||
*.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
const path = require('path'); | ||
const webpackConfig = require('./webpack.config')[0]; | ||
|
||
// TODO: Sourcemaps | ||
// TODO: eslint-plugin-tape | ||
module.exports = function(config) { | ||
config.set({ | ||
basePath: '', | ||
frameworks: ['tap'], | ||
files: [ | ||
'test/unit/index.js' | ||
], | ||
preprocessors: { | ||
'test/unit/index.js': ['webpack', 'sourcemap'] | ||
}, | ||
reporters: ['dots', 'coverage'], | ||
port: 9876, | ||
colors: true, | ||
logLevel: config.LOG_INFO, | ||
browsers: ['Chrome'], | ||
concurrency: Infinity, | ||
|
||
coverageReporter: { | ||
dir: 'coverage', | ||
reporters: [ | ||
{type: 'json', subdir: '.', file: 'coverage.json'}, | ||
{type: 'html'} | ||
] | ||
}, | ||
|
||
webpack: Object.assign({}, webpackConfig, { | ||
devtool: 'inline-source-map', | ||
node: { | ||
fs: 'empty' | ||
}, | ||
module: Object.assign({}, webpackConfig.module, { | ||
// Cover source files when not debugging tests. Otherwise, omit coverage instrumenting to get | ||
// uncluttered source maps. | ||
loaders: webpackConfig.module.loaders.concat([config.singleRun ? { | ||
test: /\.js$/, | ||
include: path.resolve('./packages'), | ||
loader: 'isparta' | ||
} : undefined]).filter(Boolean) | ||
}) | ||
}), | ||
|
||
webpackMiddleware: { | ||
noInfo: true | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/** @fileoverview Bootstraps the test bundle for karma-webpack. */ | ||
|
||
const testsContext = require.context('.', true, /\.test\.js$/); | ||
testsContext.keys().forEach(testsContext); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import bel from 'bel'; | ||
import td from 'testdouble'; | ||
import test from 'tape'; | ||
import mdlAutoInit from '../../../packages/mdl-auto-init'; | ||
|
||
class FakeComponent { | ||
static attachTo(node) { | ||
return new this(node); | ||
} | ||
|
||
constructor(node) { | ||
this.node = node; | ||
} | ||
} | ||
|
||
const createFixture = () => bel` | ||
<div id="root"> | ||
<p data-mdl-auto-init="FakeComponent" class="mdl-fake">Fake Element</p> | ||
</div> | ||
`; | ||
|
||
const setupTest = () => { | ||
mdlAutoInit.deregisterAll(); | ||
mdlAutoInit.register(FakeComponent.name, FakeComponent); | ||
return createFixture(); | ||
}; | ||
|
||
test('calls attachTo() on components registered for identifier on nodes w/ data-mdl-auto-init attr', t => { | ||
t.plan(1); | ||
|
||
const root = setupTest(); | ||
mdlAutoInit(root); | ||
|
||
t.true(root.querySelector('.mdl-fake').FakeComponent instanceof FakeComponent); | ||
}); | ||
|
||
test('passes the node where "data-mdl-auto-init" was found to attachTo()', t => { | ||
t.plan(1); | ||
|
||
const root = setupTest(); | ||
mdlAutoInit(root); | ||
|
||
const fake = root.querySelector('.mdl-fake'); | ||
t.equal(fake.FakeComponent.node, fake); | ||
}); | ||
|
||
test('throws when no constructor name is specified within "data-mdl-auto-init"', t => { | ||
t.plan(1); | ||
|
||
const root = setupTest(); | ||
root.querySelector('.mdl-fake').dataset.mdlAutoInit = ''; | ||
|
||
t.throws(() => mdlAutoInit(root)); | ||
}); | ||
|
||
test('throws when constructor is not registered', t => { | ||
t.plan(1); | ||
|
||
const root = setupTest(); | ||
root.querySelector('.mdl-fake').dataset.mdlAutoInit = 'MDLUnregisteredComponent'; | ||
|
||
t.throws(() => mdlAutoInit(root)); | ||
}); | ||
|
||
test('warns when autoInit called multiple times on a node', t => { | ||
t.plan(1); | ||
|
||
const root = setupTest(); | ||
const warn = td.func('warn'); | ||
const {contains} = td.matchers; | ||
|
||
mdlAutoInit(root, warn); | ||
mdlAutoInit(root, warn); | ||
|
||
t.doesNotThrow(() => td.verify(warn(contains('(mdl-auto-init) Component already initialized')))); | ||
}); | ||
|
||
test('#register throws when Ctor is not a function', t => { | ||
t.plan(1); | ||
t.throws(() => mdlAutoInit.register('not-a-function', 'Not a function')); | ||
}); | ||
|
||
test('#register warns when registered key is being overridden', t => { | ||
t.plan(1); | ||
|
||
const warn = td.func('warn'); | ||
const {contains} = td.matchers; | ||
|
||
mdlAutoInit.register(FakeComponent.name, () => ({overridden: true}), warn); | ||
|
||
t.true(() => td.verify(warn(contains('(mdl-auto-init) Overriding registration')))); | ||
}); |