mirror of
https://github.com/logto-io/logto.git
synced 2024-12-30 20:33:54 -05:00
refactor: use esm (batch 5)
This commit is contained in:
parent
8c93cbb650
commit
d0d9321e79
29 changed files with 884 additions and 33 deletions
|
@ -2,10 +2,22 @@ import type { Config } from '@silverhand/jest-config';
|
|||
import { merge } from '@silverhand/jest-config';
|
||||
|
||||
const config: Config.InitialOptions = merge({
|
||||
// Preset: 'ts-jest/presets/default-esm',
|
||||
// TransformIgnorePatterns: ['schemas'],
|
||||
setupFilesAfterEnv: ['./jest.setup.ts'],
|
||||
roots: ['./src'],
|
||||
moduleNameMapper: {
|
||||
'^(\\.{1,2}/.*)\\.js$': '$1',
|
||||
},
|
||||
globals: {
|
||||
'ts-jest': {
|
||||
useESM: true,
|
||||
diagnostics: {
|
||||
ignoreCodes: [1343],
|
||||
},
|
||||
},
|
||||
},
|
||||
// ExtensionsToTreatAsEsm: ['.ts', '.tsx'],
|
||||
});
|
||||
|
||||
export default config;
|
||||
|
|
19
packages/cli/jest.setup.ts
Normal file
19
packages/cli/jest.setup.ts
Normal file
|
@ -0,0 +1,19 @@
|
|||
/**
|
||||
* Mocking `import.meta.url` and `got` here since they inevitably needs native ESM, but jest is sticking with CJS.
|
||||
* Will figure out a way to run tests in native ESM mode.
|
||||
*/
|
||||
|
||||
jest.mock('./src/commands/database/alteration/meta-url.js', () => ({
|
||||
metaUrl: 'file:///',
|
||||
}));
|
||||
|
||||
jest.mock('./src/meta-url.js', () => ({
|
||||
metaUrl: 'file:///',
|
||||
}));
|
||||
|
||||
jest.mock('got', () => ({
|
||||
got: {},
|
||||
}));
|
||||
|
||||
// Make lint-staged happy
|
||||
export {};
|
|
@ -75,7 +75,7 @@
|
|||
"@types/tar": "^6.1.2",
|
||||
"@types/yargs": "^17.0.13",
|
||||
"eslint": "^8.21.0",
|
||||
"jest": "^29.1.2",
|
||||
"jest": "^29.3.1",
|
||||
"lint-staged": "^13.0.0",
|
||||
"prettier": "^2.7.1",
|
||||
"rimraf": "^3.0.2",
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import { fileURLToPath } from 'node:url';
|
||||
import path from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
|
||||
import type { AlterationScript } from '@logto/schemas/lib/types/alteration.js';
|
||||
import { findPackage } from '@logto/shared';
|
||||
|
@ -15,10 +15,11 @@ import {
|
|||
updateDatabaseTimestamp,
|
||||
} from '../../../queries/logto-config.js';
|
||||
import { getPathInModule, log } from '../../../utilities.js';
|
||||
import { metaUrl } from './meta-url.js';
|
||||
import type { AlterationFile } from './type.js';
|
||||
import { chooseAlterationsByVersion } from './version.js';
|
||||
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||
const currentDirname = path.dirname(fileURLToPath(metaUrl));
|
||||
const { copy, existsSync, remove, readdir } = fsExtra;
|
||||
const alterationFilenameRegex = /-(\d+)-?.*\.js$/;
|
||||
|
||||
|
@ -48,10 +49,10 @@ export const getAlterationFiles = async (): Promise<AlterationFile[]> => {
|
|||
* since they need a proper context that includes required dependencies (such as slonik) in `node_modules/`.
|
||||
* While the original `@logto/schemas` may remove them in production.
|
||||
*/
|
||||
const packageDirectory = await findPackage(__dirname);
|
||||
const packageDirectory = await findPackage(currentDirname);
|
||||
|
||||
const localAlterationDirectory = path.resolve(
|
||||
packageDirectory ?? __dirname,
|
||||
packageDirectory ?? currentDirname,
|
||||
'alteration-scripts'
|
||||
);
|
||||
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
// Have to define this in a separate file since Jest sticks with CJS
|
||||
// We need to mock this before running tests
|
||||
// https://github.com/facebook/jest/issues/12952
|
||||
export const metaUrl = import.meta.url;
|
4
packages/cli/src/meta-url.ts
Normal file
4
packages/cli/src/meta-url.ts
Normal file
|
@ -0,0 +1,4 @@
|
|||
// Have to define this in a separate file since Jest sticks with CJS
|
||||
// We need to mock this before running tests
|
||||
// https://github.com/facebook/jest/issues/12952
|
||||
export const metaUrl = import.meta.url;
|
|
@ -14,6 +14,8 @@ import inquirer from 'inquirer';
|
|||
import ora from 'ora';
|
||||
import { z } from 'zod';
|
||||
|
||||
import { metaUrl } from './meta-url.js';
|
||||
|
||||
export const safeExecSync = (command: string) => {
|
||||
try {
|
||||
return execSync(command, { encoding: 'utf8', stdio: 'pipe' });
|
||||
|
@ -84,7 +86,7 @@ export const downloadFile = async (url: string, destination: string) => {
|
|||
export const getPathInModule = (moduleName: string, relativePath = '/') =>
|
||||
// https://stackoverflow.com/a/49455609/12514940
|
||||
path.join(
|
||||
path.dirname(createRequire(import.meta.url).resolve(`${moduleName}/package.json`)),
|
||||
path.dirname(createRequire(metaUrl).resolve(`${moduleName}/package.json`)),
|
||||
relativePath
|
||||
);
|
||||
|
||||
|
|
|
@ -10,7 +10,7 @@
|
|||
},
|
||||
"include": [
|
||||
"src",
|
||||
"jest.config.ts"
|
||||
"jest.*.ts"
|
||||
],
|
||||
"exclude": ["**/alteration-scripts"]
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
{
|
||||
"extends": "./tsconfig",
|
||||
"compilerOptions": {
|
||||
"isolatedModules": false,
|
||||
"allowJs": true
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
"author": "Silverhand Inc. <contact@silverhand.io>",
|
||||
"homepage": "https://github.com/logto-io/logto#readme",
|
||||
"license": "MPL-2.0",
|
||||
"type": "module",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"precommit": "lint-staged",
|
||||
|
|
|
@ -11,3 +11,24 @@ jest.mock('#src/env-set/check-alteration-state.js');
|
|||
(async () => {
|
||||
await envSet.load();
|
||||
})();
|
||||
|
||||
/**
|
||||
* Mocking `import.meta.url` and `got` here since they inevitably needs native ESM, but jest is sticking with CJS.
|
||||
* Will figure out a way to run tests in native ESM mode.
|
||||
*/
|
||||
|
||||
jest.mock('./src/connectors/meta-url.js', () => ({
|
||||
metaUrl: 'file:///',
|
||||
}));
|
||||
|
||||
jest.mock('../cli/lib/meta-url.js', () => ({
|
||||
metaUrl: 'file:///',
|
||||
}));
|
||||
|
||||
jest.mock('../cli/lib/commands/database/alteration/meta-url.js', () => ({
|
||||
metaUrl: 'file:///',
|
||||
}));
|
||||
|
||||
jest.mock('got', () => ({
|
||||
got: {},
|
||||
}));
|
||||
|
|
|
@ -42,7 +42,6 @@
|
|||
"etag": "^1.8.1",
|
||||
"find-up": "^5.0.0",
|
||||
"fs-extra": "^10.1.0",
|
||||
"got": "^11.8.5",
|
||||
"hash-wasm": "^4.9.0",
|
||||
"i18next": "^21.8.16",
|
||||
"iconv-lite": "0.6.3",
|
||||
|
|
|
@ -13,10 +13,11 @@ import RequestError from '#src/errors/RequestError/index.js';
|
|||
import { findAllConnectors, insertConnector } from '#src/queries/connector.js';
|
||||
|
||||
import { defaultConnectorMethods } from './consts.js';
|
||||
import { metaUrl } from './meta-url.js';
|
||||
import type { VirtualConnector, LogtoConnector } from './types.js';
|
||||
import { getConnectorConfig, readUrl, validateConnectorModule } from './utilities/index.js';
|
||||
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||
const currentDirname = path.dirname(fileURLToPath(metaUrl));
|
||||
// eslint-disable-next-line @silverhand/fp/no-let
|
||||
let cachedVirtualConnectors: VirtualConnector[] | undefined;
|
||||
|
||||
|
@ -25,7 +26,7 @@ export const loadVirtualConnectors = async () => {
|
|||
return cachedVirtualConnectors;
|
||||
}
|
||||
|
||||
const coreDirectory = await findPackage(__dirname);
|
||||
const coreDirectory = await findPackage(currentDirname);
|
||||
const directory = coreDirectory && path.join(coreDirectory, connectorDirectory);
|
||||
|
||||
if (!directory || !existsSync(directory)) {
|
||||
|
|
4
packages/core/src/connectors/meta-url.ts
Normal file
4
packages/core/src/connectors/meta-url.ts
Normal file
|
@ -0,0 +1,4 @@
|
|||
// Have to define this in a separate file since Jest sticks with CJS
|
||||
// We need to mock this before running tests
|
||||
// https://github.com/facebook/jest/issues/12952
|
||||
export const metaUrl = import.meta.url;
|
|
@ -19,7 +19,7 @@ const connectors: Connector[] = [
|
|||
const findAllConnectors = jest.fn(async () => connectors);
|
||||
|
||||
jest.mock('#src/queries/connector.js', () => ({
|
||||
...jest.requireActual('@/queries/connector'),
|
||||
...jest.requireActual('#src/queries/connector.js'),
|
||||
findAllConnectors: async () => findAllConnectors(),
|
||||
}));
|
||||
|
||||
|
|
|
@ -8,7 +8,7 @@ import { validateSignUp } from './sign-up.js';
|
|||
const enabledConnectors = [mockAliyunDmConnector, mockAliyunSmsConnector];
|
||||
|
||||
jest.mock('#src/lib/session.js', () => ({
|
||||
...jest.requireActual('@/lib/session'),
|
||||
...jest.requireActual('#src/lib/session.js'),
|
||||
getApplicationIdFromInteraction: jest.fn(),
|
||||
}));
|
||||
|
||||
|
|
|
@ -39,7 +39,7 @@ jest.mock('#src/queries/custom-phrase.js', () => ({
|
|||
}));
|
||||
|
||||
jest.mock('#src/lib/phrase.js', () => ({
|
||||
...jest.requireActual('@/lib/phrase'),
|
||||
...jest.requireActual('#src/lib/phrase.js'),
|
||||
getPhrase: jest.fn().mockResolvedValue(en),
|
||||
}));
|
||||
|
||||
|
|
|
@ -53,7 +53,7 @@ jest.mock('#src/queries/custom-phrase.js', () => ({
|
|||
const getPhrase = jest.fn(async (language: string, customLanguages: string[]) => zhCN);
|
||||
|
||||
jest.mock('#src/lib/phrase.js', () => ({
|
||||
...jest.requireActual('@/lib/phrase'),
|
||||
...jest.requireActual('#src/lib/phrase.js'),
|
||||
getPhrase: async (language: string, customLanguages: string[]) =>
|
||||
getPhrase(language, customLanguages),
|
||||
}));
|
||||
|
|
|
@ -24,12 +24,12 @@ const getYesterdayDate = () => subDays(Date.now(), 1);
|
|||
const getTomorrowDate = () => addDays(Date.now(), 1);
|
||||
|
||||
jest.mock('#src/lib/user.js', () => ({
|
||||
...jest.requireActual('@/lib/user'),
|
||||
...jest.requireActual('#src/lib/user.js'),
|
||||
encryptUserPassword: async (password: string) => encryptUserPassword(password),
|
||||
}));
|
||||
|
||||
jest.mock('#src/queries/user.js', () => ({
|
||||
...jest.requireActual('@/queries/user'),
|
||||
...jest.requireActual('#src/queries/user.js'),
|
||||
hasUserWithPhone: async (phone: string) => phone === '13000000000',
|
||||
findUserByPhone: async () => ({ userId: 'id' }),
|
||||
hasUserWithEmail: async (email: string) => email === 'a@a.com',
|
||||
|
|
|
@ -52,7 +52,7 @@ jest.mock('#src/lib/user.js', () => ({
|
|||
}));
|
||||
|
||||
jest.mock('#src/lib/session.js', () => ({
|
||||
...jest.requireActual('@/lib/session'),
|
||||
...jest.requireActual('#src/lib/session.js'),
|
||||
getApplicationIdFromInteraction: jest.fn(),
|
||||
}));
|
||||
|
||||
|
|
|
@ -35,7 +35,7 @@ jest.mock('#src/lib/user.js', () => ({
|
|||
}));
|
||||
|
||||
jest.mock('#src/lib/session.js', () => ({
|
||||
...jest.requireActual('@/lib/session'),
|
||||
...jest.requireActual('#src/lib/session.js'),
|
||||
getApplicationIdFromInteraction: jest.fn(),
|
||||
}));
|
||||
|
||||
|
|
|
@ -42,7 +42,7 @@ jest.mock('oidc-provider', () => ({
|
|||
}));
|
||||
|
||||
jest.mock('#src/lib/user.js', () => ({
|
||||
...jest.requireActual('@/lib/user'),
|
||||
...jest.requireActual('#src/lib/user.js'),
|
||||
encryptUserPassword: async (password: string) => encryptUserPassword(password),
|
||||
}));
|
||||
|
||||
|
@ -69,7 +69,7 @@ jest.mock('#src/lib/social', () => ({
|
|||
}));
|
||||
|
||||
jest.mock('#src/queries/user.js', () => ({
|
||||
...jest.requireActual('@/queries/user'),
|
||||
...jest.requireActual('#src/queries/user.js'),
|
||||
findUserById: async () => mockFindUserById(),
|
||||
hasUser: async () => mockHasUser(),
|
||||
hasUserWithEmail: async () => mockHasUserWithEmail(),
|
||||
|
|
|
@ -15,7 +15,7 @@ const findSocialRelatedUser = jest.fn(async () => [
|
|||
{ id: 'user1', identities: {}, isSuspended: false },
|
||||
]);
|
||||
jest.mock('#src/lib/social.js', () => ({
|
||||
...jest.requireActual('@/lib/social'),
|
||||
...jest.requireActual('#src/lib/social.js'),
|
||||
findSocialRelatedUser: async () => findSocialRelatedUser(),
|
||||
async getUserInfoByAuthCode(connectorId: string, data: { code: string }) {
|
||||
if (connectorId === '_connectorId') {
|
||||
|
|
|
@ -15,7 +15,7 @@ const findSocialRelatedUser = jest.fn(async () => [
|
|||
{ id: 'user1', identities: {}, isSuspended: false },
|
||||
]);
|
||||
jest.mock('#src/lib/social.js', () => ({
|
||||
...jest.requireActual('@/lib/social'),
|
||||
...jest.requireActual('#src/lib/social.js'),
|
||||
findSocialRelatedUser: async () => findSocialRelatedUser(),
|
||||
async getUserInfoByAuthCode(connectorId: string, data: { code: string }) {
|
||||
if (connectorId === '_connectorId') {
|
||||
|
|
|
@ -28,7 +28,7 @@ jest.mock('#src/connectors.js', () => ({
|
|||
const validateLanguageInfo = jest.fn(async (languageInfo: LanguageInfo): Promise<void> => {});
|
||||
|
||||
jest.mock('#src/lib/sign-in-experience.js', () => ({
|
||||
...jest.requireActual('@/lib/sign-in-experience'),
|
||||
...jest.requireActual('#src/lib/sign-in-experience.js'),
|
||||
validateLanguageInfo: async (languageInfo: LanguageInfo) => validateLanguageInfo(languageInfo),
|
||||
}));
|
||||
|
||||
|
|
|
@ -32,7 +32,7 @@ const getLogtoConnectors = jest.fn(async () => logtoConnectors);
|
|||
|
||||
jest.mock('#src/connectors.js', () => {
|
||||
return {
|
||||
...jest.requireActual('@/connectors'),
|
||||
...jest.requireActual('#src/connectors.js'),
|
||||
getLogtoConnectors: jest.fn(async () => getLogtoConnectors()),
|
||||
};
|
||||
});
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
"name": "@logto/ui",
|
||||
"version": "1.0.0-beta.14",
|
||||
"license": "MPL-2.0",
|
||||
"type": "module",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"precommit": "lint-staged",
|
||||
|
|
799
pnpm-lock.yaml
799
pnpm-lock.yaml
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue