Skip to content

Commit

Permalink
refactor(checkbox): Add Jasmine test for checkbox foundation. (#5299)
Browse files Browse the repository at this point in the history
- Add checkbox foundation test using Jasmine framework (converted from existing Mocha test). Does not delete Mocha test yet; this will be done in a follow-up PR.
- Set up Karma config such that `npm run test:jasmineunit` runs Karma/Jasmine unit tests, `npm run test:mochaunit` runs Karma/Mocha unit tests, and `npm run test:unit` runs both.
- Set up code coverage via `karma-typescript`/`istanbul`. Thresholds are lowered for now since Jasmine tests do not cover component tests yet, which were increasing the coverage percentage.
  • Loading branch information
joyzhong authored Dec 9, 2019
1 parent 4faa4dd commit a0da497
Show file tree
Hide file tree
Showing 9 changed files with 1,622 additions and 155 deletions.
180 changes: 118 additions & 62 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ const webpackConfig = require('./webpack.config')[1];

const USING_TRAVISCI = Boolean(process.env.TRAVIS);
const USING_SL = Boolean(process.env.SAUCE_USERNAME && process.env.SAUCE_ACCESS_KEY);
// If true, runs new suite of Jasmine foundation unit tests.
// Otherwise, runs old Mocha unit tests.
const USE_JASMINE = Boolean(process.env.USE_JASMINE);

// Files to include in Jasmine tests.
const FILES_TO_USE = [
'packages/*/!(node_modules)/**/!(*.d).ts',
'packages/*/!(*.d).ts',
'testing/**/*.ts',
];

const HEADLESS_LAUNCHERS = {
/** See https://github.com/travis-ci/travis-ci/issues/8836#issuecomment-348248951 */
Expand Down Expand Up @@ -63,79 +73,125 @@ const istanbulInstrumenterLoader = {
include: path.resolve('./packages'),
};

module.exports = function(config) {
config.set({
basePath: '',
// Refer https://github.com/karma-runner/karma-mocha
frameworks: ['mocha'],
files: [
// Refer https://github.com/babel/karma-babel-preprocessor
'node_modules/@babel/polyfill/dist/polyfill.js',
'test/unit/index.js',
],
preprocessors: {
'test/unit/index.js': ['webpack', 'sourcemap'],
const mochaConfig = {
basePath: '',
// Refer https://github.com/karma-runner/karma-mocha
frameworks: ['mocha'],
files: [
// Refer https://github.com/babel/karma-babel-preprocessor
'node_modules/@babel/polyfill/dist/polyfill.js',
'test/unit/index.js',
],
preprocessors: {
'test/unit/index.js': ['webpack', 'sourcemap'],
},
reporters: ['progress', 'coverage-istanbul'],

coverageIstanbulReporter: {
'dir': 'coverage',
'reports': ['html', 'lcovonly', 'json'],
'report-config': {
lcovonly: {subdir: '.'},
json: {subdir: '.', file: 'coverage.json'},
},
// Set 'emitWarning = true' to NOT fail tests if the thresholds are not met
'emitWarning': false,
'thresholds': {
statements: 95,
branches: 95,
lines: 95,
functions: 95,
},
},

client: {
mocha: {
reporter: 'html',
ui: 'qunit',

// Number of milliseconds to wait for an individual `test(...)` function to complete.
// The default is 2000.
timeout: 10000,
},
},

webpackMiddleware: {
noInfo: true,
stats: 'minimal',
},
};

const jasmineConfig = {
basePath: '',
files: FILES_TO_USE,
frameworks: ['jasmine', 'karma-typescript'],
karmaTypescriptConfig: {
coverageOptions: {
threshold: {
global: {
// TODO: Raise threshold to at least 90% after more tests have been migrated.
statements: 80,
branches: 80,
functions: 50,
lines: 80,
excludes: [
'testing/**/*.ts',
'packages/!(mdc-checkbox)/**/*',
'packages/**/component.ts', // Jasmine tests cover foundation/adapter only.
],
},
},
},
reports: {
html: 'coverage',
lcovonly: 'coverage',
json: {
directory: 'coverage',
filename: 'coverage.json',
},
},
reporters: ['progress', 'coverage-istanbul'],
tsconfig: './tsconfig.json',
},
preprocessors: FILES_TO_USE.reduce((obj, file) => {
obj[file] = 'karma-typescript';
return obj;
}, {}),
reporters: ['progress', 'karma-typescript'],
};

module.exports = function(config) {
config.set(Object.assign(USE_JASMINE ? jasmineConfig : mochaConfig, {
logLevel: config.LOG_INFO,
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
browsers: browsers,
browserDisconnectTimeout: 40000,
browserNoActivityTimeout: 120000,
captureTimeout: 240000,
concurrency: USING_SL ? 4 : Infinity,
customLaunchers: customLaunchers,
}));

coverageIstanbulReporter: {
'dir': 'coverage',
'reports': ['html', 'lcovonly', 'json'],
'report-config': {
lcovonly: {subdir: '.'},
json: {subdir: '.', file: 'coverage.json'},
},
// 'emitWarning' causes the tests to fail if the thresholds are not met
'emitWarning': false,
'thresholds': {
statements: 95,
branches: 95,
lines: 95,
functions: 95,
},
},

client: {
mocha: {
reporter: 'html',
ui: 'qunit',

// Number of milliseconds to wait for an individual `test(...)` function to complete.
// The default is 2000.
timeout: 10000,
},
},

// Refer https://github.com/webpack-contrib/karma-webpack
webpack: Object.assign({}, webpackConfig, {
plugins: [], // Exclude UglifyJs plugin from test build.
mode: 'development',
module: Object.assign({}, webpackConfig.module, {
// Cover source files when not debugging tests. Otherwise, omit coverage instrumenting to get
// uncluttered source maps.
rules: webpackConfig.module.rules.concat(config.singleRun ? [Object.assign({
enforce: 'post',
test: /\.ts$/,
}, istanbulInstrumenterLoader), Object.assign({
test: /\.js$/,
}, istanbulInstrumenterLoader)] : []),
if (!USE_JASMINE) {
// Need to set webpack here rather than in `mochaConfig` to read `config.singleRun`.
config.set({
// Refer https://github.com/webpack-contrib/karma-webpack
webpack: Object.assign({}, webpackConfig, {
plugins: [], // Exclude UglifyJs plugin from test build.
mode: 'development',
module: Object.assign({}, webpackConfig.module, {
// Cover source files when not debugging tests. Otherwise, omit coverage instrumenting to get
// uncluttered source maps.
rules: webpackConfig.module.rules.concat(config.singleRun ? [Object.assign({
enforce: 'post',
test: /\.ts$/,
}, istanbulInstrumenterLoader), Object.assign({
test: /\.js$/,
}, istanbulInstrumenterLoader)] : []),
}),
}),
}),

webpackMiddleware: {
noInfo: true,
stats: 'minimal',
},
});
});
}

if (USING_SL) {
const sauceLabsConfig = {
Expand Down
Loading

0 comments on commit a0da497

Please sign in to comment.