Skip to content

Commit

Permalink
Merge pull request #1145 from IGx89/task/test-newer-node-versions
Browse files Browse the repository at this point in the history
#1144@patch: Run tests against newer Node.js versions
  • Loading branch information
capricorn86 authored Oct 27, 2023
2 parents c404462 + 61d4274 commit 9a0062b
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 27 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ on: pull_request
jobs:
build:
runs-on: ubuntu-latest
continue-on-error: true
strategy:
matrix:
node-version: [16]
node-version: [16, 18, 20]

steps:
- uses: actions/checkout@v3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,26 @@ export default class HTMLInputElementDateUtility {
/**
* Returns iso week number from given date
*
* @see https://stackoverflow.com/a/6117889
* @param date Date or number.
* @returns Iso-week string.
*/
public static dateIsoWeek(date: Date | number): string {
date = new Date(date);
const day = (date.getUTCDay() + 6) % 7;
date.setUTCDate(date.getUTCDate() - day + 3);
const firstThursday = date.getTime();
date.setUTCMonth(0, 1);
if (date.getDay() !== 4) {
date.setUTCMonth(0, 1 + ((4 - date.getDay() + 7) % 7));
}
return (
date.getUTCFullYear() +
'-W' +
String(1 + Math.ceil((firstThursday - date.getTime()) / 604800000)).padStart(2, '0')
date = typeof date === 'number' ? new Date(date) : date;
// Copy date so don't modify original
date = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()));
// Set to nearest Thursday: current date + 4 - current day number
// Make Sunday's day number 7
date.setUTCDate(date.getUTCDate() + 4 - (date.getUTCDay() || 7));
// Get first day of year
const yearStart = new Date(Date.UTC(date.getUTCFullYear(), 0, 1));
// Calculate full weeks to nearest Thursday
const weekNo = Math.ceil(
((<number>(<unknown>date) - <number>(<unknown>yearStart)) / 86400000 + 1) / 7
);
return `${date.getUTCFullYear()}-W${weekNo < 10 ? '0' : ''}${weekNo}`;
}

/**
* Returns a date object for monday of given iso week string (\d\d\d\d-W\d\d)
*
Expand All @@ -37,7 +39,7 @@ export default class HTMLInputElementDateUtility {
}
const date = new Date(`${Y}-01-01T00:00Z`);
const jan4th = new Date(`${Y}-01-04T00:00Z`);
const jan4thDay = (jan4th.getDay() + 6) % 7;
const jan4thDay = (jan4th.getUTCDay() + 6) % 7;
const ordinalDate = 1 + (Number(W) - 1) * 7 - jan4thDay + 3;
date.setUTCDate(ordinalDate);
if (date.getUTCFullYear() > Number(Y)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ describe('HTMLInputElement', () => {
{ type: 'time', value: '00:00', want: new Date('1970-01-01T00:00Z') },
{ type: 'time', value: '12:00', want: new Date('1970-01-01T12:00Z') },
{ type: 'time', value: '18:55', want: new Date('1970-01-01T18:55Z') },
{ type: 'week', value: '1981-W01', want: new Date('1980-12-29T00:00Z') },
{ type: 'week', value: '2023-W22', want: new Date('2023-05-29T00:00Z') }
])(`Should return valid date for type $type with valid value`, ({ type, value, want }) => {
element.type = type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { describe, it, expect } from 'vitest';
import HTMLInputElementDateUtility from '../../../src/nodes/html-input-element/HTMLInputElementDateUtility.js';

describe('HTMLInputElementDateUtility', () => {
describe('dateToIsoWeek()', () => {
describe('dateIsoWeek()', () => {
it('Returns the ISO week number', () => {
expect(HTMLInputElementDateUtility.dateIsoWeek(new Date('2021-01-01'))).toBe('2020-W53');
expect(HTMLInputElementDateUtility.dateIsoWeek(new Date('2021-01-03'))).toBe('2020-W53');
Expand Down Expand Up @@ -35,7 +35,7 @@ describe('HTMLInputElementDateUtility', () => {
});
});

describe('IsoWeekToDate()', () => {
describe('isoWeekDate()', () => {
it('Returns the ISO week number', () => {
expect(HTMLInputElementDateUtility.isoWeekDate('2020-W53')).toEqual(
new Date('2020-12-28T00:00Z')
Expand Down
4 changes: 2 additions & 2 deletions packages/happy-dom/test/window/Window.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ describe('Window', () => {
expect((<ErrorEvent>(<unknown>errorEvent)).error?.message).toBe('Test error');
expect((<ErrorEvent>(<unknown>errorEvent)).message).toBe('Test error');
resolve(null);
}, 15);
}, 20);
});
});
});
Expand Down Expand Up @@ -1030,7 +1030,7 @@ describe('Window', () => {
expect((<ErrorEvent>(<unknown>errorEvent)).error?.message).toBe('Test error');
expect((<ErrorEvent>(<unknown>errorEvent)).message).toBe('Test error');
resolve(null);
}, 10);
}, 20);
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,25 +61,24 @@ async function itObservesUnhandledJavaScriptFetchRejections(): Promise<void> {
<script src="https://localhost:3000/404.js" async></script>
`);

await new Promise((resolve) => setTimeout(resolve, 10));
for (let i = 0; i < 10; i++) {
await new Promise((resolve) => setTimeout(resolve, 10));
if (errorEvent) {
break;
}
}

observer.disconnect();

if (!(errorEvent instanceof window.ErrorEvent)) {
throw new Error('Error event not dispatched.');
}

if (
errorEvent.error.message !==
'Fetch to "https://localhost:3000/404.js" failed. Error: connect ECONNREFUSED 127.0.0.1:3000'
) {
if (!errorEvent.error.message.startsWith('Fetch to "https://localhost:3000/404.js" failed.')) {
throw new Error('Error message not correct.');
}

if (
errorEvent.message !==
'Fetch to "https://localhost:3000/404.js" failed. Error: connect ECONNREFUSED 127.0.0.1:3000'
) {
if (!errorEvent.message.startsWith('Fetch to "https://localhost:3000/404.js" failed.')) {
throw new Error('Error message not correct.');
}
}
Expand Down

0 comments on commit 9a0062b

Please sign in to comment.