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 1e7e4a9
Show file tree
Hide file tree
Showing 286 changed files with 1,509 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
62 changes: 59 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 Expand Up @@ -61,3 +61,59 @@ export function cleanupOutput(message: string) {
const cwdRegexp = new RegExp(process.cwd(), 'g');
return message.replace(cwdRegexp, '.');
}

// ---------------------------------------------

// const { readFileSync } = require('fs');
// const { join } = require('path');
// import { parseYaml } from '../packages/core/src/utils.js'; // not able to import from @redocly/openapi-core
// import { addSerializer } from './specific-snapshot.cjs';
// import { spawnSync } from 'child_process';

// function getParams(indexEntryPoint, command, args) {
// return ['--transpile-only', indexEntryPoint, command, ...args];
// }

// function getEntrypoints(folderPath) {
// const redoclyYamlFile = readFileSync(join(folderPath, 'redocly.yaml'), 'utf8');
// const redoclyYaml = parseYaml(redoclyYamlFile);
// return Object.keys(redoclyYaml.apis);
// }

// function getCommandOutput(params, folderPath) {
// const result = spawnSync('ts-node', params, {
// cwd: folderPath,
// env: {
// ...process.env,
// NODE_ENV: 'test',
// NO_COLOR: 'TRUE',
// },
// });
// const out = result.stdout.toString('utf-8');
// const err = result.stderr.toString('utf-8');
// return `${out}\n${err}`;
// }

// function cleanUpVersion(str) {
// return str.replace(/"version":\s(\".*\")*/g, '"version": "<version>"');
// }

// function callSerializer() {
// addSerializer({
// test: (val) => typeof val === 'string',
// print: (v) => cleanUpVersion(v),
// });
// }

// function cleanupOutput(message) {
// const cwdRegexp = new RegExp(process.cwd(), 'g');
// return message.replace(cwdRegexp, '.');
// }

// module.exports = {
// getParams,
// getEntrypoints,
// getCommandOutput,
// callSerializer,
// cleanupOutput,
// };
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
Loading

0 comments on commit 1e7e4a9

Please sign in to comment.