Skip to content
Draft
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
2 changes: 1 addition & 1 deletion .storybook/time-machine.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import timemachine from 'timemachine';
import timemachine from './timemachine';
/*
timemachine is a node module which overrides the system time for a repo. There are components on storybook with date and time which varies according to current date and time. This causes inconsistency across chromaticqa tests hence the use of this dependency.
*/
Expand Down
93 changes: 93 additions & 0 deletions .storybook/timemachine.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
const OriginalDate = Date;

const Timemachine = {
timestamp: 0,
tick: false,
tickStartDate: null,
keepTime: false,
difference: 0,

config({ dateString, timestamp, difference, keepTime, tick } = {}) {
this.timestamp = OriginalDate.parse(dateString) || timestamp || this.timestamp;

if (difference !== undefined) this.difference = difference;
if (keepTime !== undefined) this.keepTime = keepTime;

if (tick !== undefined) {
this.tick = tick;
if (this.tick) {
this.tickStartDate = new OriginalDate();
}
}

this._apply();
},

reset() {
this.timestamp = 0;
this.tick = false;
this.tickStartDate = null;
this.keepTime = false;
this.difference = 0;

globalThis.Date = OriginalDate;
},

_apply() {
const self = this;

// We use a standard function instead of a class here so it can be
// called without 'new', mimicking native Date behavior.
function MockDate(...args) {
let date;

if (self.keepTime) {
date = new OriginalDate();
} else if (args.length > 0) {
// ✨ The spread operator replaces the massive 7-level ES5 if/else block
date = new OriginalDate(...args);
} else {
date = new OriginalDate(self.timestamp);
}

if (args.length === 0) {
const difference = self._getDifference();
if (difference !== 0) {
date = new OriginalDate(date.getTime() + difference);
}
}

return date;
}

MockDate.prototype = OriginalDate.prototype;

MockDate.now = () => {
const timestamp = self.keepTime ? OriginalDate.now() : self.timestamp;
return timestamp + self._getDifference();
};

MockDate.OriginalDate = OriginalDate;
MockDate.UTC = OriginalDate.UTC;
MockDate.parse = OriginalDate.parse; // Added for completeness

// Safely override the global Date object in any environment
globalThis.Date = MockDate;
},

_getDifference() {
let difference = this.difference;

if (this.tick && this.tickStartDate) {
difference += OriginalDate.now() - this.tickStartDate.getTime();
}

return difference;
},
};

// Initialize
Timemachine._apply();

