|
| 1 | +import ts from 'typescript'; |
| 2 | +import { describe, expect, test, vi } from 'vitest'; |
| 3 | +import { testMocksDir } from '../../__tests__/fixtures.js'; |
| 4 | +import { runTsc } from '../tsc.js'; |
| 5 | + |
| 6 | +const mockedWriteFile = vi.hoisted(() => vi.fn<(arg0: string, arg1: string) => void>(() => undefined)); |
| 7 | + |
| 8 | +vi.mock(import('typescript'), async importOriginal => { |
| 9 | + const { default: original } = await importOriginal(); |
| 10 | + |
| 11 | + // @ts-expect-error createProgram has two overloads but we can only really define 1 |
| 12 | + const createProgram: typeof original.createProgram = vi.fn((opts: ts.CreateProgramOptions) => { |
| 13 | + const program = original.createProgram(opts); |
| 14 | + const emit: typeof program.emit = (sourceFile, _, cancelToken, emitDts, transformers) => { |
| 15 | + return program.emit(sourceFile, mockedWriteFile, cancelToken, emitDts, transformers); |
| 16 | + }; |
| 17 | + |
| 18 | + return { |
| 19 | + ...program, |
| 20 | + emit |
| 21 | + }; |
| 22 | + }); |
| 23 | + |
| 24 | + return { |
| 25 | + default: { |
| 26 | + ...original, |
| 27 | + createProgram, |
| 28 | + } |
| 29 | + }; |
| 30 | +}); |
| 31 | + |
| 32 | +describe('Test the augmented tsc functionality', () => { |
| 33 | + test('tsc on a bundle', async () => { |
| 34 | + await runTsc({ |
| 35 | + type: 'bundle', |
| 36 | + directory: `${testMocksDir}/bundles/test0`, |
| 37 | + name: 'test0', |
| 38 | + manifest: {} |
| 39 | + }, false); |
| 40 | + |
| 41 | + expect(ts.createProgram).toHaveBeenCalledTimes(2); |
| 42 | + expect(mockedWriteFile).toHaveBeenCalledTimes(1); |
| 43 | + const [[writePath]] = mockedWriteFile.mock.calls; |
| 44 | + |
| 45 | + expect(writePath).not.toEqual(`${testMocksDir}/bundles/test0/__tests__/test0.test.js`,); |
| 46 | + }); |
| 47 | + |
| 48 | + test('tsc on a bundle with --noEmit', async () => { |
| 49 | + await runTsc({ |
| 50 | + type: 'bundle', |
| 51 | + directory: `${testMocksDir}/bundles/test0`, |
| 52 | + name: 'test0', |
| 53 | + manifest: {} |
| 54 | + }, true); |
| 55 | + |
| 56 | + expect(ts.createProgram).toHaveBeenCalledTimes(1); |
| 57 | + expect(mockedWriteFile).not.toBeCalled(); |
| 58 | + }); |
| 59 | + |
| 60 | + test('tsc on a bundle with noEmit in its tsconfig', async () => { |
| 61 | + const originalTs: typeof ts = await vi.importActual('typescript'); |
| 62 | + |
| 63 | + vi.spyOn(ts, 'parseJsonConfigFileContent').mockImplementationOnce((...args) => { |
| 64 | + const rawResult = originalTs.parseJsonConfigFileContent(...args); |
| 65 | + rawResult.options.noEmit = true; |
| 66 | + return rawResult; |
| 67 | + }); |
| 68 | + |
| 69 | + await runTsc({ |
| 70 | + type: 'bundle', |
| 71 | + directory: `${testMocksDir}/bundles/test0`, |
| 72 | + name: 'test0', |
| 73 | + manifest: {} |
| 74 | + }, false); |
| 75 | + |
| 76 | + expect(ts.createProgram).toHaveBeenCalledTimes(1); |
| 77 | + expect(mockedWriteFile).not.toBeCalled(); |
| 78 | + }); |
| 79 | + |
| 80 | + test('tsc on a tab', async () => { |
| 81 | + await runTsc({ |
| 82 | + type: 'tab', |
| 83 | + directory: `${testMocksDir}/tabs/tab0`, |
| 84 | + entryPoint: `${testMocksDir}/tabs/tab0/src/index.tsx`, |
| 85 | + name: 'tab0' |
| 86 | + }, false); |
| 87 | + |
| 88 | + expect(ts.createProgram).toHaveBeenCalledTimes(1); |
| 89 | + expect(mockedWriteFile).not.toBeCalled(); |
| 90 | + }); |
| 91 | +}); |
0 commit comments