Skip to content

Commit

Permalink
ci: add CI workflow for running tests and coverage on push to master …
Browse files Browse the repository at this point in the history
…or pull requests

ci: update Jest configuration to include coverage settings and test match patterns
docs: update package.json scripts with coverage command
style: improve formatting and readability in readme.md
test: enhance test coverage and add fuzz testing for processText function in index.spec.ts
  • Loading branch information
multipliedtwice committed May 28, 2024
1 parent 6f1fc5b commit 617da57
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 17 deletions.
63 changes: 63 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
name: CI
on:
push:
branches:
- master
pull_request:
branches:
- "*"
jobs:
Test_and_Coverage:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Use Node.js
uses: actions/setup-node@v2
with:
node-version: 20.x
registry-url: https://registry.npmjs.org

- uses: actions/cache@v2
with:
path: "**/node_modules"
key: ${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }}

- name: Install dependencies
run: yarn install --frozen-lockfile

- name: Run tests and collect coverage
run: yarn coverage

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4
with:
files: coverage/lcov.info
flags: unittests
verbose: true
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

Publish:
runs-on: ubuntu-latest
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
steps:
- uses: actions/checkout@v2
- name: Use Node.js
uses: actions/setup-node@v2
with:
node-version: 20.x
registry-url: https://registry.npmjs.org
- uses: actions/cache@v2
with:
path: "**/node_modules"
key: ${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }}
- name: Install dependencies
run: yarn install --frozen-lockfile
- name: Publish 🚀
working-directory: ./src
run: npx semantic-release
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
permissions:
contents: write
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
.pnp.*
node_modules
.npmrc
.coverage
coverage
7 changes: 6 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
collectCoverageFrom: ['src/*.spec.{js,ts}'],
// collectCoverageFrom: ['src/*.spec.{js,ts}'],
modulePathIgnorePatterns: ['<rootDir>/dist/'],
testEnvironment: 'node',
preset: 'ts-jest',
testMatch: ['<rootDir>/src/__tests__/*.spec.{js,ts}'],
collectCoverage: true,
coverageDirectory: 'coverage',
coverageReporters: ['lcov', 'text'],
coveragePathIgnorePatterns: ['<rootDir>/src/fuzz/fuzz.ts'],
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"main": "./dist/index.js",
"scripts": {
"test": "jest",
"build": "tsc && terser -c -m toplevel -o dist/index.js -- dist/index.js"
"build": "tsc && terser -c -m toplevel -o dist/index.js -- dist/index.js",
"coverage": "jest --coverage"
},
"repository": {
"type": "git",
Expand Down
7 changes: 7 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
[![npm version](https://badge.fury.io/js/backticks-codeblocks.svg)](https://badge.fury.io/js/backticks-codeblocks)
[![npm](https://img.shields.io/npm/dt/backticks-codeblocks.svg)](https://www.npmjs.com/package/backticks-codeblocks)
[![HitCount](https://hits.dwyl.com/multipliedtwice/backticks-codeblocks.svg?style=flat)](http://hits.dwyl.com/multipliedtwice/backticks-codeblocks)
[![Coverage](https://img.shields.io/codecov/c/github/multipliedtwice/backticks-codeblocks/main.svg)](https://codecov.io/gh/multipliedtwice/backticks-codeblocks)
[![npm](https://img.shields.io/npm/l/backticks-codeblocks.svg)](LICENSE)

# Backticks-Codeblocks, syntax highlighting parser

Backticks Codeblocks is a library for parsing text and generating structures that can be rendered as beautiful code blocks using PrismJS. It supports integration with modern JavaScript frameworks such as React, Angular, Vue, and Svelte.

## Features

- Generates PrismJS-compatible structures
- Supports multiple JavaScript frameworks (React, Angular, Vue, Svelte)
- Easy integration
Expand Down
45 changes: 31 additions & 14 deletions src/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,13 @@ describe('processText', () => {

it('handles interrupted code blocks', () => {
const input = 'Start ```incomplete code `block`';
const expectedSegments = ['Start ```incomplete code ', {
"code": "block",
"isBlock": false,
}];
const expectedSegments = [
'Start ```incomplete code ',
{
code: 'block',
isBlock: false,
},
];
expect(processText(input)).toEqual(expectedSegments);
});

Expand Down Expand Up @@ -191,7 +194,11 @@ describe('processText', () => {

it('handles backticks inside a word', () => {
const input = 'word`inside`word';
const expectedSegments = ['word', { code: 'inside', isBlock: false }, 'word'];
const expectedSegments = [
'word',
{ code: 'inside', isBlock: false },
'word',
];
expect(processText(input)).toEqual(expectedSegments);
});

Expand Down Expand Up @@ -282,7 +289,11 @@ describe('processText', () => {

it('treats single backticks within words as apostrophes', () => {
const input = "It's not a `code` block";
const expectedSegments = ["It's not a ", { code: 'code', isBlock: false }, " block"];
const expectedSegments = [
"It's not a ",
{ code: 'code', isBlock: false },
' block',
];
expect(processText(input)).toEqual(expectedSegments);
});

Expand Down Expand Up @@ -333,7 +344,7 @@ describe('processText', () => {

const blockRegex = /```((?:.|\r?\n)*?)```/gs;
const matches = [...input.matchAll(blockRegex)];
const codeBlocks = matches.map(match => match[1].trim());
const codeBlocks = matches.map((match) => match[1].trim());

// The expected result is the code block as a string without the backticks
const expectedResult = `
Expand Down Expand Up @@ -386,21 +397,27 @@ describe('processText', () => {
fuzzValues.forEach((fuzzValue, index) => {
it(`Test ${index + 1}: processText with fuzz input`, () => {
let result: (string | CodeSegment)[];

try {
const inputString = typeof fuzzValue === 'string' ? fuzzValue : JSON.stringify(fuzzValue);
const inputString =
typeof fuzzValue === 'string'
? fuzzValue
: JSON.stringify(fuzzValue);
result = processText(inputString);
} catch (error) {
result = []; // Handle errors or set default value
}

// General checks
expect(Array.isArray(result)).toBe(true);

// Check if each segment is either a string or a CodeSegment
result.forEach(segment => {
expect(typeof segment === 'string' || (typeof segment === 'object' && segment !== null)).toBe(true);

result.forEach((segment) => {
expect(
typeof segment === 'string' ||
(typeof segment === 'object' && segment !== null)
).toBe(true);

// If segment is a CodeSegment, check its structure
if (typeof segment === 'object') {
expect(segment).toHaveProperty('code');
Expand Down

0 comments on commit 617da57

Please sign in to comment.