|  | 
|  | 1 | +const { expect } = require('chai') | 
|  | 2 | +const sinon = require('sinon') | 
|  | 3 | +const proxyquire = require('proxyquire').noPreserveCache() | 
|  | 4 | + | 
|  | 5 | +type Typescript = { | 
|  | 6 | +  createProgram: sinon.SinonStub | 
|  | 7 | +} | 
|  | 8 | + | 
|  | 9 | +let typescript: Typescript | 
|  | 10 | +let createProgram: Typescript['createProgram'] | 
|  | 11 | + | 
|  | 12 | +import '../../lib/typescript-overrides' | 
|  | 13 | + | 
|  | 14 | +describe('./lib/typescript-overrides', () => { | 
|  | 15 | +  beforeEach(() => { | 
|  | 16 | +    createProgram = sinon.stub() | 
|  | 17 | +    typescript = { | 
|  | 18 | +      createProgram, | 
|  | 19 | +    } | 
|  | 20 | +  }) | 
|  | 21 | + | 
|  | 22 | +  context('.getSourceMapOverride', () => { | 
|  | 23 | +    it('is null by default', () => { | 
|  | 24 | +      const typescriptOverrides = proxyquire('../../lib/typescript-overrides', { | 
|  | 25 | +        typescript, | 
|  | 26 | +      }) | 
|  | 27 | + | 
|  | 28 | +      expect(typescriptOverrides.getSourceMapOverride()).to.be.null | 
|  | 29 | +    }) | 
|  | 30 | +  }) | 
|  | 31 | + | 
|  | 32 | +  context('.tryRequireTypescript', () => { | 
|  | 33 | +    it('gracefully returns error when typescript cannot be required', () => { | 
|  | 34 | +      const typescriptOverrides = proxyquire('../../lib/typescript-overrides', { | 
|  | 35 | +        typescript: null, | 
|  | 36 | +      }) | 
|  | 37 | + | 
|  | 38 | +      const err = typescriptOverrides.tryRequireTypescript() | 
|  | 39 | + | 
|  | 40 | +      expect(err).to.be.instanceOf(Error) | 
|  | 41 | +      expect(err.message).to.eq(`Cannot find module 'typescript'`) | 
|  | 42 | +    }) | 
|  | 43 | +  }) | 
|  | 44 | + | 
|  | 45 | +  context('.overrideSourceMaps', () => { | 
|  | 46 | +    it('it sets sourceMap: true', () => { | 
|  | 47 | +      const typescriptOverrides = proxyquire('../../lib/typescript-overrides', { | 
|  | 48 | +        typescript, | 
|  | 49 | +      }) | 
|  | 50 | + | 
|  | 51 | +      typescriptOverrides.overrideSourceMaps(true) | 
|  | 52 | + | 
|  | 53 | +      expect(typescriptOverrides.getSourceMapOverride()).to.be.true | 
|  | 54 | + | 
|  | 55 | +      typescript.createProgram({ | 
|  | 56 | +        options: { | 
|  | 57 | +          sourceMap: false, | 
|  | 58 | +          inlineSources: true, | 
|  | 59 | +          inlineSourceMap: true, | 
|  | 60 | +        }, | 
|  | 61 | +      }) | 
|  | 62 | + | 
|  | 63 | +      expect(createProgram).to.be.calledOn(typescript) | 
|  | 64 | +      expect(createProgram).to.be.calledWith({ | 
|  | 65 | +        options: { | 
|  | 66 | +          sourceMap: true, | 
|  | 67 | +        }, | 
|  | 68 | +      }) | 
|  | 69 | +    }) | 
|  | 70 | + | 
|  | 71 | +    it('it sets sourceMap: false', () => { | 
|  | 72 | +      const typescriptOverrides = proxyquire('../../lib/typescript-overrides', { | 
|  | 73 | +        typescript, | 
|  | 74 | +      }) | 
|  | 75 | + | 
|  | 76 | +      typescriptOverrides.overrideSourceMaps(false) | 
|  | 77 | + | 
|  | 78 | +      expect(typescriptOverrides.getSourceMapOverride()).to.be.false | 
|  | 79 | + | 
|  | 80 | +      typescript.createProgram({ | 
|  | 81 | +        options: { | 
|  | 82 | +          sourceMap: true, | 
|  | 83 | +          inlineSources: true, | 
|  | 84 | +          inlineSourceMap: true, | 
|  | 85 | +        }, | 
|  | 86 | +      }) | 
|  | 87 | + | 
|  | 88 | +      expect(createProgram).to.be.calledOn(typescript) | 
|  | 89 | +      expect(createProgram).to.be.calledWith({ | 
|  | 90 | +        options: { | 
|  | 91 | +          sourceMap: false, | 
|  | 92 | +        }, | 
|  | 93 | +      }) | 
|  | 94 | +    }) | 
|  | 95 | + | 
|  | 96 | +    it('does not override sourcemaps', () => { | 
|  | 97 | +      const typescriptOverrides = proxyquire('../../lib/typescript-overrides', { | 
|  | 98 | +        typescript, | 
|  | 99 | +      }) | 
|  | 100 | + | 
|  | 101 | +      expect(typescriptOverrides.getSourceMapOverride()).to.be.null | 
|  | 102 | + | 
|  | 103 | +      typescript.createProgram({ | 
|  | 104 | +        options: { | 
|  | 105 | +          sourceMap: true, | 
|  | 106 | +          inlineSources: true, | 
|  | 107 | +          inlineSourceMap: true, | 
|  | 108 | +        }, | 
|  | 109 | +      }) | 
|  | 110 | + | 
|  | 111 | +      expect(createProgram).to.be.calledOn(typescript) | 
|  | 112 | +      expect(createProgram).to.be.calledWith({ | 
|  | 113 | +        options: { | 
|  | 114 | +          sourceMap: true, | 
|  | 115 | +          inlineSources: true, | 
|  | 116 | +          inlineSourceMap: true, | 
|  | 117 | +        }, | 
|  | 118 | +      }) | 
|  | 119 | +    }) | 
|  | 120 | +  }) | 
|  | 121 | +}) | 
0 commit comments