From 1749456251bbd315b55bd0c8810c36eb31d52c85 Mon Sep 17 00:00:00 2001 From: jjangga0214 Date: Sun, 4 Sep 2022 20:10:32 +0900 Subject: [PATCH] test(e2e): for `pathsToModuleNameMapper` in esm project --- e2e/__tests__/native-esm-ts.test.ts | 4 ++-- .../__tests__/native-esm-ts.spec.ts | 7 +++++- e2e/native-esm-ts/jest-isolated.config.js | 8 +++---- e2e/native-esm-ts/jest.config.js | 23 +++++++++++++++++++ e2e/native-esm-ts/package.json | 9 -------- e2e/native-esm-ts/quadruple/calculate.ts | 2 ++ e2e/native-esm-ts/quadruple/index.ts | 1 + e2e/native-esm-ts/tsconfig.json | 5 +++- 8 files changed, 42 insertions(+), 17 deletions(-) create mode 100644 e2e/native-esm-ts/jest.config.js create mode 100644 e2e/native-esm-ts/quadruple/calculate.ts create mode 100644 e2e/native-esm-ts/quadruple/index.ts diff --git a/e2e/__tests__/native-esm-ts.test.ts b/e2e/__tests__/native-esm-ts.test.ts index 50efbe8a41..7fdc21753d 100644 --- a/e2e/__tests__/native-esm-ts.test.ts +++ b/e2e/__tests__/native-esm-ts.test.ts @@ -8,7 +8,7 @@ onNodeVersions('>=12.16.0', () => { }) expect(exitCode).toBe(0) - expect(json.numTotalTests).toBe(3) - expect(json.numPassedTests).toBe(3) + expect(json.numTotalTests).toBe(4) + expect(json.numPassedTests).toBe(4) }) }) diff --git a/e2e/native-esm-ts/__tests__/native-esm-ts.spec.ts b/e2e/native-esm-ts/__tests__/native-esm-ts.spec.ts index 22e0000bd2..d54f4f0056 100644 --- a/e2e/native-esm-ts/__tests__/native-esm-ts.spec.ts +++ b/e2e/native-esm-ts/__tests__/native-esm-ts.spec.ts @@ -1,6 +1,7 @@ import { test, expect } from '@jest/globals' -import { double } from '../double' +import { double } from '../double.js' +import { quadruple } from '../quadruple/index.js' import { triple } from '../triple.mjs' test('double', () => { @@ -11,6 +12,10 @@ test('triple', () => { expect(triple(2)).toBe(6) }) +test('quadruple', () => { + expect(quadruple(2)).toBe(8) +}) + test('import.meta', () => { expect(typeof import.meta.url).toBe('string') }) diff --git a/e2e/native-esm-ts/jest-isolated.config.js b/e2e/native-esm-ts/jest-isolated.config.js index 2efd1bdabc..d9c1547803 100644 --- a/e2e/native-esm-ts/jest-isolated.config.js +++ b/e2e/native-esm-ts/jest-isolated.config.js @@ -1,7 +1,7 @@ -/** @type {import('../../dist').InitialOptionsTsJest} */ -module.exports = { - extensionsToTreatAsEsm: ['.ts'], - resolver: '/mjs-resolver.ts', +import config from './jest.config.js' + +export default { + ...config, transform: { '^.+\\.m?tsx?$': [ '/../../legacy.js', diff --git a/e2e/native-esm-ts/jest.config.js b/e2e/native-esm-ts/jest.config.js new file mode 100644 index 0000000000..7585b9333d --- /dev/null +++ b/e2e/native-esm-ts/jest.config.js @@ -0,0 +1,23 @@ +import { pathsToModuleNameMapper } from '../../dist/index.js' +import { createRequire } from 'module' + +const require = createRequire(import.meta.url) +const tsConfig = require('./tsconfig.json') + +/** @type {import('../../dist').InitialOptionsTsJest} */ +export default { + extensionsToTreatAsEsm: ['.ts'], + resolver: '/mjs-resolver.ts', + moduleNameMapper: pathsToModuleNameMapper(tsConfig.compilerOptions.paths, { + prefix: '', + removeJsExtension: true, + }), + transform: { + '^.+\\.m?tsx?$': [ + '/../../legacy.js', + { + useESM: true, + }, + ], + }, +} diff --git a/e2e/native-esm-ts/package.json b/e2e/native-esm-ts/package.json index 7df48aadd9..3736ab195b 100644 --- a/e2e/native-esm-ts/package.json +++ b/e2e/native-esm-ts/package.json @@ -2,14 +2,5 @@ "type": "module", "devDependencies": { "@jest/globals": "^29.0.2" - }, - "jest": { - "extensionsToTreatAsEsm": [".ts"], - "resolver": "/mjs-resolver.ts", - "transform": { - "^.+\\.m?tsx?$": ["/../../legacy.js", { - "useESM": true - }] - } } } diff --git a/e2e/native-esm-ts/quadruple/calculate.ts b/e2e/native-esm-ts/quadruple/calculate.ts new file mode 100644 index 0000000000..4f3afc5b44 --- /dev/null +++ b/e2e/native-esm-ts/quadruple/calculate.ts @@ -0,0 +1,2 @@ +const calculate = (x: number) => x * 4 +export default calculate diff --git a/e2e/native-esm-ts/quadruple/index.ts b/e2e/native-esm-ts/quadruple/index.ts new file mode 100644 index 0000000000..78919a5fe9 --- /dev/null +++ b/e2e/native-esm-ts/quadruple/index.ts @@ -0,0 +1 @@ +export { default as quadruple } from '@quadruple/calculate.js' diff --git a/e2e/native-esm-ts/tsconfig.json b/e2e/native-esm-ts/tsconfig.json index a33e7ef827..3ef020cec9 100644 --- a/e2e/native-esm-ts/tsconfig.json +++ b/e2e/native-esm-ts/tsconfig.json @@ -3,6 +3,9 @@ "module": "Node16", "target": "ESNext", "moduleResolution": "Node16", - "esModuleInterop": true + "esModuleInterop": true, + "paths": { + "@quadruple/*": ["quadruple/*"] + } } }