Skip to content

Commit

Permalink
chore: test esm
Browse files Browse the repository at this point in the history
  • Loading branch information
tatomyr committed Aug 13, 2024
1 parent d0fc69c commit e9f48ec
Show file tree
Hide file tree
Showing 286 changed files with 1,453 additions and 1,210 deletions.
12 changes: 6 additions & 6 deletions __tests__/commands.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { readdirSync, statSync, existsSync } from 'fs';
import { readdirSync, statSync, existsSync } from 'node:fs';
import { join, relative } from 'path';
//@ts-ignore
import { toMatchSpecificSnapshot } from './specific-snapshot';
// @ts-ignore
import { toMatchSpecificSnapshot } from './specific-snapshot.cjs';
import {
getCommandOutput,
getEntrypoints,
callSerializer,
getParams,
cleanupOutput,
} from './helpers';
import * as fs from 'fs';
import { spawnSync } from 'child_process';
} from './helpers.js';
import * as fs from 'node:fs';
import { spawnSync } from 'node:child_process';

expect.extend({
toMatchExtendedSpecificSnapshot(received, snapshotFile) {
Expand Down
6 changes: 3 additions & 3 deletions __tests__/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { readFileSync } from 'fs';
import { join } from 'path';
import { parseYaml } from '../packages/core/src/utils'; // not able to import from @redocly/openapi-core
//@ts-ignore
import { addSerializer } from './specific-snapshot';
import { parseYaml } from '../packages/core/src/utils.js'; // not able to import from @redocly/openapi-core
// @ts-ignore
import { addSerializer } from './specific-snapshot.cjs';
import { spawnSync } from 'child_process';

type CLICommands =
Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions __tests__/webpack-bundle.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { readFileSync } from 'fs';
import { join } from 'path';
import { getCommandOutput, getEntrypoints, callSerializer, getParams } from './helpers';
//@ts-ignore
import { toMatchSpecificSnapshot, addSerializer } from './specific-snapshot';
import { getCommandOutput, getEntrypoints, callSerializer, getParams } from './helpers.js';
// @ts-ignore
import { toMatchSpecificSnapshot, addSerializer } from './specific-snapshot.cjs';

expect.extend({
toMatchExtendedSpecificSnapshot(received, snapshotFile) {
Expand Down
20 changes: 13 additions & 7 deletions jest.config.js → jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
module.exports = {
import type { JestConfigWithTsJest } from 'ts-jest';

const jestConfig: JestConfigWithTsJest = {
clearMocks: true,
restoreMocks: true,
preset: 'ts-jest',
preset: 'ts-jest/presets/default-esm',
extensionsToTreatAsEsm: ['.ts'],
testEnvironment: 'node',
collectCoverageFrom: [
'packages/*/src/**/*.ts',
Expand All @@ -23,11 +26,14 @@ module.exports = {
functions: 60,
lines: 60,
},
},
testMatch: ['**/__tests__/**/*.test.ts', '**/*.test.ts'],
globals: {
'ts-jest': {
diagnostics: false,
global: {
statements: 70,
branches: 70,
functions: 70,
lines: 70,
},
},
testMatch: ['**/__tests__/**/*.test.ts', '**/*.test.ts'],
};

export default jestConfig;
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"name": "@redocly/cli",
"version": "1.0.0",
"type": "module",
"description": "",
"private": true,
"engines": {
"node": ">=15.0.0",
"npm": ">=7.0.0"
"node": ">=18",
"npm": ">=10"
},
"engineStrict": true,
"scripts": {
Expand All @@ -17,6 +18,7 @@
"coverage:core": "npm run jest -- --roots packages/core/src --coverage",
"typecheck": "tsc --noEmit --skipLibCheck",
"e2e": "npm run webpack-bundle -- --mode=none && REDOCLY_TELEMETRY=off jest --roots=./__tests__/",
"e2e2": "node --experimental-vm-modules node_modules/jest/bin/jest.js --roots=./__tests__/",
"prettier": " npx prettier --write \"**/*.{ts,js,yaml,yml,json,md}\"",
"prettier:check": "npx prettier --check \"**/*.{ts,js,yaml,yml,json,md}\"",
"eslint": "eslint packages/**",
Expand All @@ -25,6 +27,7 @@
"compile": "tsc -b tsconfig.build.json",
"prepare": "npm run compile",
"cli": "ts-node packages/cli/src/index.ts",
"cli2": "ts-node packages/cli/lib/index.js",
"lint": "npm run cli lint resources/pets.yaml -- --format stylish",
"bundle": "npm run cli bundle resources/pets.yaml",
"stats": "npm run cli stats resources/pets.yaml",
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/bin/cli.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env node

require('../lib/index');
import '../lib/index.js';
9 changes: 5 additions & 4 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
{
"name": "@redocly/cli",
"version": "1.19.0",
"type": "module",
"description": "",
"license": "MIT",
"bin": {
"openapi": "bin/cli.js",
"redocly": "bin/cli.js"
"openapi": "./bin/cli.js",
"redocly": "./bin/cli.js"
},
"engines": {
"node": ">=14.19.0",
"npm": ">=7.0.0"
"node": ">=18",
"npm": ">=10"
},
"engineStrict": true,
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/__mocks__/@redocly/openapi-core.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ConfigFixture } from './../../__tests__/fixtures/config';
import { firstDocument, secondDocument, thirdDocument } from '../documents';
import { ConfigFixture } from './../../__tests__/fixtures/config.js';
import { firstDocument, secondDocument, thirdDocument } from '../documents.js';

import type { Document } from '@redocly/openapi-core';

Expand Down
8 changes: 4 additions & 4 deletions packages/cli/src/__tests__/commands/build-docs.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { createStore, loadAndBundleSpec } from 'redoc';
import { renderToString } from 'react-dom/server';
import { handlerBuildCommand } from '../../commands/build-docs';
import { BuildDocsArgv } from '../../commands/build-docs/types';
import { getPageHTML } from '../../commands/build-docs/utils';
import { getFallbackApisOrExit } from '../../utils/miscellaneous';
import { handlerBuildCommand } from '../../commands/build-docs/index.js';
import { BuildDocsArgv } from '../../commands/build-docs/types.js';
import { getPageHTML } from '../../commands/build-docs/utils.js';
import { getFallbackApisOrExit } from '../../utils/miscellaneous.js';

jest.mock('redoc');
jest.mock('fs');
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/__tests__/commands/bundle.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { bundle, getTotals, getMergedConfig } from '@redocly/openapi-core';

import { BundleOptions, handleBundle } from '../../commands/bundle';
import { handleError } from '../../utils/miscellaneous';
import { commandWrapper } from '../../wrapper';
import { BundleOptions, handleBundle } from '../../commands/bundle.js';
import { handleError } from '../../utils/miscellaneous.js';
import { commandWrapper } from '../../wrapper.js';
import SpyInstance = jest.SpyInstance;
import { Arguments } from 'yargs';

Expand Down
8 changes: 4 additions & 4 deletions packages/cli/src/__tests__/commands/join.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { yellow } from 'colorette';
import { detectSpec } from '@redocly/openapi-core';
import { handleJoin } from '../../commands/join';
import { exitWithError, writeToFileByExtension } from '../../utils/miscellaneous';
import { loadConfig } from '../../__mocks__/@redocly/openapi-core';
import { ConfigFixture } from '../fixtures/config';
import { handleJoin } from '../../commands/join.js';
import { exitWithError, writeToFileByExtension } from '../../utils/miscellaneous.js';
import { loadConfig } from '../../__mocks__/@redocly/openapi-core.js';
import { ConfigFixture } from '../fixtures/config.js';

jest.mock('../../utils/miscellaneous');

Expand Down
8 changes: 4 additions & 4 deletions packages/cli/src/__tests__/commands/lint.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { handleLint, LintOptions } from '../../commands/lint';
import { handleLint, LintOptions } from '../../commands/lint.js';
import {
getMergedConfig,
lint,
Expand All @@ -14,10 +14,10 @@ import {
exitWithError,
loadConfigAndHandleErrors,
checkIfRulesetExist,
} from '../../utils/miscellaneous';
import { ConfigFixture } from '../fixtures/config';
} from '../../utils/miscellaneous.js';
import { ConfigFixture } from '../fixtures/config.js';
import { performance } from 'perf_hooks';
import { commandWrapper } from '../../wrapper';
import { commandWrapper } from '../../wrapper.js';
import { Arguments } from 'yargs';
import { blue } from 'colorette';

Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/__tests__/commands/push-region.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getMergedConfig } from '@redocly/openapi-core';
import { handlePush } from '../../commands/push';
import { promptClientToken } from '../../commands/login';
import { ConfigFixture } from '../fixtures/config';
import { handlePush } from '../../commands/push.js';
import { promptClientToken } from '../../commands/login.js';
import { ConfigFixture } from '../fixtures/config.js';

jest.mock('fs');
jest.mock('node-fetch', () => ({
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/__tests__/commands/push.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as fs from 'fs';
import { Config, getMergedConfig } from '@redocly/openapi-core';
import { exitWithError } from '../../utils/miscellaneous';
import { getApiRoot, getDestinationProps, handlePush, transformPush } from '../../commands/push';
import { ConfigFixture } from '../fixtures/config';
import { exitWithError } from '../../utils/miscellaneous.js';
import { getApiRoot, getDestinationProps, handlePush, transformPush } from '../../commands/push.js';
import { ConfigFixture } from '../fixtures/config.js';
import { yellow } from 'colorette';

jest.mock('fs');
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/src/__tests__/fetch-with-timeout.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import AbortController from 'abort-controller';
import fetchWithTimeout from '../utils/fetch-with-timeout';
import fetchWithTimeout from '../utils/fetch-with-timeout.js';
import nodeFetch from 'node-fetch';

jest.mock('node-fetch');
Expand All @@ -17,6 +17,7 @@ describe('fetchWithTimeout', () => {
await fetchWithTimeout('url');

expect(global.setTimeout).toHaveBeenCalledTimes(1);
// @ts-ignore FIXME:
expect(nodeFetch).toHaveBeenCalledWith('url', { signal: new AbortController().signal });
expect(global.clearTimeout).toHaveBeenCalledTimes(1);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/__tests__/spinner.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Spinner } from '../utils/spinner';
import { Spinner } from '../utils/spinner.js';
import * as process from 'process';

jest.useFakeTimers();
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
cleanRawInput,
getAndValidateFileExtension,
writeToFileByExtension,
} from '../utils/miscellaneous';
} from '../utils/miscellaneous.js';
import {
ResolvedApi,
Totals,
Expand Down
8 changes: 4 additions & 4 deletions packages/cli/src/__tests__/wrapper.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { loadConfigAndHandleErrors, sendTelemetry } from '../utils/miscellaneous';
import { loadConfigAndHandleErrors, sendTelemetry } from '../utils/miscellaneous.js';
import * as process from 'process';
import { commandWrapper } from '../wrapper';
import { handleLint } from '../commands/lint';
import { commandWrapper } from '../wrapper.js';
import { handleLint } from '../commands/lint.js';
import { Arguments } from 'yargs';
import { handlePush, PushOptions } from '../commands/push';
import { handlePush, PushOptions } from '../commands/push.js';
import { detectSpec } from '@redocly/openapi-core';

jest.mock('node-fetch');
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/cms/api/__tests__/api-keys.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getApiKeys } from '../api-keys';
import { getApiKeys } from '../api-keys.js';
import * as fs from 'fs';

describe('getApiKeys()', () => {
Expand Down
8 changes: 5 additions & 3 deletions packages/cli/src/cms/api/__tests__/api.client.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import fetch, { Response } from 'node-fetch';
import * as FormData from 'form-data';
import _fetch, { Response } from 'node-fetch';
import FormData from 'form-data';

import { ReuniteApiClient, PushPayload } from '../api-client';
import { ReuniteApiClient, PushPayload } from '../api-client.js';

const fetch = _fetch.default;

jest.mock('node-fetch', () => ({
default: jest.fn(),
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/cms/api/__tests__/domains.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getDomain } from '../domains';
import { getDomain } from '../domains.js';

describe('getDomain()', () => {
it('should return the domain from environment variable', () => {
Expand Down
10 changes: 6 additions & 4 deletions packages/cli/src/cms/api/api-client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fetch from 'node-fetch';
import * as FormData from 'form-data';
import _fetch from 'node-fetch';
import FormData from 'form-data';
import { getProxyAgent } from '@redocly/openapi-core';
import fetchWithTimeout from '../../utils/fetch-with-timeout';
import fetchWithTimeout from '../../utils/fetch-with-timeout.js';

import type { Response } from 'node-fetch';
import type { ReadStream } from 'fs';
Expand All @@ -10,7 +10,9 @@ import type {
ProjectSourceResponse,
PushResponse,
UpsertRemoteResponse,
} from './types';
} from './types.js';

const fetch = _fetch.default;

class RemotesApiClient {
constructor(private readonly domain: string, private readonly apiKey: string) {}
Expand Down
3 changes: 1 addition & 2 deletions packages/cli/src/cms/api/api-keys.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { resolve } from 'path';
import { homedir } from 'os';
import { existsSync, readFileSync } from 'fs';
import { isNotEmptyObject } from '@redocly/openapi-core/lib/utils';
import { TOKEN_FILENAME } from '@redocly/openapi-core/lib/redocly';
import { isNotEmptyObject, TOKEN_FILENAME } from '@redocly/openapi-core';

function readCredentialsFile(credentialsPath: string) {
return existsSync(credentialsPath) ? JSON.parse(readFileSync(credentialsPath, 'utf-8')) : {};
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/cms/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './api-client';
export * from './domains';
export * from './api-keys';
export * from './api-client.js';
export * from './domains.js';
export * from './api-keys.js';
4 changes: 2 additions & 2 deletions packages/cli/src/cms/commands/__tests__/push-status.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { handlePushStatus } from '../push-status';
import { PushResponse } from '../../api/types';
import { handlePushStatus } from '../push-status.js';
import { PushResponse } from '../../api/types.js';

const remotes = {
getPush: jest.fn(),
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/cms/commands/__tests__/push.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as fs from 'fs';
import * as path from 'path';
import { handlePush } from '../push';
import { ReuniteApiClient } from '../../api';
import { handlePush } from '../push.js';
import { ReuniteApiClient } from '../../api/index.js';

const remotes = {
push: jest.fn(),
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/cms/commands/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { retryUntilConditionMet } from '../utils';
import { retryUntilConditionMet } from '../utils.js';

jest.mock('@redocly/openapi-core', () => ({
pause: jest.requireActual('@redocly/openapi-core').pause,
Expand Down
Loading

0 comments on commit e9f48ec

Please sign in to comment.