|
| 1 | +const ts = require('typescript'); |
| 2 | +const path = require('path'); |
| 3 | + |
| 4 | +function compile(...filenames) { |
| 5 | + const program = ts.createProgram(filenames, { |
| 6 | + allowSyntheticDefaultImports: true, |
| 7 | + jsx: 'react', |
| 8 | + noEmit: true, |
| 9 | + skipLibCheck: true, |
| 10 | + }); |
| 11 | + |
| 12 | + const emitResult = program.emit(); |
| 13 | + const allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics); |
| 14 | + const errors = []; |
| 15 | + |
| 16 | + allDiagnostics.forEach(diagnostic => { |
| 17 | + if (diagnostic.file) { |
| 18 | + const { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start); |
| 19 | + const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n'); |
| 20 | + errors.push(`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`); |
| 21 | + } else { |
| 22 | + errors.push(ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n')); |
| 23 | + } |
| 24 | + }); |
| 25 | + |
| 26 | + return errors; |
| 27 | +}; |
| 28 | + |
| 29 | + |
| 30 | +it('should pass dir as string', () => { |
| 31 | + const dirStringErrors = compile(path.join(__dirname, './__typescript__/dirString.tsx')); |
| 32 | + |
| 33 | + expect(dirStringErrors).toHaveProperty('length', 0); |
| 34 | +}); |
| 35 | + |
| 36 | +it('should fail on dir as number', () => { |
| 37 | + const dirNumErrors = compile(path.join(__dirname, './__typescript__/dirNumber.tsx')); |
| 38 | + |
| 39 | + expect(dirNumErrors).toHaveProperty('length', 1); |
| 40 | + expect(dirNumErrors[0]).toEqual(expect.stringContaining(`Type 'number' is not assignable to type '"ltr" | "rtl" | "auto"'`)); |
| 41 | +}); |
| 42 | + |
| 43 | +it('should fail on accent', () => { |
| 44 | + const accentErrors = compile(path.join(__dirname, './__typescript__/styleOptionsAccent.tsx')); |
| 45 | + |
| 46 | + expect(accentErrors).toHaveProperty('length', 0); |
| 47 | +}); |
| 48 | + |
| 49 | +it('should pass on cardEmphasisBackgroundColor', () => { |
| 50 | + const cardEmphErrors = compile(path.join(__dirname, './__typescript__/styleOptionsCardEmph.tsx')); |
| 51 | + |
| 52 | + expect(cardEmphErrors).toHaveProperty('length', 1); |
| 53 | + expect(cardEmphErrors[0]).toEqual(expect.stringContaining(`Type '{ cardEmphasisBackgroundColor: string; }' is not assignable to type 'StyleOptions'`)); |
| 54 | +}); |
0 commit comments