I bootstrap a simple Angular (v10.1) app and create Github action workflow as below. Note that I have configured test to run with recommended configurations as specified in Angular Testing doc
# ./github/workflows/main.yml
name: Test master branch
on:
push:
branches: [master]
pull_request:
branches: [master]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [14.10]
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: Cache Node.js modules
uses: actions/cache@v2
with:
# npm cache files are stored in `~/.npm` on Linux/macOS
path: ~/.npm
key: ${{ runner.OS }}-node-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.OS }}-node-
${{ runner.OS }}-
- name: Install dependencies
run: npm install
- name: Run lint
run: npm lint
- name: Run tests
run: npm test -- --no-watch --no-progress --browsers=ChromeHeadlessCI
The test fails with error Cannot load browser "ChromeHeadlessCI": it is not registered! Perhaps you are missing some plugin?
.
The same error is thrown even if I try running npm test -- --no-watch --no-progress --browsers=ChromeHeadlessCI
on local computer.
$ npm test -- --no-watch --no-progress --browsers=ChromeHeadlessCI
> [email protected] test C:\sampleapp\sampleapp-frontend
> ng test "--no-watch" "--no-progress" "--browsers=ChromeHeadlessCI"
12 09 2020 21:33:13.888:INFO [karma-server]: Karma v5.0.9 server started at http://0.0.0.0:9876/
12 09 2020 21:33:13.892:INFO [launcher]: Launching browsers ChromeHeadlessCI with concurrency unlimited
12 09 2020 21:33:13.894:ERROR [launcher]: Cannot load browser "ChromeHeadlessCI": it is not registered! Perhaps you are missing some plugin?
12 09 2020 21:33:13.895:ERROR [karma-server]: Error: Found 1 load error
at Server.<anonymous> (C:\sampleapp\sampleapp-frontend\node_modules\karma\lib\server.js:189:27)
at Object.onceWrapper (events.js:420:28)
at Server.emit (events.js:326:22)
at emitListeningNT (net.js:1351:10)
at processTicksAndRejections (internal/process/task_queues.js:79:21)
npm ERR! Test failed. See above for more details.
How can I fix this issue with ChromeHeadlessCI
?
Be sure to check that the following items are in karma.conf.js:
browsers: ['Chrome', 'ChromeHeadless', 'ChromeHeadlessCI'],
customLaunchers: {
ChromeHeadlessCI: {
base: 'ChromeHeadless',
flags: ['--no-sandbox']
}
},
This will help the launcher find ChromeHeadlessCI.
The above answers seem incomplete to me. Here's a working example:
We use puppeteer
to call & manage the headless-chrome instance.
We also install karma-chrome-launcher
to provide the plugin for Karma.
Run npm i -D puppeteer karma-chrome-launcher
to install both.
Three more code lines and we're good to go.
puppeteer
:process.env.CHROME_BIN = require('puppeteer').executablePath()
require('karma-chrome-launcher')]
ChromeHeadless
to the list of available browsers:browsers: ['ChromeHeadless', 'Firefox']
// karma.conf.js
process.env.CHROME_BIN = require('puppeteer').executablePath() // IMPORTANT!
module.exports = function(config) {
config.set({
browsers: ['ChromeHeadless', 'Firefox'], // IMPORTANT! You can list & use multiple browsers
plugins: [
require('karma-jasmine'),
require('karma-chrome-launcher')], // IMPORTANT!
require('karma-firefox-launcher'),
...
],
...
})
}
Execute ng test --watch=false --browsers=ChromeHeadless
& enjoy your tests!
I've added an alias inside package.json
that I call via npm run test-headless
in my GitHub Action.
"scripts": {
...,
"test-headless": "ng test --watch=false --browsers=ChromeHeadless",
...
When on desktop, I use Firefox via ng test --browsers=Firefox
.
Sources:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With