Skip to content

Conversation

@gayanW
Copy link
Contributor

@gayanW gayanW commented Nov 26, 2025

Karma Project Usage

Create project:

npm init -y
npm install --save-dev karma karma-jasmine jasmine-core karma-chrome-launcher karma-json-reporter

The instructions are based on:
https://karma-runner.github.io/6.4/intro/installation.html

Generate karma.conf.js:

npx karma init

# Answer the prompts:
# - Framework: jasmine
# - Require.js: no
# - Browser: ChromeHeadless
# - Test files: 
# - Files to exclude:
# - Watch files: no

Add following to karma.conf.js while keeping the current settings:

module.exports = function (config) {
  config.set({
    files: process.env.KARMA_FILES ? JSON.parse(process.env.KARMA_FILES) : [],
    ...
    plugins: [
      ...
      require('karma-json-reporter')
    ],
    jsonReporter: {
      outputFile: require('path').join(__dirname, 'test-results.json'),
      stdout: false
    },
    reporters: [..., 'json']
  });
};

Create a test file:

mkdir test

cat > test/example.spec.js << 'EOF'
describe('Example', function() {
  it('should pass', function() {
    expect(true).toBe(true);
  });

  it('should add numbers', function() {
    expect(1 + 1).toBe(2);
  });
});
EOF

Record session:

git add . && git commit -m "Initial commit"
launchable record build --name ${BUILD_NAME}
launchable record session --build ${BUILD_NAME} > session.txt

Run all tests:

find test -name "*.spec.ts" -o -name "*.spec.js" > test_list.txt
cat test_list.txt
KARMA_FILES=$(cat test_list.txt | jq -R -s -c 'split("\n")[:-1]')
npx karma start --single-run

Record tests:

launchable record tests karma test-results.json

Request subset:

cat test_list.txt | launchable subset --target 25% karma  > subset.txt

Run subset of tests:

KARMA_FILES=$(cat subset.txt | jq -R -s -c 'split("\n")[:-1]')
npx karma start --single-run

Angular Project with Karma Usage

Create project:

ng new ng-karma-app --test-runner=karma
cd ng-karma-app
npm install --save-dev karma karma-chrome-launcher karma-coverage karma-jasmine karma-jasmine-html-reporter jasmine-core karma-json-reporter @types/jasmine
ng test --no-watch --no-progress --browsers=ChromeHeadless

The instructions are based on:

Generate karma.conf.js:

ng generate config karma

Add following to karma.conf.js while keeping the current settings:

module.exports = function (config) {
  config.set({
    ...
    plugins: [
      ...
      require('karma-json-reporter')
    ],
    jsonReporter: {
      outputFile: require('path').join(__dirname, 'test-results.json'),
      stdout: false
    },
    reporters: [..., 'json']
  });
};

Record session:

git add . && git commit -m "Initial commit"
launchable record build --name ${BUILD_NAME}
launchable record session --build ${BUILD_NAME} > session.txt

Record tests:

ng test --no-watch --no-progress --browsers=ChromeHeadless
launchable record tests karma test-results.json

Subset tests with ng:

ng test --list-tests | grep src > test_list.txt
cat test_list.txt | launchable subset --target 25% karma --with ng > subset.txt

Run subset of tests

ng test --no-watch --no-progress --browsers=ChromeHeadless --include $(cat subset.txt)

@gayanW gayanW force-pushed the LCHIB-668-1 branch 3 times, most recently from 921a158 to b54b15c Compare November 26, 2025 00:31
@gayanW gayanW merged commit deb78d9 into v1 Nov 26, 2025
13 checks passed
@gayanW gayanW deleted the LCHIB-668-1 branch November 26, 2025 01:55
@github-actions github-actions bot mentioned this pull request Nov 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants