While there is an abundance of resources on how to unit test with Webpack and Jasmine for Angular projects, I am working on a project that utilizes 'plain' TypeScript instead of AngularJs.
I have TypeScript classes in my project but do not use components. I am struggling to apply the information I find to a non-AngularJs project as everything seems to be focused on using components.
How can I integrate Jasmine (ts spec files) into a Typescript Webpack project?
I would prefer a solution that involves using a separate webpack configuration for the tests.
My setup/what I have to work with:
package.json I use a start script that launches node with dev-build
{
"name": "webpack.typescript",
"version": "1.0.0",
"description": "Webpack + TypeScript",
"main": "dev-build.js",
"author": "Shane Osbourne and John Lindquist",
"license": "MIT",
"scripts": {
"start": "node dev-build"
},
"dependencies": {
"bootstrap": "^3.3.7",
"lodash": "^4.17.4"
},
"devDependencies": {
"@types/lodash": "^4.14.53",
"browser-sync": "^2.18.8",
"bs-pretty-message": "^1.0.8",
"css-loader": "^0.26.2",
"node-sass": "^4.5.0",
"sass-loader": "^6.0.2",
"style-loader": "^0.13.2",
"ts-loader": "^2.0.1",
"typescript": "^2.2.1",
"url-loader": "^0.5.8",
"webpack": "^2.2.1",
"webpack-dev-middleware": "^1.10.1"
}
}
dev-build.js This file loads config, bundles the code, and starts BrowserSync.
/**
* Require Browsersync along with webpack and middleware for it
*/
var browserSync = require('browser-sync').create();
var webpack = require('webpack');
var webpackDevMiddleware = require('webpack-dev-middleware');
/**
* Require ./dev-webpack.config.js and make a bundler from it
*/
var webpackConfig = require('./dev-webpack.config');
var bundler = webpack(webpackConfig);
/**
* Reload all devices when bundle is complete or send a fullscreen error message to the browser instead
*/
bundler.plugin('done', function (stats) {
if (stats.hasErrors() || stats.hasWarnings()) {
return browserSync.sockets.emit('fullscreen:message', {
title: "Error",
timeout: 100000
});
}
browserSync.reload();
});
/**
* Run Browsersync and use middleware for Hot Module Replacement
*/
browserSync.init({
server: 'app',
open: false,
logFileChanges: false,
middleware: [
webpackDevMiddleware(bundler, {
publicPath: webpackConfig.output.publicPath,
stats: {colors: true}
})
],
plugins: ['bs-pretty-message'],
files: [
'app/css/*.css',
'app/*.html'
]
});
dev-webpack.config.js This file handles typescript and scss. (I was thinking maybe I should also have a test-webpack.config.js)
var webpack = require('webpack');
var path = require('path');
module.exports = {
devtool: '#inline-source-map',
entry: [
'./src/main.ts',
'./src/main.scss'
],
output: {
path: path.join(__dirname, 'app'),
publicPath: '/',
filename: 'dist/bundle.js'
},
plugins: [
new webpack.NoEmitOnErrorsPlugin(),
new webpack.LoaderOptionsPlugin({
debug: true
})
],
resolve: {
extensions: ['.ts', '.js', '.scss']
},
module: {
rules: [
{
test: /\.ts$/,
use: [{
loader: 'ts-loader'
}]
},
{
test: /\.woff($|\?)|\.woff2($|\?)|\.ttf($|\?)|\.eot($|\?)|\.svg($|\?)/,
use: [{
loader: 'url-loader'
}]
},
{
test: /\.scss$/,
use: [{
loader: "style-loader"
}, {
loader: "css-loader",
options: {
sourceMap: true
}
}, {
loader: "sass-loader",
options: {
sourceMap: true
}
}]
}
],
}
};
What I've come across in my search:
Unit testing with Webpack and Mocha
Unit testing with Webpack, Jasmine (-core), typescript
Jasmine Spec as Typescript File
TypeScript compilation failure and Karma test execution?
Executing Typescript Jasmine tests via Webpack (Terminal) to test Angular2