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 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: 39 additions & 0 deletions .github/workflows/test-coverage-pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# ./.github/workflows/test-coverage-pr.yml
#
# Automated GitHub Actions workflow to run tests and coverage
name: Test Coverage Pull Request

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

jobs:
coverage:
name: Test Coverage

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
34 changes: 34 additions & 0 deletions __tests__/Fuzzy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import Fuzzy from "../src/Fuzzy";

describe("Test Fuzzy", () => {
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();
});

it("should return empty array for no matches", () => {
const query = "xyz";
const results = fuzzy.search(query);
expect(results).toMatchInlineSnapshot(`[]`);
});

// 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", () => {
// Simulate absence of list
const badFuzzy = new Fuzzy(undefined as any);
expect(badFuzzy).toMatchSnapshot();
});
``;
});
113 changes: 113 additions & 0 deletions __tests__/__snapshots__/Fuzzy.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`Test 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[`Test 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[`Test Fuzzy > should set this.list to an empty array if list is undefined 1`] = `
Fuzzy {
"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[`Test getMatchingIndices > should return the correct matching indices 1`] = `
[
[
3,
3,
],
]
`;

exports[`Test 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[`Test 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("Test package entrypoint", () => {
it("should correctly export the Fuzzy class", () => {
expect(IndexExport.default).toBe(Fuzzy);
});
});
79 changes: 79 additions & 0 deletions __tests__/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// ./__tests__/utils.test.ts
//
// Unittests for the L-Dist utility functions.

// Local utility functions import.
import {
calculateScore,
getMatchingIndices,
getMaxLevenshteinDistance,
levenshteinFullMatrixSearch,
} from '../src/utils';

// levenshteinFullMatrixSearch test suite.
describe('Test levenshteinFullMatrixSearch', () => {
it('should return a matrix of the correct dimensions', () => {
const query = 'hello';
const target = 'world';
const matrix = levenshteinFullMatrixSearch(query, target);
expect(matrix).toMatchSnapshot();
});

it('should return a matrix with the correct values', () => {
const query = 'hello';
const target = 'world';
const matrix = levenshteinFullMatrixSearch(query, target);
expect(matrix).toMatchSnapshot();
});
});

// getMaxLevenshteinDistance test suite.
describe('Test getMaxLevenshteinDistance', () => {
it('should return strictly 3 for short len 0~5 strings', () => {
expect(getMaxLevenshteinDistance('', '')).toBe(3); // Empty str edge case.
expect(getMaxLevenshteinDistance('a', '12')).toBe(3); // Routine case.
expect(getMaxLevenshteinDistance('12345', 'cd')).toBe(3); // Len 5 edge case.
});

it('should return strictly 10 for medium len 6~15 strings', () => {
expect(getMaxLevenshteinDistance('123456', 'hey')).toBe(10); // Len 6 edge case.
expect(getMaxLevenshteinDistance('hello world', 'world')).toBe(10); // Routine case.
expect(getMaxLevenshteinDistance('hello world', '123456789012345')).toBe(10); // Len 15 edge case.
});

it('should return strictly 15 for long len >=16 strings', () => {
expect(getMaxLevenshteinDistance('hello world, world hello', '')).toBe(15);
});
});

// getMatchingIndices test suite.
describe('Test getMatchingIndices', () => {
it('should return the correct matching indices', () => {
const query = 'hello';
const target = 'world';
const matrix = levenshteinFullMatrixSearch(query, target);
const matches = getMatchingIndices(matrix, query, target);
expect(matches).toMatchSnapshot();
});
});

// calculateScore test suite.
describe('Test calculateScore', () => {
it('should return the correct score when param dist <= max dist', () => {
const query = 'hello';
const target = 'world';
const matrix = levenshteinFullMatrixSearch(query, target);
const matches = getMatchingIndices(matrix, query, target);
const score = calculateScore(query, target, matches, 0);
expect(score).toBe(0.6);
});

it('should return strictly 0 when param dist > max dist', () => {
const query = 'hello';
const target = 'world';
const matrix = levenshteinFullMatrixSearch(query, target);
const matches = getMatchingIndices(matrix, query, target);
const score = calculateScore(query, target, matches, 9999);
expect(score).toBe(0);
});
});
Loading
Loading