Skip to content

Commit 4322701

Browse files
committed
feat: adds logging + better hashing
1 parent a5b5852 commit 4322701

23 files changed

Lines changed: 496 additions & 178 deletions

.github/ISSUE_TEMPLATE.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ https://github.com/kulshekhar/ts-jest/wiki/Troubleshooting -->
77
## Expected behavior <!-- describe the expected behavior below -->:
88

99

10-
## Output from debug log <!-- You can activate the debug logger by setting the environment variable TS_JEST_DEBUG="true" before running yarn test. The output of the logger will be in **<your_project_dir>/node_modules/ts-jest/debug.txt**, paste it below -->:
10+
## Output from debug log <!-- You can activate the debug logger by setting the environment variable TS_JEST_DEBUG="true" before running tests. The output of the logger will be in **ts-jest-debug.log**, paste it below -->:
1111
```bash
12-
# content of debug.txt :
12+
# content of ts-jest-debug.log :
1313

1414
```
1515

e2e/__helpers__/test-case/RunResult.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { SpawnSyncReturns } from 'child_process'
22
import ProcessedFileIo from './ProcessedFileIo'
3-
import { stripAnsiColors, normalizeJestOutput } from './utils'
3+
import { stripAnsiColors, normalizeJestOutput, escapeRegex } from './utils'
4+
import { resolve } from 'path'
5+
import { readFileSync, realpathSync } from 'fs'
6+
import { LOG_PREFIX } from '../../../src/util/debug'
7+
import { tmpdir } from 'os'
48

59
// tslint:disable-next-line:no-default-export
610
export default class RunResult {
@@ -14,6 +18,14 @@ export default class RunResult {
1418
env: { [key: string]: string },
1519
}>,
1620
) { }
21+
get logFilePath() { return resolve(this.cwd, 'ts-jest-debug.log') }
22+
get logFileContent() { return readFileSync(this.logFilePath).toString('utf8') }
23+
get normalizedLogFileContent() {
24+
const prefix = ` ${LOG_PREFIX} `
25+
return this.normalize(this.logFileContent.split(/\n/g).map(s => {
26+
return s.split(prefix).slice(1).join(prefix)
27+
}).join('\n'))
28+
}
1729
get isPass() { return this.status === 0 }
1830
get isFail() { return !this.isPass }
1931
get status() { return this.result.status }
@@ -25,11 +37,27 @@ export default class RunResult {
2537
get cmdLine() {
2638
return [this.context.cmd, ...this.context.args].join(' ')
2739
}
40+
2841
ioFor(relFilePath: string): ProcessedFileIo {
2942
if (!this.context.ioDir) {
3043
throw new Error('IO not written for test, you must configure the test with `writeIo: true`.')
3144
}
3245
const io = require(`${this.context.ioDir}/${relFilePath}.json`)
3346
return new ProcessedFileIo(this.cwd, relFilePath, io.in, io.out)
3447
}
48+
49+
normalize(str: string) {
50+
// TODO: hmmm clean this!
51+
return [
52+
{ from: this.cwd, to: '<cwd>' },
53+
{ from: realpathSync(this.cwd), to: '<cwd>' },
54+
{ from: tmpdir(), to: '<tmp>' },
55+
{ from: realpathSync(tmpdir()), to: '<tmp>' },
56+
{ from: /\b[a-f0-9]{40}\b/g, to: '<hex:40>' },
57+
]
58+
.sort((a, b) => ((b.from as any).length || 0) - ((a.from as any).length || 0))
59+
.reduce((str, { from, to }) => {
60+
return str.replace(typeof from === 'string' ? new RegExp(`${escapeRegex(from)}`, 'g') : from, to)
61+
}, str)
62+
}
3563
}

e2e/__helpers__/test-case/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,7 @@ export function normalizeJestOutput(output: string): string {
5252
}
5353
return out
5454
}
55+
56+
export function escapeRegex(s: string) {
57+
return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')
58+
}

e2e/__tests__/__snapshots__/logger.test.ts.snap

Lines changed: 139 additions & 0 deletions
Large diffs are not rendered by default.

e2e/__tests__/__snapshots__/warning-messages.test.ts.snap

Lines changed: 0 additions & 20 deletions
This file was deleted.

e2e/__tests__/logger.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { configureTestCase } from '../__helpers__/test-case'
2+
import { PackageSets, allValidPackageSets } from '../__helpers__/templates'
3+
import { existsSync } from 'fs'
4+
5+
describe('With unsupported version test', () => {
6+
const testCase = configureTestCase('simple')
7+
8+
testCase.runWithTemplates([PackageSets.unsupportedVersion], 0, (runTest, { testLabel }) => {
9+
it(testLabel, () => {
10+
const result = runTest()
11+
expect(result.status).toBe(0)
12+
expect(result).toMatchSnapshot()
13+
})
14+
})
15+
})
16+
17+
describe('TS_JEST_DEBUG', () => {
18+
const testCase = configureTestCase('simple', { env: { TS_JEST_DEBUG: 'true' } })
19+
20+
testCase.runWithTemplates(allValidPackageSets, 0, (runTest, { templateName }) => {
21+
it(`should pass and create log file when using tempalte "${templateName}"`, () => {
22+
const result = runTest()
23+
expect(result.status).toBe(0)
24+
expect(existsSync(result.logFilePath))
25+
expect(result.normalizedLogFileContent).toMatchSnapshot()
26+
})
27+
})
28+
})

e2e/__tests__/warning-messages.test.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

package-lock.json

Lines changed: 1 addition & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@
8181
"lodash.merge": "^4.6.1",
8282
"lodash.set": "^4.3.2",
8383
"npm-run-all": "^4.1.3",
84-
"outdent": "^0.5.0",
8584
"prettier": "^1.14.2",
8685
"reflect-metadata": "^0.1.12",
8786
"source-map": "^0.7.3",

src/__helpers__/snapshots.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)