// Export as a standard ES Module
export default Timemachine;
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
11 changes: 1 addition & 10 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,7 @@ const puppeteerTests = {
module.exports = {
projects: [unitTests, clientUnitTests, puppeteerTests],
reporters: [
'default',
[
'jest-junit',
{
suiteName: 'Jest Tests',
outputDirectory: 'reports/jest',
uniqueOutputName: 'true',
ancestorSeparator: ' › ',
},
],
'default'
],
fakeTimers: {
enableGlobally: true,
Expand Down
10 changes: 1 addition & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"lighthouse": "./scripts/lighthouseRun.sh",
"prepare": "husky install",
"storybook": "storybook dev -p 9001 -c .storybook",
"test:local": "yarn test:lint && yarn test:dependencies && yarn test:unit && run-p --race start amp:validate",
"test:local": "yarn test:lint && yarn test:dependencies && yarn test:unit && amp:validate",
"test:dependencies": "node ./scripts/dependencyCheck",
"test:lint": "biome check --max-diagnostics 50000 --diagnostic-level error ./src ./ws-nextjs-app ./data ./cypress",
"test:lint:fix": "biome check --max-diagnostics 50000 --diagnostic-level error --write ./src ./ws-nextjs-app ./data ./cypress",
Expand Down Expand Up @@ -118,7 +118,6 @@
"@testing-library/user-event": "14.6.1",
"@types/jest": "30.0.0",
"@types/js-cookie": "^3.0.3",
"@types/jsdom": "27.0.0",
"@types/ramda": "0.31.1",
"@types/react": "19.2.14",
"@types/react-dom": "19.2.3",
Expand All @@ -129,9 +128,7 @@
"babel-jest": "30.3.0",
"babel-loader": "10.1.1",
"babel-plugin-dynamic-import-node": "2.3.3",
"chalk": "5.6.2",
"chromatic": "13.1.4",
"cli-table": "0.3.11",
"colors": "^1.4.0",
"crypto": "1.0.1",
"cypress": "15.11.0",
Expand All @@ -144,18 +141,13 @@
"jest": "30.3.0",
"jest-environment-jsdom": "30.2.0",
"jest-expect-message": "1.1.3",
"jest-junit": "16.0.0",
"jsdom": "27.0.0",
"lighthouse": "13.0.1",
"minimist": "1.2.8",
"mkdirp": "3.0.1",
"mocha-junit-reporter": "2.2.1",
"npm-run-all2": "8.0.4",
"puppeteer": "24.22.3",
"retry": "0.13.1",
"storybook": "10.3.6",
"supertest": "7.2.2",
"timemachine": "0.3.2",
"ts-jest": "29.4.9",
"typescript": "5.9.3",
"webpack": "5.106.2"
Expand Down
17 changes: 10 additions & 7 deletions scripts/ampHtmlValidator/checkManifest/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
// biome-ignore-all lint/suspicious/noConsole: we want this
import { JSDOM } from 'jsdom';

const getManifestFile = async url => {
const response = await fetch(url);

const html = await response.text();

const {
window: { document },
} = new JSDOM(html);
// 1. Isolate the <link> tag that has rel="manifest"
const linkTagMatch = html.match(/<link[^>]*rel=["']manifest["'][^>]*>/i);

if (linkTagMatch) {
// 2. Extract the href attribute from that specific tag
const hrefMatch = linkTagMatch[0].match(/href=["']([^"']+)["']/i);
return hrefMatch ? hrefMatch[1] : null;
}

return document.querySelector('link[rel="manifest"]').getAttribute('href');
return null;
};

export default async () => {
Expand Down Expand Up @@ -57,4 +60,4 @@ export default async () => {
});
process.exitCode = 1;
}
};
};
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
// biome-ignore-all lint/style/noCommonJs: we want this
// biome-ignore-all lint/style/useNodejsImportProtocol: we want this
const fs = require('fs');
const mkdirp = require('mkdirp');
const path = require('path');

const writeToNestedFile = (filePath, contents) => {
const fullFilePath = path.join(__dirname, filePath);
const folder = path.dirname(fullFilePath);

mkdirp.sync(folder);
if (!fs.existsSync(folder)) fs.mkdirSync(folder, { recursive: true });

fs.writeFileSync(fullFilePath, contents);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@
import fs from 'fs';
import path from 'path';

import mkdirp from 'mkdirp';

import writeToNestedFile from '.';

jest.mock('fs', () => ({
writeFileSync: jest.fn(),
}));

jest.mock('mkdirp', () => ({
sync: jest.fn(),
existsSync: jest.fn(),
mkdirSync: jest.fn(),
}));

jest.mock('../writeNewTimezoneData', () => jest.fn());
Expand All @@ -20,9 +16,6 @@ describe('writeToNestedFile', () => {
it('Should create directory and write file correctly', () => {
writeToNestedFile('path/to/a/place', 'filecontent');

expect(mkdirp.sync).toHaveBeenCalledTimes(1);
expect(mkdirp.sync).toHaveBeenCalledWith(path.join(__dirname, 'path/to/a'));

expect(fs.writeFileSync).toHaveBeenCalledTimes(1);
expect(fs.writeFileSync).toHaveBeenCalledWith(
path.join(__dirname, 'path/to/a/place'),
Expand Down
Binary file not shown.
1 change: 0 additions & 1 deletion ws-nextjs-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
"cypress-multi-reporters": "2.0.5",
"cypress-terminal-report": "7.2.1",
"jest": "30.3.0",
"jest-environment-jsdom": "30.2.0",
"mocha-junit-reporter": "2.2.1",
"next-test-api-route-handler": "5.0.5",
"npm-run-all2": "8.0.4",
Expand Down
Loading
Loading