Skip to content

Commit f72d5f9

Browse files
committed
Add example project for karma
1 parent 634ed91 commit f72d5f9

8 files changed

Lines changed: 2198 additions & 0 deletions

File tree

.github/workflows/karma.yaml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
name: karma
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
paths:
8+
- "karma/**"
9+
pull_request:
10+
paths:
11+
- "karma/**"
12+
- .github/workflows/karma.yaml
13+
14+
env:
15+
LAUNCHABLE_TOKEN: ${{ secrets.LAUNCHABLE_TOKEN_KARMA }}
16+
LAUNCHABLE_DEBUG: 1
17+
LAUNCHABLE_REPORT_ERROR: 1
18+
19+
jobs:
20+
tests:
21+
runs-on: ubuntu-latest
22+
defaults:
23+
run:
24+
working-directory: karma
25+
steps:
26+
- uses: actions/checkout@v4
27+
- uses: actions/setup-python@v5
28+
- uses: actions/setup-node@v4
29+
with:
30+
node-version: '22'
31+
- name: Set up JDK
32+
uses: actions/setup-java@v4
33+
with:
34+
java-version: '11'
35+
distribution: 'temurin'
36+
- name: Install Launchable CLI
37+
run: pip install launchable
38+
- name: Launchable verify
39+
run: launchable verify
40+
- name: Install dependencies
41+
run: npm install
42+
- name: Record build
43+
run: launchable record build --name "$GITHUB_RUN_ID"
44+
- name: Save all tests to a file
45+
run: find test -name "*.spec.ts" -o -name "*.spec.js" > test_list.txt
46+
- name: Run all tests
47+
run: |
48+
KARMA_FILES=$(cat test_list.txt | jq -R -s -c 'split("\n")[:-1]')
49+
npx karma start --single-run
50+
- name: Record tests
51+
run: launchable record tests karma test-results.json
52+
- name: Request subset
53+
run: cat test_list.txt | launchable subset --target 25% karma > subset.txt
54+
- name: Run subset of tests
55+
run: |
56+
KARMA_FILES=$(cat subset.txt | jq -R -s -c 'split("\n")[:-1]')
57+
npx karma start --single-run

karma/.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
node_modules
2+
.launchable
3+
.python-version
4+
*.txt
5+
test-results.json
6+
TESTS.xml

karma/README.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
Karma
2+
======
3+
4+
**Steps taken to create this project:**
5+
6+
```bash
7+
npm init -y
8+
npm install --save-dev karma karma-jasmine jasmine-core karma-chrome-launcher karma-json-reporter karma-reports-with-file-paths
9+
```
10+
The instructions are based on:
11+
12+
https://karma-runner.github.io/6.4/intro/installation.html
13+
14+
**Generate `karma.conf.js`:**
15+
```bash
16+
npx karma init
17+
18+
# Answer the prompts:
19+
# - Framework: jasmine
20+
# - Require.js: no
21+
# - Browser: ChromeHeadless
22+
# - Test files:
23+
# - Files to exclude:
24+
# - Watch files: no
25+
```
26+
27+
**Add following to `karma.conf.js` while keeping the current settings:**
28+
```
29+
module.exports = function (config) {
30+
config.set({
31+
files: process.env.KARMA_FILES ? JSON.parse(process.env.KARMA_FILES) : [],
32+
...
33+
preprocessors: {
34+
'**/*.spec.js': ['reports-with-file-paths']
35+
},
36+
...
37+
plugins: [
38+
...
39+
require('karma-json-reporter'),
40+
require('karma-reports-with-file-paths')
41+
],
42+
jsonReporter: {
43+
outputFile: require('path').join(__dirname, 'test-results.json'),
44+
stdout: false
45+
},
46+
reporters: [..., 'json']
47+
});
48+
};
49+
```
50+
51+
**Create a test file:**
52+
```bash
53+
mkdir test
54+
55+
cat > test/foo.spec.js << 'EOF'
56+
describe('Foo', function() {
57+
it('should pass', function() {
58+
expect(true).toBe(true);
59+
});
60+
61+
it('should add numbers', function() {
62+
expect(1 + 1).toBe(2);
63+
});
64+
});
65+
EOF
66+
```
67+
68+
**Record session:**
69+
```bash
70+
git init && git add . && git commit -m "Initial commit"
71+
launchable record build --name ${BUILD_NAME}
72+
launchable record session --build ${BUILD_NAME} > session.txt
73+
```
74+
75+
**Run all tests:**
76+
```bash
77+
find test -name "*.spec.ts" -o -name "*.spec.js" > test_list.txt
78+
KARMA_FILES=$(cat test_list.txt | jq -R -s -c 'split("\n")[:-1]')
79+
npx karma start --single-run
80+
```
81+
82+
**Record tests:**
83+
```bash
84+
launchable record tests karma test-results.json
85+
```
86+
87+
**Request subset:**
88+
```bash
89+
cat test_list.txt | launchable subset --target 25% karma > subset.txt
90+
```
91+
92+
**Run subset of tests:**
93+
```bash
94+
KARMA_FILES=$(cat subset.txt | jq -R -s -c 'split("\n")[:-1]')
95+
npx karma start --single-run
96+
```

