Skip to content

Instantly share code, notes, and snippets.

@vgebrev
Last active August 19, 2019 10:51
Show Gist options
  • Save vgebrev/61b98a00143410e9d517c999d706864c to your computer and use it in GitHub Desktop.
Save vgebrev/61b98a00143410e9d517c999d706864c to your computer and use it in GitHub Desktop.
Bitbucket Pipelines to run .NETCore and Angular tests on every Push and every Pull Request
# Bitbucket Pipelines to run .NETCore and Angular tests on every Push and every Pull Request
# At the time of writing: .NET Core 2.2, Node 10.16, Angular 8.2
# Example .net core test project: ./MyProject.Tests/MyProject.Tests.csproj
# Example asp.net core host for an angular app: ./MyProject.Web.UI/my-angular-app
image: microsoft/dotnet:sdk
definitions:
steps:
- step: &dotnet-test
name: Run dotnet tests
caches:
- dotnetcore
script:
- dotnet restore
- dotnet test MyProject.Tests
- step: &ng-test
name: Run ng tests
script:
#install node
- apt-get update
- apt-get -y install curl gnupg
- curl -sL https://deb.nodesource.com/setup_10.x | bash -
- apt-get -y install nodejs
#install chrome
- wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
- sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list'
- apt-get update
- apt-get install -y google-chrome-stable fonts-ipafont-gothic fonts-wqy-zenhei fonts-thai-tlwg fonts-kacst ttf-freefont --no-install-recommends
- rm -rf /var/lib/apt/lists/*
#install npm dependencies and run tests
- pushd ./MyProject.Web.UI/my-angular-app
- npm install
- npm install -g @angular/cli
- ng test --watch=false --browsers=ChromeHeadlessNoSandbox
- popd
pipelines:
pull-requests:
'**':
- step: *dotnet-test
- step: *ng-test
default:
- step: *dotnet-test
- step: *ng-test
# Setup: install Puppeteer as a dev dependency: npm install --save-dev puppeteer
process.env.CHROME_BIN = require('puppeteer').executablePath();
module.exports = function (config) {
config.set({
basePath: '',
frameworks: ['jasmine', '@angular-devkit/build-angular'],
plugins: [
require('karma-jasmine'),
require('karma-chrome-launcher'),
require('karma-jasmine-html-reporter'),
require('karma-coverage-istanbul-reporter'),
require('@angular-devkit/build-angular/plugins/karma')
],
client: {
clearContext: false // leave Jasmine Spec Runner output visible in browser
},
coverageIstanbulReporter: {
dir: require('path').join(__dirname, './coverage/my-angular-app'),
reports: ['html', 'lcovonly', 'text-summary'],
fixWebpackSourcePaths: true
},
reporters: ['progress', 'kjhtml'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['ChromeHeadlessNoSandbox', 'Chrome'],
customLaunchers: {
ChromeHeadlessNoSandbox: {
base: 'ChromeHeadless',
flags: ['--no-sandbox']
}
},
singleRun: false,
restartOnFileChange: true
});
};
@vgebrev
Copy link
Author

vgebrev commented Aug 19, 2019

This uses Puppeteer (https://github.com/GoogleChrome/puppeteer) to let karma run chrome headless in the pipelines container, so we need to setup karma accordingly:

  1. Puppeteer needs to be added as a dev dependency to the Angular app.
    npm install --save-dev puppeteer

2.1. In karma.config.js:
set CHROME_BIN to puppteer's executable path at the top of the file:
process.env.CHROME_BIN = require('puppeteer').executablePath();

2.2. We also add a custom launcher with a --no-sandbox parameter because the container is running as root:

browsers: ['ChromeHeadlessNoSandbox', 'Chrome'],
customLaunchers: {
    ChromeHeadlessNoSandbox: {
      base: 'ChromeHeadless',
      flags: ['--no-sandbox']
    }
},
  1. In the pipeline, we run ng test using the custom launcher:
    ng test --watch=false --browsers=ChromeHeadlessNoSandbox

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment