Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
0t4u committed Dec 9, 2024
1 parent b12ca63 commit f6ef965
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 22 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"root": true,
"parserOptions": {
"sourceType": "module"
"sourceType": "module",
"ecmaVersion": "latest"
},
"env": {
"browser": true,
Expand Down
14 changes: 9 additions & 5 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
// @ts-check
/* eslint-disable no-undef */
import { createDefaultEsmPreset } from 'ts-jest';

/** @type {import('ts-jest').JestConfigWithTsJest} **/
module.exports = {
export default {
...createDefaultEsmPreset({}),
testEnvironment: "node",
transform: {
"^.+.tsx?$": ["ts-jest",{}],
},
moduleFileExtensions: [
"ts",
"tsx",
"mts",
"mjs",
"js"
],
moduleNameMapper: {
"^(\\.\\.?\\/.+)\\.jsx?$": "$1"
},
setupFilesAfterEnv: [
"./test/hooks.ts"
],
Expand Down
3 changes: 2 additions & 1 deletion src/etc/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import fs from 'fs-extra';
import semver from 'semver';
import type { version, semanticAlias, metadata } from '../types.js';
import { Application } from 'typedoc';
import { fileURLToPath } from 'url';
const packagePath = path.join(process.cwd(), 'package.json');
const pack = fs.readJSONSync(packagePath);

Expand Down Expand Up @@ -428,7 +429,7 @@ export function handleJeckyll(rootPath: string, targetPath: string): void {
* Copies static assets to the document build folder
* @param targetPath
*/
export function handleAssets(targetPath: string, srcDir: string = import.meta.dirname) {
export function handleAssets(targetPath: string, srcDir: string = path.dirname(fileURLToPath(import.meta.url))) {
const sourceAsset = path.join(srcDir, '../assets/versionsMenu.js');
fs.ensureDirSync(path.join(targetPath, 'assets'));
fs.copyFileSync(
Expand Down
2 changes: 1 addition & 1 deletion test/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ process.env.Node = 'test';

import fs from 'fs-extra';
import path from 'path';
import { docsPath, stubVersions } from './stubs/stubs';
import { docsPath, stubVersions } from './stubs/stubs.js';

jest.spyOn(console, 'error').mockClear();
jest.spyOn(console, 'warn').mockClear();
Expand Down
25 changes: 15 additions & 10 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
import expect from 'expect';
import { expect } from 'expect';
import path from 'path';
import fs from 'fs-extra';
import { describe, it } from '@jest/globals';
import * as vUtils from '../src/etc/utils';
import { minorVerRegex, verRegex } from '../src/etc/utils';
import * as vUtils from '../src/etc/utils.js';
import { minorVerRegex, verRegex } from '../src/etc/utils.js';
import {
docsPath,
jsKeys,
optionsPath,
stubOptionKeys,
stubPathKeys,
stubRootPath,
stubSemanticLinks,
stubTargetPath,
stubVersions,
} from './stubs/stubs';
} from './stubs/stubs.js';
import { Application } from 'typedoc';
import { load } from '../src/index';
import { load } from '../src/index.js';

const typedocOptions: Parameters<typeof Application.bootstrap>[0] = {
options: optionsPath,
}

describe('Unit testing for typedoc-plugin-versions', () => {
it('loads and parses options', async () => {
const app = await Application.bootstrap();
const app = await Application.bootstrap(typedocOptions);
const options = load(app);

for (const key of stubOptionKeys) {
Expand Down Expand Up @@ -141,7 +146,7 @@ describe('Unit testing for typedoc-plugin-versions', () => {
});
describe('handle file operations correctly', () => {
it('maps correct output paths', async () => {
const app = await Application.bootstrap();
const app = await Application.bootstrap(typedocOptions);
const version = 'v0.0.0';
app.options.setValue('out', docsPath);
const dirs = vUtils.getPaths(app, version);
Expand All @@ -152,7 +157,7 @@ describe('Unit testing for typedoc-plugin-versions', () => {
expect(dirs.targetPath.endsWith(stubTargetPath(version))).toBe(true);
});
it('does not error if .nojekyll is not present', async () => {
const app = await Application.bootstrap();
const app = await Application.bootstrap(typedocOptions);
const version = 'v0.0.0';
app.options.setValue('out', docsPath);
const dirs = vUtils.getPaths(app, version);
Expand All @@ -161,7 +166,7 @@ describe('Unit testing for typedoc-plugin-versions', () => {
}).not.toThrow();
});
it('copies .nojekyll to the document root if exists', async () => {
const app = await Application.bootstrap();
const app = await Application.bootstrap(typedocOptions);
const version = 'v0.0.0';
app.options.setValue('out', docsPath);
const dirs = vUtils.getPaths(app, version);
Expand All @@ -170,7 +175,7 @@ describe('Unit testing for typedoc-plugin-versions', () => {
expect(fs.existsSync(path.join(dirs.rootPath, '.nojekyll'))).toBe(true);
});
it('copies static assets into the target version folder', async () => {
const app = await Application.bootstrap();
const app = await Application.bootstrap(typedocOptions);
const version = 'v0.0.0';
app.options.setValue('out', docsPath);
const dirs = vUtils.getPaths(app, version);
Expand Down
9 changes: 6 additions & 3 deletions test/stubs/stubs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import path from 'path';
import { getSemanticVersion } from '../../src/etc/utils';
import { fileURLToPath } from 'url';
import { getSemanticVersion } from '../../src/etc/utils.js';

export const stubsPath = __dirname;
export const stubsPath = path.dirname(fileURLToPath(import.meta.url));
export const optionsPath = path.join(stubsPath, 'typedoc.json');
export const docsPath = path.join(stubsPath, 'docs');
export const stubVersions = ['v0.0.0', 'v0.1.0', 'v0.1.1', 'v0.2.3', 'v0.10.1'];
export const stubSemanticLinks = ['v0.0', 'v0.1', 'v0.2', 'v0.10'];
Expand All @@ -15,13 +17,14 @@ export const stubOptionKeys = [
export const stubPathKeys = ['rootPath', 'targetPath'];
export const stubRootPath =
process.platform === 'win32' ? '\\test\\stubs\\docs' : '/test/stubs/docs';
export const stubTargetPath = (version) =>
export const stubTargetPath = (version: string) =>
path.join(stubRootPath, getSemanticVersion(version));

export const jsKeys = `"use strict"
export const DOC_VERSIONS = [
'dev',
'v0.10',
'v0.3',
'v0.2',
'v0.1',
'v0.0',
Expand Down
3 changes: 3 additions & 0 deletions test/stubs/typedoc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"$schema": "https://typedoc.org/schema.json"
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"target": "ES2020",
"esModuleInterop": true,
"strict": true,
"isolatedModules": true,
// "isolatedModules": true,
"declaration": true,
"declarationMap": true,
"sourceMap": true,
Expand Down

0 comments on commit f6ef965

Please sign in to comment.