|
| 1 | +import { spawnSync } from 'node:child_process'; |
| 2 | +import { existsSync } from 'node:fs'; |
| 3 | +import { join } from 'node:path'; |
| 4 | +import { describe, expect, test } from '@rstest/core'; |
| 5 | +import { buildAndGetResults, createTempFiles, queryContent } from 'test-helper'; |
| 6 | + |
| 7 | +describe('dts with tsgo when build: true', () => { |
| 8 | + test('basic', async () => { |
| 9 | + const fixturePath = join(__dirname, 'build', 'basic'); |
| 10 | + const { files } = await buildAndGetResults({ |
| 11 | + fixturePath, |
| 12 | + type: 'dts', |
| 13 | + }); |
| 14 | + |
| 15 | + expect(files.esm).toMatchInlineSnapshot(` |
| 16 | + [ |
| 17 | + "<ROOT>/tests/integration/dts-tsgo/build/basic/dist/esm/index.d.ts", |
| 18 | + "<ROOT>/tests/integration/dts-tsgo/build/basic/dist/esm/sum.d.ts", |
| 19 | + ] |
| 20 | + `); |
| 21 | + |
| 22 | + const referenceDistPath = join( |
| 23 | + fixturePath, |
| 24 | + '../__references__/dist/index.d.ts', |
| 25 | + ); |
| 26 | + expect(existsSync(referenceDistPath)).toBeTruthy(); |
| 27 | + |
| 28 | + const buildInfoPath = join(fixturePath, 'tsconfig.tsbuildinfo'); |
| 29 | + expect(existsSync(buildInfoPath)).toBeTruthy(); |
| 30 | + }); |
| 31 | + |
| 32 | + test('distPath', async () => { |
| 33 | + const fixturePath = join(__dirname, 'build', 'dist-path'); |
| 34 | + const { files } = await buildAndGetResults({ |
| 35 | + fixturePath, |
| 36 | + type: 'dts', |
| 37 | + }); |
| 38 | + |
| 39 | + expect(files.esm).toMatchInlineSnapshot(` |
| 40 | + [ |
| 41 | + "<ROOT>/tests/integration/dts-tsgo/build/dist-path/dist/custom/index.d.ts", |
| 42 | + ] |
| 43 | + `); |
| 44 | + |
| 45 | + const buildInfoPath = join(fixturePath, 'tsconfig.tsbuildinfo'); |
| 46 | + expect(existsSync(buildInfoPath)).toBeTruthy(); |
| 47 | + }); |
| 48 | + |
| 49 | + test('autoExtension: true', async () => { |
| 50 | + const fixturePath = join(__dirname, 'build', 'auto-extension'); |
| 51 | + const { files } = await buildAndGetResults({ fixturePath, type: 'dts' }); |
| 52 | + |
| 53 | + expect(files.cjs).toMatchInlineSnapshot(` |
| 54 | + [ |
| 55 | + "<ROOT>/tests/integration/dts-tsgo/build/auto-extension/dist/types/index.d.cts", |
| 56 | + "<ROOT>/tests/integration/dts-tsgo/build/auto-extension/dist/types/sum.d.cts", |
| 57 | + ] |
| 58 | + `); |
| 59 | + |
| 60 | + const buildInfoPath = join(fixturePath, 'tsconfig.tsbuildinfo'); |
| 61 | + expect(existsSync(buildInfoPath)).toBeTruthy(); |
| 62 | + }); |
| 63 | + |
| 64 | + test('process files - auto extension and banner / footer', async () => { |
| 65 | + const fixturePath = join(__dirname, 'build', 'process-files'); |
| 66 | + const { contents } = await buildAndGetResults({ |
| 67 | + fixturePath, |
| 68 | + type: 'dts', |
| 69 | + }); |
| 70 | + |
| 71 | + expect(contents.esm).toMatchInlineSnapshot(` |
| 72 | + { |
| 73 | + "<ROOT>/tests/integration/dts-tsgo/build/process-files/dist/esm/index.d.mts": "/*! hello banner dts build*/ |
| 74 | + export declare const num1 = 1; |
| 75 | +
|
| 76 | + /*! hello banner dts build*/ |
| 77 | + ", |
| 78 | + } |
| 79 | + `); |
| 80 | + |
| 81 | + const buildInfoPath = join(fixturePath, 'tsconfig.tsbuildinfo'); |
| 82 | + expect(existsSync(buildInfoPath)).toBeTruthy(); |
| 83 | + }); |
| 84 | + |
| 85 | + test('abortOnError: false', async () => { |
| 86 | + const fixturePath = join(__dirname, 'build', 'abort-on-error'); |
| 87 | + |
| 88 | + const result = spawnSync('npx', ['rslib', 'build'], { |
| 89 | + cwd: fixturePath, |
| 90 | + // do not show output in test console |
| 91 | + stdio: 'ignore', |
| 92 | + shell: true, |
| 93 | + }); |
| 94 | + |
| 95 | + expect(result.status).toBe(0); |
| 96 | + |
| 97 | + const buildInfoPath = join(fixturePath, 'tsconfig.tsbuildinfo'); |
| 98 | + expect(existsSync(buildInfoPath)).toBeTruthy(); |
| 99 | + }); |
| 100 | + |
| 101 | + test('should clean dts dist files', async () => { |
| 102 | + const fixturePath = join(__dirname, 'build', 'clean'); |
| 103 | + |
| 104 | + const checkFiles = await createTempFiles(fixturePath, false); |
| 105 | + |
| 106 | + const { files } = await buildAndGetResults({ fixturePath, type: 'dts' }); |
| 107 | + |
| 108 | + for (const file of checkFiles) { |
| 109 | + expect(existsSync(file)).toBe(false); |
| 110 | + } |
| 111 | + |
| 112 | + expect(files.esm).toMatchInlineSnapshot(` |
| 113 | + [ |
| 114 | + "<ROOT>/tests/integration/dts-tsgo/build/clean/dist-types/esm/index.d.ts", |
| 115 | + "<ROOT>/tests/integration/dts-tsgo/build/clean/dist-types/esm/sum.d.ts", |
| 116 | + ] |
| 117 | + `); |
| 118 | + |
| 119 | + expect(files.cjs).toMatchInlineSnapshot(` |
| 120 | + [ |
| 121 | + "<ROOT>/tests/integration/dts-tsgo/build/clean/dist-types/cjs/index.d.ts", |
| 122 | + "<ROOT>/tests/integration/dts-tsgo/build/clean/dist-types/cjs/sum.d.ts", |
| 123 | + ] |
| 124 | + `); |
| 125 | + |
| 126 | + const referenceDistPath = join( |
| 127 | + fixturePath, |
| 128 | + '../__references__/dist/index.d.ts', |
| 129 | + ); |
| 130 | + expect(existsSync(referenceDistPath)).toBeTruthy(); |
| 131 | + |
| 132 | + const cjsBuildInfoPath = join(fixturePath, 'tsconfig.cjs.tsbuildinfo'); |
| 133 | + expect(existsSync(cjsBuildInfoPath)).toBeTruthy(); |
| 134 | + |
| 135 | + const esmBuildInfoPath = join(fixturePath, 'tsconfig.esm.tsbuildinfo'); |
| 136 | + expect(existsSync(esmBuildInfoPath)).toBeTruthy(); |
| 137 | + }); |
| 138 | + |
| 139 | + test('declarationMap', async () => { |
| 140 | + const fixturePath = join(__dirname, 'build', 'declaration-map'); |
| 141 | + const { files, contents } = await buildAndGetResults({ |
| 142 | + fixturePath, |
| 143 | + type: 'dts', |
| 144 | + }); |
| 145 | + |
| 146 | + expect(files.esm).toMatchInlineSnapshot(` |
| 147 | + [ |
| 148 | + "<ROOT>/tests/integration/dts-tsgo/build/declaration-map/dist/esm/index.d.ts", |
| 149 | + "<ROOT>/tests/integration/dts-tsgo/build/declaration-map/dist/esm/index.d.ts.map", |
| 150 | + ] |
| 151 | + `); |
| 152 | + |
| 153 | + expect(files.cjs).toMatchInlineSnapshot(` |
| 154 | + [ |
| 155 | + "<ROOT>/tests/integration/dts-tsgo/build/declaration-map/dist/cjs/index.d.cts", |
| 156 | + "<ROOT>/tests/integration/dts-tsgo/build/declaration-map/dist/cjs/index.d.cts.map", |
| 157 | + ] |
| 158 | + `); |
| 159 | + |
| 160 | + const { content: indexDtsEsm } = queryContent(contents.esm, 'index.d.ts', { |
| 161 | + basename: true, |
| 162 | + }); |
| 163 | + const { content: indexDtsCjs } = queryContent(contents.cjs, 'index.d.cts', { |
| 164 | + basename: true, |
| 165 | + }); |
| 166 | + const { content: indexMapEsm } = queryContent( |
| 167 | + contents.esm, |
| 168 | + 'index.d.ts.map', |
| 169 | + { |
| 170 | + basename: true, |
| 171 | + }, |
| 172 | + ); |
| 173 | + const { content: indexMapCjs } = queryContent( |
| 174 | + contents.cjs, |
| 175 | + 'index.d.cts.map', |
| 176 | + { |
| 177 | + basename: true, |
| 178 | + }, |
| 179 | + ); |
| 180 | + expect(indexDtsEsm).toContain('//# sourceMappingURL=index.d.ts.map'); |
| 181 | + expect(indexDtsCjs).toContain('//# sourceMappingURL=index.d.cts.map'); |
| 182 | + expect(indexMapEsm).toContain('"file":"index.d.ts"'); |
| 183 | + expect(indexMapCjs).toContain('"file":"index.d.cts"'); |
| 184 | + |
| 185 | + const referenceEsmDistPath = join( |
| 186 | + fixturePath, |
| 187 | + '../__references__/dist/index.d.ts', |
| 188 | + ); |
| 189 | + expect(existsSync(referenceEsmDistPath)).toBeTruthy(); |
| 190 | + |
| 191 | + // TODO: can not rename dts files in reference yet |
| 192 | + // const referenceCjsDistPath = join( |
| 193 | + // fixturePath, |
| 194 | + // '../__references__/dist/index.d.cts', |
| 195 | + // ); |
| 196 | + // expect(existsSync(referenceCjsDistPath)).toBeTruthy(); |
| 197 | + |
| 198 | + const esmBuildInfoPath = join(fixturePath, 'tsconfig.esm.tsbuildinfo'); |
| 199 | + const cjsBuildInfoPath = join(fixturePath, 'tsconfig.cjs.tsbuildinfo'); |
| 200 | + expect(existsSync(esmBuildInfoPath)).toBeTruthy(); |
| 201 | + expect(existsSync(cjsBuildInfoPath)).toBeTruthy(); |
| 202 | + }); |
| 203 | +}); |
0 commit comments