Skip to content

Commit

Permalink
combine unit and e2e coverage, simplify test scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
morris committed Jan 29, 2024
1 parent 9991ec2 commit dd29d07
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 38 deletions.
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -570,12 +570,12 @@ just one dependency.

Reference:

- [addItem.test.mjs](./test/e2e/addItem.test.mjs)
- [util.test.mjs](./test/unit/util.test.mjs)
- [addItem.test.js](./test/e2e/addItem.test.js)
- [util.test.js](./test/unit/util.test.js)

#### 4.3.1. Code Coverage

I was able to set up code coverage (at least for end-to-end tests) via
I was able to set up code coverage for unit _and_ end-to-end tests via
[Playwright's code coverage feature](https://playwright.dev/docs/api/class-coverage)
and [c8](https://github.com/bcoe/c8). This introduced another dependency and was
slightly more involved to get right, e.g. mapping localhost URLs to file URLs.
Expand All @@ -589,7 +589,7 @@ Note that the implementation is specific to the project structure, e.g.
Reference:

- [test-coverage.sh](./scripts/test-coverage.sh)
- [coverage.mjs](./test/coverage.mjs)
- [coverage.js](./test/coverage.js)

### 4.4. Pipeline

Expand Down Expand Up @@ -880,6 +880,11 @@ Thanks!

## 9. Changelog

### 01/2024

- Correctly combine [code coverage](#431-code-coverage) from end-to-end and unit
tests

### 12/2023

- Added [debugging section](#45-debugging)
Expand Down
52 changes: 24 additions & 28 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
},
"devDependencies": {
"@playwright/test": "^1.33.0",
"c8": "^8.0.1",
"c8": "^9.1.0",
"eslint": "^8.20.0",
"eslint-plugin-compat": "^4.0.2",
"mime": "^4.0.0",
Expand Down
3 changes: 1 addition & 2 deletions scripts/test-coverage.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
set -e
rm -rf coverage
COVERAGE=true playwright test $1
c8 report --src public --reporter text --reporter lcov
c8 --src public --reporter text --reporter lcov playwright test $1
6 changes: 3 additions & 3 deletions test/coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { test } from 'playwright/test';

// See also https://playwright.dev/docs/api/class-coverage

if (process.env.COVERAGE) {
if (process.env.NODE_V8_COVERAGE) {
test.beforeEach(async ({ page }) => {
await page.coverage.startJSCoverage();
});
Expand All @@ -20,9 +20,9 @@ if (process.env.COVERAGE) {
})),
};

await fs.mkdir('coverage/tmp', { recursive: true });
await fs.mkdir(process.env.NODE_V8_COVERAGE, { recursive: true });
await fs.writeFile(
`coverage/tmp/coverage-${randomUUID()}.json`,
path.join(process.env.NODE_V8_COVERAGE, `coverage-${randomUUID()}.json`),
JSON.stringify(output),
);
});
Expand Down
43 changes: 43 additions & 0 deletions test/unit/TodoLogic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,46 @@ test('TodoLogic.moveTodoItem', () => {
},
]);
});

test('TodoLogic.checkTodoItem', () => {
let data = TodoLogic.initTodoData(new Date(0));

data = {
...data,
items: [
{
id: 'a',
listId: '1970-01-01',
label: 'foo',
index: 0,
done: false,
},
{
id: 'b',
listId: '1970-01-01',
label: 'bar',
index: 1,
done: false,
},
],
};

data = TodoLogic.checkTodoItem(data, { id: 'a', done: true });

expect(data.items).toEqual([
{
id: 'a',
listId: '1970-01-01',
label: 'foo',
index: 0,
done: true,
},
{
id: 'b',
listId: '1970-01-01',
label: 'bar',
index: 1,
done: false,
},
]);
});

0 comments on commit dd29d07

Please sign in to comment.