Skip to content

Commit

Permalink
Merge pull request #72 from jaredwray/adding-in-vitest-for-testing-mo…
Browse files Browse the repository at this point in the history
…ving-forward

adding in vitest
  • Loading branch information
jaredwray authored Nov 1, 2024
2 parents 7e97acd + 69578f4 commit 1b0e040
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 10 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ jobs:
- name: Install Dependencies
run: npm install

- name: Testing
run: npm run test
- name: Testing Legacy Helpers
run: npm run test:legacy:ci

- name: Testing Fumanchu
run: npm run test:ci

- name: Code Coverage
uses: codecov/codecov-action@v3
Expand Down
5 changes: 4 additions & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,8 @@ jobs:
- name: Install Dependencies
run: npm install

- name: Testing
- name: Testing Legacy Helpers
run: npm run test:legacy:ci

- name: Testing Fumanchu
run: npm run test:ci
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3098,6 +3098,8 @@ Generate a random number
## How to Contribute
clone the repository locally and run 'npm i' in the root. Now that you've set up your workspace, you're ready to contribute changes to the `fumanchu` repository you can refer to the [CONTRIBUTING](CONTRIBUTING.md) guide. If you have any questions please feel free to ask by creating an issue and label it `question`.

To test the legacy helpers, you can run `npm run test:legacy` to run the tests. If you want to test the new helpers, you can run `npm run test`.

## License and Copyright
[MIT](LICENSE) and codebase after 2023 will be copyright of Jared Wray.

Expand Down
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"test": "npm run build && xo --fix && c8 mocha",
"test:ci": "npm run build && xo && c8 --reporter=lcov mocha",
"test": "xo --fix && vitest run --coverage",
"test:ci": "xo && vitest run --coverage",
"test:legacy": "npm run build && c8 mocha",
"test:legacy:ci": "npm run build && c8 --reporter=lcov mocha",
"build": "rimraf ./dist && tsup ./src/index.ts --format cjs --dts --clean",
"prepare": "npm run build",
"clean": "rimraf ./node_modules ./coverage yarn.lock package-lock.json ./dist ./dist-site",
Expand Down Expand Up @@ -55,7 +57,7 @@
"dependencies": {
"arr-flatten": "^1.1.0",
"array-sort": "^1.0.0",
"cacheable": "^1.8.2",
"cacheable": "^1.8.3",
"create-frame": "^1.0.0",
"define-property": "^2.0.2",
"ent": "^2.2.1",
Expand Down Expand Up @@ -87,6 +89,7 @@
"year": "^0.2.1"
},
"devDependencies": {
"@vitest/coverage-v8": "^2.1.4",
"c8": "^10.1.2",
"chai": "^4.3.10",
"docula": "^0.9.4",
Expand All @@ -99,6 +102,7 @@
"tsup": "^8.3.5",
"typecript": "^0.0.1-security",
"typescript": "^5.6.3",
"vitest": "^2.1.4",
"xo": "^0.59.3"
},
"xo": {
Expand Down
10 changes: 6 additions & 4 deletions src/helper-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ export class HelperRegistry {
this.registerHelpers(dateHelpers);
}

public register(helper: Helper) {
if (this.has(helper.name)) {
throw new Error(`Helper ${helper.name} already exists.`);
public register(helper: Helper): boolean {
const result = false;
if (!this.has(helper.name)) {
this._helpers.push(helper);
}

this._helpers.push(helper);
return result;
}

public registerHelpers(helpers: Helper[]) {
Expand All @@ -49,6 +50,7 @@ export class HelperRegistry {
}

public filter(filter: HelperFilter): Helper[] {
/* c8 ignore next 4 */
return this._helpers.filter(helper => (!filter.name || helper.name === filter.name)
&& (!filter.category || helper.category === filter.category)
&& (!filter.compatibility || helper.compatibility === filter.compatibility));
Expand Down
79 changes: 79 additions & 0 deletions test/helper-registry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import {describe, test, expect} from 'vitest';
import {HelperRegistry, HelperRegistryCompatibility} from '../src/helper-registry.js';

describe('HelperRegistry', () => {
test('should have helpers', () => {
const registry = new HelperRegistry();
expect(registry).toBeDefined();
});
});

describe('HelperRegistry Register', () => {
test('should register a helper', () => {
const registry = new HelperRegistry();
registry.register({
name: 'test',
category: 'test',
fn: () => 'test',
});
expect(registry.has('test')).toBeTruthy();
});

test('should not register a helper twice', () => {
const registry = new HelperRegistry();
const helper = {
name: 'test',
category: 'test',
fn: () => 'test',
};
registry.register(helper);
expect(registry.register(helper)).toBeFalsy();
});
});

describe('HelperRegistry Filter', () => {
test('should return nothing filter by name', () => {
const registry = new HelperRegistry();
registry.register({
name: 'test',
category: 'test',
fn: () => 'test',
});
expect(registry.filter({name: 'test1'}).length).toBe(1);
});
test('should filter by name', () => {
const registry = new HelperRegistry();
registry.register({
name: 'test',
category: 'test',
fn: () => 'test',
});
expect(registry.filter({name: 'test'}).length).toBe(1);
});
test('should filter by category', () => {
const registry = new HelperRegistry();
registry.register({
name: 'test',
category: 'test',
fn: () => 'test',
});
registry.register({
name: 'test2',
category: 'test2',
fn: () => 'test2',
});
expect(registry.filter({category: 'test'}).length).toBe(1);
});

test('should filter by compatibility', () => {
const registry = new HelperRegistry();
registry.register({
name: 'test',
category: 'test',
compatibility: HelperRegistryCompatibility.BROWSER,
fn: () => 'test',
});
expect(registry.filter({compatibility: HelperRegistryCompatibility.BROWSER}).length).toBe(1);
});
});

24 changes: 24 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {describe, test, expect} from 'vitest';
import {
helpers, handlebars, createHandlebars, fumanchu,
} from '../src/index.js';

describe('fumanchu', () => {
test('should have helpers', () => {
expect(helpers).toBeDefined();
});

test('should have handlebars', () => {
expect(handlebars).toBeDefined();
});

test('should be able to createHandlebars', async () => {
const handlebars = await createHandlebars();
expect(handlebars).toBeDefined();
});

test('should be able to run fumanchu()', () => {
const handlebars = fumanchu();
expect(handlebars).toBeDefined();
});
});
18 changes: 18 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {defineConfig} from 'vitest/config';

export default defineConfig({
test: {
coverage: {
reporter: ['lcov', 'text'],
exclude: [
'**/node_modules/**',
'helpers/**',
'vitest.config.ts',
'site/**',
'dist/**',
'dist-site/**',
'test/**',
],
},
},
});

0 comments on commit 1b0e040

Please sign in to comment.