diff --git a/packages/jest-transform/__tests__/__snapshots__/index.test.js.snap b/packages/jest-transform/__tests__/__snapshots__/index.test.mjs.snap similarity index 100% rename from packages/jest-transform/__tests__/__snapshots__/index.test.js.snap rename to packages/jest-transform/__tests__/__snapshots__/index.test.mjs.snap diff --git a/packages/jest-transform/__tests__/index.test.js b/packages/jest-transform/__tests__/index.test.mjs similarity index 100% rename from packages/jest-transform/__tests__/index.test.js rename to packages/jest-transform/__tests__/index.test.mjs diff --git a/packages/jest-transform/jest.config.mjs b/packages/jest-transform/jest.config.mjs index 92696ee..47d90cc 100644 --- a/packages/jest-transform/jest.config.mjs +++ b/packages/jest-transform/jest.config.mjs @@ -157,10 +157,10 @@ const config = { // testLocationInResults: false, // The glob patterns Jest uses to detect test files - // testMatch: [ - // "**/__tests__/**/*.[jt]s?(x)", - // "**/?(*.)+(spec|test).[tj]s?(x)", - // ], + testMatch: [ + "**/__tests__/**/*.(m)[jt]s?(x)", + // "**/?(*.)+(spec|test).[tj]s?(x)", + ], // An array of regexp pattern strings that are matched against all test paths, matched tests are skipped // testPathIgnorePatterns: [ diff --git a/packages/jest-transform/package.json b/packages/jest-transform/package.json index 67481fd..8c252fb 100644 --- a/packages/jest-transform/package.json +++ b/packages/jest-transform/package.json @@ -7,7 +7,6 @@ "url": "https://github.com/uhyo/nitrogql.git", "directory": "packages/jest-transform" }, - "type": "module", "main": "dist/index.js", "types": "dist/index.d.ts", "exports": { diff --git a/packages/jest-transform/src/index.ts b/packages/jest-transform/src/index.ts index 896826d..3356763 100644 --- a/packages/jest-transform/src/index.ts +++ b/packages/jest-transform/src/index.ts @@ -1,11 +1,13 @@ import { readFileSync } from "node:fs"; -import type { SyncTransformer, TransformedSource } from "@jest/transform"; -import { executeConfigFileSync } from "@nitrogql/core"; -import { init } from "@nitrogql/loader-core"; +import type { + SyncTransformer, + TransformedSource, + TransformerCreator, +} from "@jest/transform"; -const { initiateTask, getLog } = await init(); - -let lastLoadedConfigPath: string | undefined = undefined; +// Note: this package uses CommonJS because jest-chain-transform does not support loading +// ESM modules. +// See: https://github.com/anc95/jest-chain-transform/issues/11 export type TransformerConfig = { /** @@ -14,43 +16,56 @@ export type TransformerConfig = { configFile?: string; }; -const transformer: SyncTransformer = { - process(sourceText, sourcePath, options): TransformedSource { - const configFile = options.transformerConfig.configFile; - const task = initiateTask(sourcePath, sourceText); - - if (lastLoadedConfigPath !== configFile && configFile) { - const configFileSource = configFileIsJS(configFile) - ? executeConfigFileSync(configFile) - : readFileSync(configFile, "utf-8"); - task.loadConfig(configFileSource); - } - lastLoadedConfigPath = configFile; - - while (true) { - const status = task.status(); - switch (status.status) { - case "fileRequired": { - const requiredFiles = status.files; - for (const requiredFile of requiredFiles) { - const requiredFileSource = readFileSync(requiredFile, "utf-8"); - task.supplyFile(requiredFile, requiredFileSource); +const createTransformer: TransformerCreator< + SyncTransformer, + TransformerConfig +> = async () => { + const { init } = await import("@nitrogql/loader-core"); + const { executeConfigFileSync } = await import("@nitrogql/core"); + const { initiateTask, getLog } = await init(); + + let lastLoadedConfigPath: string | undefined = undefined; + const transformer: SyncTransformer = { + process(sourceText, sourcePath, options): TransformedSource { + const configFile = options.transformerConfig.configFile; + const task = initiateTask(sourcePath, sourceText); + + if (lastLoadedConfigPath !== configFile && configFile) { + const configFileSource = configFileIsJS(configFile) + ? executeConfigFileSync(configFile) + : readFileSync(configFile, "utf-8"); + task.loadConfig(configFileSource); + } + lastLoadedConfigPath = configFile; + + while (true) { + const status = task.status(); + switch (status.status) { + case "fileRequired": { + const requiredFiles = status.files; + for (const requiredFile of requiredFiles) { + const requiredFileSource = readFileSync(requiredFile, "utf-8"); + task.supplyFile(requiredFile, requiredFileSource); + } + break; + } + case "ready": { + const result = task.emit(); + task.free(); + return { + code: result, + }; } - break; - } - case "ready": { - const result = task.emit(); - task.free(); - return { - code: result, - }; } } - } - }, + }, + }; + return transformer; }; -export default transformer; +export default { + createTransformer, +}; function configFileIsJS(configFile: string) { return /\.[cm]?[jt]s$/.test(configFile);