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

Codecov #37

Merged
merged 5 commits into from
Dec 30, 2023
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
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ jobs:
- name: Run tests
run: yarn test:coverage

- name: Upload code coverage
run: bash <(curl -s https://codecov.io/bash)
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
4 changes: 2 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ jobs:
- name: Run tests
run: yarn test:coverage

- name: Upload code coverage
run: bash <(curl -s https://codecov.io/bash)
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3

- name: Publish NPM
if: startsWith(github.ref, 'refs/tags/v') && contains(github.ref, 'beta') == false
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ jobs:
- name: Build app
run: yarn build

- name: Run tests
run: yarn test:coverage
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"@types/node": "^20.10.6",
"@typescript-eslint/eslint-plugin": "^6.16.0",
"@typescript-eslint/parser": "^6.16.0",
"@vitest/coverage-v8": "^1.1.0",
"@vitest/coverage-istanbul": "^1.1.0",
"eslint": "^8.56.0",
"eslint-config-google": "^0.14.0",
"eslint-config-prettier": "^9.1.0",
Expand Down
7 changes: 7 additions & 0 deletions src/fetchers/fetch.polyfill.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Check if `fetch` is available in global scope (nodejs 18+) or in window (browser). If not, use cross-fetch polyfill.
import { fetch as crossFetch } from 'cross-fetch';
export const fetchSafe =
(typeof fetch === 'function' && fetch) || // ServiceWorker fetch (Cloud Functions + Chrome extension)
(typeof global === 'object' && global.fetch) || // Node.js 18+ fetch
(typeof window !== 'undefined' && window.fetch) || // Browser fetch
crossFetch; // Polyfill fetch
8 changes: 1 addition & 7 deletions src/fetchers/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
// Check if `fetch` is available in global scope (nodejs 18+) or in window (browser). If not, use cross-fetch polyfill.
import { fetch as crossFetch } from 'cross-fetch';
const fetchSafe =
(typeof fetch === 'function' && fetch) || // ServiceWorker fetch (Cloud Functions + Chrome extension)
(typeof global === 'object' && global.fetch) || // Node.js 18+ fetch
(typeof window !== 'undefined' && window.fetch) || // Browser fetch
crossFetch; // Polyfill fetch
import { fetchSafe } from './fetch.polyfill';

const USER_AGENTS = [
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36',
Expand Down
18 changes: 11 additions & 7 deletions src/helpers/global.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,8 @@ export const addProtocol = (url: string): string => {
return url.startsWith('//') ? 'https:' + url : url;
};

export const parseISO8601Duration = (iso: string): number => {
const iso8601DurationRegex =
/(-)?P(?:([.,\d]+)Y)?(?:([.,\d]+)M)?(?:([.,\d]+)W)?(?:([.,\d]+)D)?T(?:([.,\d]+)H)?(?:([.,\d]+)M)?(?:([.,\d]+)S)?/;

const matches = iso.match(iso8601DurationRegex);

const duration = {
export const getDuration = (matches: any[]) => {
return {
sign: matches[1] === undefined ? '+' : '-',
years: matches[2] === undefined ? 0 : matches[2],
months: matches[3] === undefined ? 0 : matches[3],
Expand All @@ -61,6 +56,15 @@ export const parseISO8601Duration = (iso: string): number => {
minutes: matches[7] === undefined ? 0 : matches[7],
seconds: matches[8] === undefined ? 0 : matches[8]
};
};

export const parseISO8601Duration = (iso: string): number => {
const iso8601DurationRegex =
/(-)?P(?:([.,\d]+)Y)?(?:([.,\d]+)M)?(?:([.,\d]+)W)?(?:([.,\d]+)D)?T(?:([.,\d]+)H)?(?:([.,\d]+)M)?(?:([.,\d]+)S)?/;

const matches = iso.match(iso8601DurationRegex);

const duration = getDuration(matches);

return +duration.minutes;
};
35 changes: 35 additions & 0 deletions tests/global.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { describe, expect, test } from 'vitest';
import { getDuration } from '../src/helpers/global.helper';

export const durationInput = [
'PT142M',
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
'142',
undefined,
{ index: 0 },
{ input: 'PT142M' },
{ groups: undefined }
];

const result = {
sign: '+',
years: 0,
months: 0,
weeks: 0,
days: 0,
hours: 0,
minutes: '142',
seconds: 0
};

describe('Live: Fetch rating page', () => {
test('Resolve duration', async () => {
const resolver = getDuration(durationInput);
expect(resolver).toEqual(result);
});
});
6 changes: 5 additions & 1 deletion tests/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('Parse Id', () => {
expect(url).toBe(906693);
});
test('Handle bad url', () => {
const url = parseIdFromUrl(null);
const url = parseIdFromUrl(null as any);
expect(url).toBe(null);
});
test('bad string', () => {
Expand All @@ -44,6 +44,10 @@ describe('Parse color', () => {
const url = parseColor('blue');
expect(url).toBe('average');
});
test('Light grey', () => {
const url = parseColor('lightgrey');
expect(url).toBe('unknown');
});
test('Grey', () => {
const url = parseColor('grey');
expect(url).toBe('bad');
Expand Down
10 changes: 5 additions & 5 deletions tests/movie.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@ import { movieMock4 } from './mocks/movie4.html';
import { seriesMock } from './mocks/series1.html';

const getPageClasses = (node: HTMLElement): string[] => {
return node.querySelector('.page-content').classNames.split(' ');
return node.querySelector('.page-content')?.classNames.split(' ') ?? [''];
};

const getAsideNode = (node: HTMLElement): HTMLElement => {
return node.querySelector('.aside-movie-profile');
return node.querySelector('.aside-movie-profile') as HTMLElement;
};

const getNode = (node: HTMLElement): HTMLElement => {
return node.querySelector('.main-movie-profile');
return node.querySelector('.main-movie-profile') as HTMLElement;
};

const getJsonLd = (node: HTMLElement): string => {
return node.querySelector('script[type="application/ld+json"]').innerText;
return node.querySelector('script[type="application/ld+json"]')?.innerText ?? '{}';
};

const getMovie = (
Expand Down Expand Up @@ -99,7 +99,7 @@ const {
// Movie 4
const movieHtml4 = parse(movieMock4);

const { aside: asideNode4, pNode: movieNode4, jsonLd: movie4JsonLd } = getMovie(movieHtml4);
const { aside: asideNode4 } = getMovie(movieHtml4);

// Series
const seriesHtml = parse(seriesMock);
Expand Down
10 changes: 10 additions & 0 deletions vitest.config.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { configDefaults, defineConfig } from 'vitest/config';

export default defineConfig({
test: {
coverage: {
provider: 'istanbul',
exclude: [...configDefaults.exclude, 'demo.ts', '**/*.polyfill.ts', 'vars.ts']
}
}
});
Loading