timezones-ical-library
Version:
Easy direct access to the most recent official timezone information for iCalendar files via JavaScript/TypeScript
79 lines (69 loc) ⢠2.66 kB
JavaScript
const { execSync, spawnSync } = require('child_process');
const testCases = [
'CST6CDT',
'GMT0',
'Europe/Berlin',
'America/New_York',
'America/Argentina/Buenos_Aires',
'Antarctica/Casey',
'Africa/Bangui',
];
const apiDir = './demo/public/api'; // relative to root
try {
execSync('npm run build:lib-only', { stdio: [0, 1, 2] });
const testCasesString = JSON.stringify(testCases);
console.log('\nšļø Running Tests...');
// Test ES Module import
try {
const result = spawnSync('node', ['test/load-module.mjs', testCasesString], { encoding: 'utf8' });
if (result.stdout) process.stdout.write(result.stdout);
if (result.status !== 0 || (result.stderr && result.stderr.length > 0)) {
throw new Error('Test script passed with errors');
}
console.log('ā
Importing the script as module and running tests\n');
} catch (error) {
console.error('ā Something went wrong with testing the ES Module setup\n');
throw error;
}
// Test CommonJS require
try {
const result = spawnSync('node', ['test/load-commonjs.cjs', testCasesString], { encoding: 'utf8' });
if (result.stdout) process.stdout.write(result.stdout);
if (result.status !== 0 || (result.stderr && result.stderr.length > 0)) {
throw new Error('Test script passed with errors');
}
console.log('ā
CommonJS init via require and running tests\n');
} catch (error) {
console.error('ā Something went wrong with testing the CommonJS setup\n');
throw error;
}
// Test whether there is an ics file per test case in the api folder
console.log('\nāļø Testing for ics files in the API folder:\n');
try {
testCases.forEach((tz) => {
const fs = require('fs');
const path = `${apiDir}/${tz}.ics`;
// eslint-disable-next-line security/detect-non-literal-fs-filename
if (!fs.existsSync(path)) {
throw new Error(`š“ Missing file for time zone ${tz} at API folder`);
}
console.log(`š¢ Found ics file for time zone ${tz} at API folder`);
});
console.log('ā
All time zones have a corresponding ics file in the API folder\n');
} catch (error) {
console.error(error.message);
console.error('ā Could not find all ics files in the API folder\n');
throw error;
}
const fs = require('fs');
const dirToDrop = 'dist';
fs.rm(dirToDrop, { recursive: true }, (error) => {
if (error) {
throw error;
}
console.log(`... ${dirToDrop} directory deleted again`);
console.log('\nš All Tests SUCCESSFUL!\n');
});
} catch (_error) {
console.error('\nš FAILED: Tests did not pass unfortunately.\n');
}