test: Nodeのカスタムローダーを直してテストが動くように (#8625)

* test: Nodeのカスタムローダーを直してテストが動くように

* dev: mochaを呼ぶコマンドにNODE_ENV=testを追加

* Update packages/backend/test/loader.js

Co-authored-by: Johann150 <johann@qwertqwefsday.eu>

* chore: change export style in loader.js

Co-authored-by: Johann150 <johann@qwertqwefsday.eu>
This commit is contained in:
iwata 2022-05-14 16:10:20 +09:00 committed by GitHub
parent ebb4308a5c
commit 67e1ee41c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 34 deletions

View File

@ -22,7 +22,7 @@
"cy:open": "cypress open", "cy:open": "cypress open",
"cy:run": "cypress run", "cy:run": "cypress run",
"e2e": "start-server-and-test start:test http://localhost:61812 cy:run", "e2e": "start-server-and-test start:test http://localhost:61812 cy:run",
"mocha": "cd packages/backend && cross-env TS_NODE_FILES=true TS_NODE_TRANSPILE_ONLY=true TS_NODE_PROJECT=\"./test/tsconfig.json\" npx mocha", "mocha": "cd packages/backend && cross-env NODE_ENV=test TS_NODE_FILES=true TS_NODE_TRANSPILE_ONLY=true TS_NODE_PROJECT=\"./test/tsconfig.json\" npx mocha",
"test": "npm run mocha", "test": "npm run mocha",
"format": "gulp format", "format": "gulp format",
"clean": "node ./scripts/clean.js", "clean": "node ./scripts/clean.js",

View File

@ -6,7 +6,7 @@
"build": "tsc -p tsconfig.json || echo done. && tsc-alias -p tsconfig.json", "build": "tsc -p tsconfig.json || echo done. && tsc-alias -p tsconfig.json",
"watch": "node watch.mjs", "watch": "node watch.mjs",
"lint": "eslint --quiet \"src/**/*.ts\"", "lint": "eslint --quiet \"src/**/*.ts\"",
"mocha": "cross-env TS_NODE_FILES=true TS_NODE_TRANSPILE_ONLY=true TS_NODE_PROJECT=\"./test/tsconfig.json\" mocha", "mocha": "cross-env NODE_ENV=test TS_NODE_FILES=true TS_NODE_TRANSPILE_ONLY=true TS_NODE_PROJECT=\"./test/tsconfig.json\" mocha",
"test": "npm run mocha" "test": "npm run mocha"
}, },
"resolutions": { "resolutions": {

View File

@ -1,37 +1,34 @@
import path from 'path' /**
import typescript from 'typescript' * ts-node/esmローダーに投げる前にpath mappingを解決する
import { createMatchPath } from 'tsconfig-paths' * 参考
import { resolve as BaseResolve, getFormat, transformSource } from 'ts-node/esm' * - https://github.com/TypeStrong/ts-node/discussions/1450#discussioncomment-1806115
* - https://nodejs.org/api/esm.html#loaders
* https://github.com/TypeStrong/ts-node/pull/1585 が取り込まれたらこのカスタムローダーは必要なくなる
*/
const { readConfigFile, parseJsonConfigFileContent, sys } = typescript import { resolve as resolveTs, load } from 'ts-node/esm';
import { loadConfig, createMatchPath } from 'tsconfig-paths';
import { pathToFileURL } from 'url';
const __dirname = path.dirname(new URL(import.meta.url).pathname) const tsconfig = loadConfig();
const matchPath = createMatchPath(tsconfig.absoluteBaseUrl, tsconfig.paths);
const configFile = readConfigFile('./test/tsconfig.json', sys.readFile) export function resolve(specifier, ctx, defaultResolve) {
if (typeof configFile.error !== 'undefined') { let resolvedSpecifier;
throw new Error(`Failed to load tsconfig: ${configFile.error}`) if (specifier.endsWith('.js')) {
// maybe transpiled
const specifierWithoutExtension = specifier.substring(0, specifier.length - '.js'.length);
const matchedSpecifier = matchPath(specifierWithoutExtension);
if (matchedSpecifier) {
resolvedSpecifier = pathToFileURL(`${matchedSpecifier}.js`).href;
}
} else {
const matchedSpecifier = matchPath(specifier);
if (matchedSpecifier) {
resolvedSpecifier = pathToFileURL(matchedSpecifier).href;
}
}
return resolveTs(resolvedSpecifier ?? specifier, ctx, defaultResolve);
} }
const { options } = parseJsonConfigFileContent( export { load };
configFile.config,
{
fileExists: sys.fileExists,
readFile: sys.readFile,
readDirectory: sys.readDirectory,
useCaseSensitiveFileNames: true,
},
__dirname
)
export { getFormat, transformSource } // こいつらはそのまま使ってほしいので re-export する
const matchPath = createMatchPath(options.baseUrl, options.paths)
export async function resolve(specifier, context, defaultResolve) {
const matchedSpecifier = matchPath(specifier.replace('.js', '.ts'))
return BaseResolve( // ts-node/esm の resolve に tsconfig-paths で解決したパスを渡す
matchedSpecifier ? `${matchedSpecifier}.ts` : specifier,
context,
defaultResolve
)
}