|
| 1 | +import path from 'node:path' |
| 2 | + |
| 3 | +import { vol } from 'memfs' |
| 4 | +import type { PackageJson } from 'type-fest' |
| 5 | +import ts from 'typescript' |
| 6 | + |
| 7 | +import { workspaceRoot } from '../../__helpers__/workspace-root' |
| 8 | + |
| 9 | +import { tsTranspileModule } from './transpile-module' |
| 10 | + |
| 11 | +jest.mock('fs', () => { |
| 12 | + const fsImpl = jest.requireActual('memfs').fs |
| 13 | + |
| 14 | + return { |
| 15 | + ...fsImpl, |
| 16 | + readFileSync: (path: string) => { |
| 17 | + if (path.includes('.ts-jest-digest')) { |
| 18 | + return 'ee2e176d55f97f10ef48a41f4e4089940db5d10f' |
| 19 | + } |
| 20 | + |
| 21 | + return fsImpl.readFileSync(path) |
| 22 | + }, |
| 23 | + } |
| 24 | +}) |
| 25 | +jest.mock('node:fs', () => { |
| 26 | + const fsImpl = jest.requireActual('memfs').fs |
| 27 | + |
| 28 | + return { |
| 29 | + ...fsImpl, |
| 30 | + readFileSync: (path: string) => { |
| 31 | + if (path.includes('.ts-jest-digest')) { |
| 32 | + return 'ee2e176d55f97f10ef48a41f4e4089940db5d10f' |
| 33 | + } |
| 34 | + |
| 35 | + return fsImpl.readFileSync(path) |
| 36 | + }, |
| 37 | + } |
| 38 | +}) |
| 39 | + |
| 40 | +function dedent(strings: TemplateStringsArray, ...values: unknown[]) { |
| 41 | + let joinedString = '' |
| 42 | + for (let i = 0; i < values.length; i++) { |
| 43 | + joinedString += `${strings[i]}${values[i]}` |
| 44 | + } |
| 45 | + joinedString += strings[strings.length - 1] |
| 46 | + |
| 47 | + return omitLeadingWhitespace(joinedString) |
| 48 | +} |
| 49 | + |
| 50 | +function omitLeadingWhitespace(text: string): string { |
| 51 | + return text.replace(/^\s+/gm, '') |
| 52 | +} |
| 53 | + |
| 54 | +describe('transpileModules', () => { |
| 55 | + describe('with modern Node resolution', () => { |
| 56 | + const esmModernNodeFilePath = path.join(workspaceRoot, 'esm-node-modern', 'foo.ts') |
| 57 | + const mtsFilePath = path.join(workspaceRoot, 'esm-node-modern', 'foo.mts') |
| 58 | + const cjsModernNodeFilePath = path.join(workspaceRoot, 'cjs-node-modern', 'foo.ts') |
| 59 | + const ctsFilePath = path.join(workspaceRoot, 'esm-node-modern', 'foo.cts') |
| 60 | + vol.fromJSON( |
| 61 | + { |
| 62 | + './esm-node-modern/package.json': JSON.stringify({ |
| 63 | + name: 'test-package-1', |
| 64 | + type: 'module', |
| 65 | + } as PackageJson), |
| 66 | + './esm-node-modern/foo.ts': ` |
| 67 | + import { foo } from 'foo'; |
| 68 | +
|
| 69 | + console.log(foo); |
| 70 | + `, |
| 71 | + './esm-node-modern/foo.mts': ` |
| 72 | + import { foo } from 'foo'; |
| 73 | +
|
| 74 | + console.log(foo); |
| 75 | + `, |
| 76 | + './cjs-node-modern/package.json': JSON.stringify({ |
| 77 | + name: 'test-package-1', |
| 78 | + type: 'commonjs', |
| 79 | + } as PackageJson), |
| 80 | + './cjs-node-modern/foo.ts': ` |
| 81 | + import { foo } from 'foo'; |
| 82 | +
|
| 83 | + console.log(foo); |
| 84 | + `, |
| 85 | + './esm-node-modern/foo.cts': ` |
| 86 | + import { foo } from 'foo'; |
| 87 | +
|
| 88 | + console.log(foo); |
| 89 | + `, |
| 90 | + }, |
| 91 | + workspaceRoot, |
| 92 | + ) |
| 93 | + |
| 94 | + it.each([ |
| 95 | + { |
| 96 | + module: ts.ModuleKind.Node16, |
| 97 | + moduleResolution: ts.ModuleResolutionKind.Node16, |
| 98 | + }, |
| 99 | + { |
| 100 | + module: ts.ModuleKind.NodeNext, |
| 101 | + moduleResolution: ts.ModuleResolutionKind.NodeNext, |
| 102 | + }, |
| 103 | + ])('should emit CJS code with "type: commonjs" in package.json', ({ module, moduleResolution }) => { |
| 104 | + const result = tsTranspileModule(vol.readFileSync(cjsModernNodeFilePath, 'utf-8').toString(), { |
| 105 | + fileName: cjsModernNodeFilePath, |
| 106 | + compilerOptions: { |
| 107 | + module, |
| 108 | + target: ts.ScriptTarget.ESNext, |
| 109 | + verbatimModuleSyntax: true, |
| 110 | + moduleResolution, |
| 111 | + }, |
| 112 | + }) |
| 113 | + |
| 114 | + expect(omitLeadingWhitespace(result.outputText)).toContain(dedent` |
| 115 | + const foo_1 = require("foo"); |
| 116 | + `) |
| 117 | + }) |
| 118 | + |
| 119 | + it.each([ |
| 120 | + { |
| 121 | + module: ts.ModuleKind.Node16, |
| 122 | + moduleResolution: ts.ModuleResolutionKind.Node16, |
| 123 | + }, |
| 124 | + { |
| 125 | + module: ts.ModuleKind.NodeNext, |
| 126 | + moduleResolution: ts.ModuleResolutionKind.NodeNext, |
| 127 | + }, |
| 128 | + ])('should emit ESM code with "type: module" in package.json', ({ module, moduleResolution }) => { |
| 129 | + const result = tsTranspileModule(vol.readFileSync(esmModernNodeFilePath, 'utf-8').toString(), { |
| 130 | + fileName: esmModernNodeFilePath, |
| 131 | + compilerOptions: { |
| 132 | + module, |
| 133 | + target: ts.ScriptTarget.ESNext, |
| 134 | + moduleResolution, |
| 135 | + }, |
| 136 | + }) |
| 137 | + |
| 138 | + expect(omitLeadingWhitespace(result.outputText)).toContain(dedent` |
| 139 | + import { foo } from 'foo'; |
| 140 | + `) |
| 141 | + }) |
| 142 | + |
| 143 | + it('should emit ESM code with .mts extension', () => { |
| 144 | + const result = tsTranspileModule(vol.readFileSync(mtsFilePath, 'utf-8').toString(), { |
| 145 | + fileName: mtsFilePath, |
| 146 | + compilerOptions: { |
| 147 | + target: ts.ScriptTarget.ESNext, |
| 148 | + }, |
| 149 | + }) |
| 150 | + |
| 151 | + expect(omitLeadingWhitespace(result.outputText)).toContain(dedent` |
| 152 | + import { foo } from 'foo'; |
| 153 | + `) |
| 154 | + }) |
| 155 | + |
| 156 | + it('should emit CJS code with .cts extension', () => { |
| 157 | + const result = tsTranspileModule(vol.readFileSync(ctsFilePath, 'utf-8').toString(), { |
| 158 | + fileName: ctsFilePath, |
| 159 | + compilerOptions: { |
| 160 | + target: ts.ScriptTarget.ESNext, |
| 161 | + }, |
| 162 | + }) |
| 163 | + |
| 164 | + expect(omitLeadingWhitespace(result.outputText)).toContain(dedent` |
| 165 | + import { foo } from 'foo'; |
| 166 | + `) |
| 167 | + }) |
| 168 | + }) |
| 169 | + |
| 170 | + describe('with classic CommonJS module and ES module kind', () => { |
| 171 | + vol.fromJSON( |
| 172 | + { |
| 173 | + './foo.ts': ` |
| 174 | + import { foo } from 'foo'; |
| 175 | +
|
| 176 | + console.log(foo); |
| 177 | + `, |
| 178 | + }, |
| 179 | + workspaceRoot, |
| 180 | + ) |
| 181 | + |
| 182 | + it('should emit CJS code with module kind set to CommonJS', () => { |
| 183 | + const filePath = path.join(workspaceRoot, 'foo.ts') |
| 184 | + const result = tsTranspileModule(vol.readFileSync(filePath, 'utf-8').toString(), { |
| 185 | + fileName: filePath, |
| 186 | + compilerOptions: { |
| 187 | + module: ts.ModuleKind.CommonJS, |
| 188 | + target: ts.ScriptTarget.ESNext, |
| 189 | + }, |
| 190 | + }) |
| 191 | + |
| 192 | + expect(omitLeadingWhitespace(result.outputText)).toContain(dedent` |
| 193 | + const foo_1 = require("foo"); |
| 194 | + `) |
| 195 | + }) |
| 196 | + |
| 197 | + it('should emit ESM code with module kind set to one of ES module value', () => { |
| 198 | + const filePath = path.join(workspaceRoot, 'foo.ts') |
| 199 | + const result = tsTranspileModule(vol.readFileSync(filePath, 'utf-8').toString(), { |
| 200 | + fileName: filePath, |
| 201 | + compilerOptions: { |
| 202 | + module: ts.ModuleKind.ES2022, |
| 203 | + target: ts.ScriptTarget.ESNext, |
| 204 | + }, |
| 205 | + }) |
| 206 | + |
| 207 | + expect(omitLeadingWhitespace(result.outputText)).toContain(dedent` |
| 208 | + import { foo } from 'foo'; |
| 209 | + `) |
| 210 | + }) |
| 211 | + }) |
| 212 | + |
| 213 | + describe('with diagnostics', () => { |
| 214 | + const testFilePath = path.join(workspaceRoot, 'foo.ts') |
| 215 | + vol.fromJSON( |
| 216 | + { |
| 217 | + './foo.ts': ` |
| 218 | + import { foo } from 'foo'; |
| 219 | +
|
| 220 | + console.log(foo); |
| 221 | + `, |
| 222 | + }, |
| 223 | + workspaceRoot, |
| 224 | + ) |
| 225 | + |
| 226 | + it('should return diagnostics for invalid combination of compiler options', () => { |
| 227 | + const result = tsTranspileModule(vol.readFileSync(testFilePath, 'utf-8').toString(), { |
| 228 | + fileName: testFilePath, |
| 229 | + compilerOptions: { |
| 230 | + module: ts.ModuleKind.Node16, |
| 231 | + moduleResolution: ts.ModuleResolutionKind.Classic, |
| 232 | + }, |
| 233 | + }) |
| 234 | + |
| 235 | + expect(result.diagnostics?.[0].messageText).toBeTruthy() |
| 236 | + }) |
| 237 | + }) |
| 238 | +}) |
0 commit comments