Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(__tests__ dir): added vitest and coverage reporting GitHub Actions workflow #8

Merged
merged 8 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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: 39 additions & 0 deletions .github/workflows/node-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# ./.github/workflows/node-ci.yml
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's rename this file to test-coverage-pr.yml

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#
# Auto CI/CD for Node.js projects using GitHub Actions.
name: Node CI
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
name: Node CI
name: Test Coverage Pull Request

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


# Enable the workflow for auto push, pull_request
# and manual workflow_dispatch events.
on:
push:
pull_request:
workflow_dispatch:

jobs:
yarn-vitest:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
yarn-vitest:
coverage:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name: Yarn Vitest
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
name: Yarn Vitest
name: Test Coverage

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


permissions:
contents: read
pull-requests: write

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Set Node.js 18.x
uses: actions/setup-node@v4
with:
node-version: 18.x

- name: Install dependencies
run: yarn install

- name: Run tests
run: yarn test

- name: Run coverage
if: always()
uses: davelosert/vitest-coverage-report-action@v2
45 changes: 45 additions & 0 deletions __tests__/Fuzzy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// ./__tests__/Fuzzy.test.ts
//
// Unittests for the Fuzzy class.

// Local Fuzzy class import.
import Fuzzy from '../src/Fuzzy';

// Fuzzy class test suite.
describe('Fuzzy', () => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
describe('Fuzzy', () => {
describe('Test Fuzzy', () => {

Let's add a Test in each describe block just to make it more consistent

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const list = ['apple', 'banana', 'grape', 'orange', 'pineapple'];
const fuzzy = new Fuzzy(list);

it('should return correct results for a query', () => {
const query = 'apple';
const results = fuzzy.search(query);
expect(results).toMatchSnapshot();
});

// Branch coverage: cache hit.
it('should NOT update cache for repeated same query', () => {
const query = 'apple';
const results = fuzzy.search(query);
expect(results).toMatchSnapshot();
});

it('should return empty array for no matches', () => {
const query = 'xyz';
const results = fuzzy.search(query);
expect(results).toMatchSnapshot();
});

// Branch coverage: includeMatches option is true.
it('should include matches if includeMatches option is true', () => {
const fuzzyWithMatches = new Fuzzy(list, { includeMatches: true });
const query = 'apple';
const results = fuzzyWithMatches.search(query);
expect(results).toMatchSnapshot();
});

// Edge case: list is undefined.
it('should set this.list to an empty array if list is undefined', () => {
const badFuzzy = new Fuzzy(undefined as any); // Simulate absence of list
expect(badFuzzy).toMatchSnapshot();
});
});
137 changes: 137 additions & 0 deletions __tests__/__snapshots__/Fuzzy.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`Fuzzy > should NOT update cache for repeated same query 1`] = `
[
{
"distance": 0,
"text": "apple",
},
{
"distance": 4,
"text": "pineapple",
},
{
"distance": 5,
"text": "orange",
},
{
"distance": 5,
"text": "banana",
},
]
`;

exports[`Fuzzy > should include matches if includeMatches option is true 1`] = `
[
{
"distance": 0,
"matches": [
[
0,
0,
],
[
1,
1,
],
[
2,
2,
],
[
3,
3,
],
[
4,
4,
],
],
"text": "apple",
},
{
"distance": 4,
"matches": [
[
0,
4,
],
[
1,
5,
],
[
2,
6,
],
[
3,
7,
],
[
4,
8,
],
],
"text": "pineapple",
},
{
"distance": 5,
"matches": [
[
0,
2,
],
[
4,
5,
],
],
"text": "orange",
},
{
"distance": 5,
"matches": [
[
0,
1,
],
],
"text": "banana",
},
]
`;

exports[`Fuzzy > should return correct results for a query 1`] = `
[
{
"distance": 0,
"text": "apple",
},
{
"distance": 4,
"text": "pineapple",
},
{
"distance": 5,
"text": "orange",
},
{
"distance": 5,
"text": "banana",
},
]
`;

exports[`Fuzzy > should return empty array for no matches 1`] = `[]`;

exports[`Fuzzy > should set this.list to an empty array if list is undefined 1`] = `
Fuzzy {
"cache": {},
"list": [],
"options": {
"includeMatches": false,
},
"search": [Function],
}
`;
116 changes: 116 additions & 0 deletions __tests__/__snapshots__/utils.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`getMatchingIndices > should return the correct matching indices 1`] = `
[
[
3,
3,
],
]
`;

exports[`levenshteinFullMatrixSearch > should return a matrix of the correct dimensions 1`] = `
[
[
0,
1,
2,
3,
4,
5,
],
[
1,
1,
2,
3,
4,
5,
],
[
2,
2,
2,
3,
4,
5,
],
[
3,
3,
3,
3,
3,
4,
],
[
4,
4,
4,
4,
3,
4,
],
[
5,
5,
4,
5,
4,
4,
],
]
`;

exports[`levenshteinFullMatrixSearch > should return a matrix with the correct values 1`] = `
[
[
0,
1,
2,
3,
4,
5,
],
[
1,
1,
2,
3,
4,
5,
],
[
2,
2,
2,
3,
4,
5,
],
[
3,
3,
3,
3,
3,
4,
],
[
4,
4,
4,
4,
3,
4,
],
[
5,
5,
4,
5,
4,
4,
],
]
`;
12 changes: 12 additions & 0 deletions __tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// ./__tests__/index.test.ts
//
// Integration test for the package entrypoint.

import Fuzzy from "../src/Fuzzy";
import * as IndexExport from "../src/index";

describe("Package entrypoint", () => {
it("should correctly export the Fuzzy class", () => {
expect(IndexExport.default).toBe(Fuzzy);
});
});
Loading
Loading