karma/karma.conf.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Karma configuration
2+
// Generated on Tue Nov 25 2025 12:14:27 GMT+0900 (Japan Standard Time)
3+
4+
module.exports = function(config) {
5+
config.set({
6+
7+
// base path that will be used to resolve all patterns (eg. files, exclude)
8+
basePath: '',
9+
10+
11+
// frameworks to use
12+
// available frameworks: https://www.npmjs.com/search?q=keywords:karma-adapter
13+
frameworks: ['jasmine'],
14+
15+
16+
// list of files / patterns to load in the browser
17+
files: process.env.KARMA_FILES ? JSON.parse(process.env.KARMA_FILES) : [],
18+
19+
20+
// list of files / patterns to exclude
21+
exclude: [
22+
],
23+
24+
25+
// preprocess matching files before serving them to the browser
26+
// available preprocessors: https://www.npmjs.com/search?q=keywords:karma-preprocessor
27+
preprocessors: {
28+
'**/*.spec.js': ['reports-with-file-paths']
29+
},
30+
31+
32+
plugins: [
33+
require('karma-jasmine'),
34+
require('karma-chrome-launcher'),
35+
require('karma-junit-reporter'),
36+
require('karma-json-reporter'),
37+
require('karma-reports-with-file-paths')
38+
],
39+
40+
jsonReporter: {
41+
outputFile: require('path').join(__dirname, 'test-results.json'),
42+
stdout: false
43+
},
44+
45+
junitReporter: {
46+
useBrowserName: false
47+
},
48+
49+
// test results reporter to use
50+
// possible values: 'dots', 'progress'
51+
// available reporters: https://www.npmjs.com/search?q=keywords:karma-reporter
52+
reporters: ['progress', 'json', 'junit'],
53+
54+
55+
// web server port
56+
port: 9876,
57+
58+
59+
// enable / disable colors in the output (reporters and logs)
60+
colors: true,
61+
62+
63+
// level of logging
64+
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
65+
logLevel: config.LOG_INFO,
66+
67+
68+
// enable / disable watching file and executing tests whenever any file changes
69+
autoWatch: false,
70+
71+
72+
// start these browsers
73+
// available browser launchers: https://www.npmjs.com/search?q=keywords:karma-launcher
74+
browsers: ['ChromeHeadless'],
75+
76+
77+
// Continuous Integration mode
78+
// if true, Karma captures browsers, runs the tests and exits
79+
singleRun: false,
80+
81+
// Concurrency level
82+
// how many browser instances should be started simultaneously
83+
concurrency: Infinity
84+
})
85+
}

0 commit comments

Comments
 (0)