Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 24 additions & 15 deletions launchable/test_runners/karma.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# This runner only supports recording tests
# For subsetting, use 'ng' test runner instead
# It's possible to use 'karma' runner for recording, and 'ng' runner for subsetting, for the same test session
import json
from typing import Dict, Generator, List

Expand All @@ -11,18 +8,6 @@
from . import launchable


@click.option('--with', '_with')
@launchable.subset
def subset(client, _with: str):
# TODO: implement the --with ng option

# read lines as test file names
for t in client.stdin():
client.test_path(t.rstrip("\n"))

client.run()


@click.argument('reports', required=True, nargs=-1)
@launchable.record.tests
def record_tests(client, reports):
Expand All @@ -34,6 +19,30 @@ def record_tests(client, reports):
client.run()


@click.option('--with', '_with', type=str, default=None,
help='Format output for specific test runner (e.g., "ng" for Angular CLI)')
@launchable.subset
def subset(client, _with):
"""
Usage:
find src -name "*.spec.ts" -o -name "*.spec.js" > test-list.txt
cat test-list.txt | launchable subset --target 10% karma

# Output in ng test format
find src -name "*.spec.ts" | launchable subset --target 10% karma --with ng
"""
for t in client.stdin():
path = t.strip()
if path:
client.test_path(path)

if _with == 'ng':
client.formatter = lambda x: "--include={}".format(x[0]['name'])
client.separator = " "

client.run()


class JSONReportParser:
"""
Sample Karma report format:
Expand Down
155 changes: 155 additions & 0 deletions tests/data/karma/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
Karma
======

### Karma Project

**Create project:**

```bash
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`:**
```bash
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:**
```bash
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:**
```bash
git add . && git commit -m "Initial commit"
launchable record build --name ${BUILD_NAME}
launchable record session --build ${BUILD_NAME} > session.txt
```

**Run all tests:**
```bash
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:**
```bash
launchable record tests karma test-results.json
```

**Request subset:**
```bash
cat test_list.txt | launchable subset --target 25% karma > subset.txt
```

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

### Angular Project with Karma

**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:
- https://angular.dev/guide/testing/karma
- https://www.npmjs.com/package/karma-json-reporter

**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)
```
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
{ "type": "file", "name": "foo/bar/zot.spec.ts" }
],
[
{ "type": "file", "name": "client-source/src/app/shared/other-test.spec.ts" }
{ "type": "file", "name": "foo/bar/another.spec.ts" }
]
],
"testRunner": "ng",
"testRunner": "karma",
"goal": {"type": "subset-by-percentage", "percentage": 0.1},
"ignoreNewTests": false,
"session": { "id": "16" },
"getTestsFromGuess": false,
"getTestsFromPreviousSessions": false
}
}
28 changes: 27 additions & 1 deletion tests/test_runners/test_karma.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,37 @@ def test_record_tests_json(self):

@responses.activate
@mock.patch.dict(os.environ, {"LAUNCHABLE_TOKEN": CliTestCase.launchable_token})
def test_subset(self):
def test_subset_with_base(self):
# emulate launchable record build
write_build(self.build_name)

result = self.cli('subset', '--target', '10%', '--base',
os.getcwd(), 'karma', '--with', 'ng', input="a.ts\nb.ts")
self.assert_success(result)
self.assert_subset_payload('subset_result.json')

@responses.activate
@mock.patch.dict(os.environ,
{"LAUNCHABLE_TOKEN": CliTestCase.launchable_token})
def test_subset(self):
write_build(self.build_name)

subset_input = """foo/bar/zot.spec.ts
foo/bar/another.spec.ts
"""
result = self.cli('subset', '--target', '10%', 'karma', input=subset_input)
self.assert_success(result)
self.assert_subset_payload('subset_payload.json')

@responses.activate
@mock.patch.dict(os.environ,
{"LAUNCHABLE_TOKEN": CliTestCase.launchable_token})
def test_subset_with_ng(self):
write_build(self.build_name)

subset_input = """foo/bar/zot.spec.ts
foo/bar/another.spec.ts
"""
result = self.cli('subset', '--target', '10%', 'karma', '--with', 'ng', input=subset_input)
self.assert_success(result)
self.assert_subset_payload('subset_payload.json')
22 changes: 0 additions & 22 deletions tests/test_runners/test_ng.py

This file was deleted.

Loading