Skip to content

Commit

Permalink
feat(logging): improves log messages + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
huafu committed Aug 31, 2018
1 parent 4322701 commit 5d03c4d
Show file tree
Hide file tree
Showing 46 changed files with 657 additions and 619 deletions.
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@
- [Angular 2](#angular-2)
- [Tips](#tips)
- [Importing packages written in TypeScript](#importing-packages-written-in-typescript)
- [Logging](#logging)
- [Known Limitations](#known-limitations)
- [Known limitations for TS compiler options](#known-limitations-for-ts-compiler-options)
- [`const enum` is not supported](#const-enum-is-not-supported)
- [`const enum` is not supported if `typeCheck` is not enabled](#const-enum-is-not-supported-if-typecheck-is-not-enabled)
- [How to Contribute](#how-to-contribute)
- [Quickstart to run tests (only if you're working on this package)](#quickstart-to-run-tests-only-if-youre-working-on-this-package)
- [License](#license)
Expand Down Expand Up @@ -364,6 +365,20 @@ your Jest configuration:

By default Jest ignores everything in `node_modules`. This setting prevents Jest from ignoring the package you're interested in, in this case `@foo/bar`, while continuing to ignore everything else in `node_modules`.

### Logging

This package is using [`bs-logger`](https://www.npmjs.com/package/bs-logger).

Use environment variable `TS_JEST_LOG=xxx` to configure log targets. By default it'll log entries with level _warning_ and above to **stderr**.

See the examples in [there](https://github.com/huafu/bs-logger#using-targets) to configure different target(s).

When posting an issue, it's best to join the full log file which you can create in CWD using:
```sh
TS_JEST_LOG=ts-jest.log jest
# or
TS_JEST_LOG=ts-jest.log npm run test
```

## Known Limitations

Expand Down
2 changes: 1 addition & 1 deletion doc/tech/process/ts-jest.puml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ if (has afterProcess hook?) then (yes)
:call afterProcess hook;
-> update
source;
floating note left
note left
if the hook returns
something it'll be
used as new source
Expand Down
2 changes: 1 addition & 1 deletion e2e/__helpers__/test-case/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RunTestOptions } from './types'
import RunDescriptor from './RunDescriptor'
import RunDescriptor from './run-descriptor'

export function configureTestCase(
name: string,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ProcessedSource from '../../../src/__helpers__/ProcessedSource'
import ProcessedSource from '../../../src/__helpers__/processed-source'

// tslint:disable-next-line:no-default-export
export default class ProcessedFileIo extends ProcessedSource {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from './types'
import { join } from 'path'
import * as Paths from '../../../scripts/lib/paths'
import RunResult from './RunResult'
import RunResult from './run-result'
import { run } from './runtime'

// tslint:disable-next-line:no-default-export
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { SpawnSyncReturns } from 'child_process'
import ProcessedFileIo from './ProcessedFileIo'
import ProcessedFileIo from './processed-file-io'
import { stripAnsiColors, normalizeJestOutput, escapeRegex } from './utils'
import { resolve } from 'path'
import { readFileSync, realpathSync } from 'fs'
import { LOG_PREFIX } from '../../../src/util/debug'
import { tmpdir } from 'os'
import { LogMessage } from 'bs-logger'

// tslint:disable-next-line:no-default-export
export default class RunResult {
Expand All @@ -18,13 +18,13 @@ export default class RunResult {
env: { [key: string]: string },
}>,
) { }
get logFilePath() { return resolve(this.cwd, 'ts-jest-debug.log') }
get logFilePath() { return resolve(this.cwd, 'ts-jest.log') }
get logFileContent() { return readFileSync(this.logFilePath).toString('utf8') }
get normalizedLogFileContent() {
const prefix = ` ${LOG_PREFIX} `
return this.normalize(this.logFileContent.split(/\n/g).map(s => {
return s.split(prefix).slice(1).join(prefix)
}).join('\n'))
get logFileEntries(): LogMessage[] {
const lines = this.logFileContent.split(/\n/g)
// remove last, empty line
lines.pop()
return lines.map(s => JSON.parse(s))
}
get isPass() { return this.status === 0 }
get isFail() { return !this.isPass }
Expand All @@ -35,7 +35,7 @@ export default class RunResult {
get stdout() { return stripAnsiColors((this.result.stdout || '').toString()) }
get normalizedStdout() { return normalizeJestOutput(this.stdout) }
get cmdLine() {
return [this.context.cmd, ...this.context.args].join(' ')
return [this.context.cmd, ...this.context.args].filter(a => !['-u', '--updateSnapshot'].includes(a)).join(' ')
}

ioFor(relFilePath: string): ProcessedFileIo {
Expand Down
2 changes: 1 addition & 1 deletion e2e/__helpers__/test-case/runtime.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RunTestOptions, PreparedTest } from './types'
import RunResult from './RunResult'
import RunResult from './run-result'
import { templateNameForPath } from './utils'
import { join, relative, sep } from 'path'
import * as Paths from '../../../scripts/lib/paths'
Expand Down
2 changes: 1 addition & 1 deletion e2e/__helpers__/test-case/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TsJestConfig } from '../../../src/types'
import RunResult from './RunResult'
import RunResult from './run-result'

export interface RunTestOptions {
template?: string
Expand Down
2 changes: 1 addition & 1 deletion e2e/__serializers__/run-result.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import RunResult from '../__helpers__/test-case/RunResult'
import RunResult from '../__helpers__/test-case/run-result'

export const test = (val: any) => val && val instanceof RunResult
export const print = (val: RunResult, serialize: any, indent: any) => {
Expand Down
54 changes: 27 additions & 27 deletions e2e/__tests__/__snapshots__/hoisting.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,24 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Hoisting jest.mock() & jest.unmock() should pass using template "default" 1`] = `
√ jest --runInBand
↳ exit code: 0
===[ STDOUT ]===================================================================
===[ STDERR ]===================================================================
PASS ./hello.spec.ts
hello
√ should have been mocked
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: XXs
Ran all test suites.
================================================================================
`;

exports[`Hoisting jest.mock() & jest.unmock() should pass using template "default" 2`] = `
exports[`Hoisting jest.mock() & jest.unmock() should pass using template "default": io 1`] = `
===[ FILE: hello.spec.ts ]======================================================
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
Expand Down Expand Up @@ -79,7 +61,7 @@ exports[`Hoisting jest.mock() & jest.unmock() should pass using template "defaul
================================================================================
`;
exports[`Hoisting jest.mock() & jest.unmock() should pass using template "with-babel-6" 1`] = `
exports[`Hoisting jest.mock() & jest.unmock() should pass using template "default": output 1`] = `
√ jest --runInBand
↳ exit code: 0
===[ STDOUT ]===================================================================
Expand All @@ -97,7 +79,7 @@ exports[`Hoisting jest.mock() & jest.unmock() should pass using template "with-b
================================================================================
`;
exports[`Hoisting jest.mock() & jest.unmock() should pass using template "with-babel-6" 2`] = `
exports[`Hoisting jest.mock() & jest.unmock() should pass using template "with-babel-6": io 1`] = `
===[ FILE: hello.spec.ts ]======================================================
"use strict";
Expand Down Expand Up @@ -159,7 +141,7 @@ exports[`Hoisting jest.mock() & jest.unmock() should pass using template "with-b
================================================================================
`;
exports[`Hoisting jest.mock() & jest.unmock() should pass using template "with-babel-7" 1`] = `
exports[`Hoisting jest.mock() & jest.unmock() should pass using template "with-babel-6": output 1`] = `
√ jest --runInBand
↳ exit code: 0
===[ STDOUT ]===================================================================
Expand All @@ -177,7 +159,7 @@ exports[`Hoisting jest.mock() & jest.unmock() should pass using template "with-b
================================================================================
`;
exports[`Hoisting jest.mock() & jest.unmock() should pass using template "with-babel-7" 2`] = `
exports[`Hoisting jest.mock() & jest.unmock() should pass using template "with-babel-7": io 1`] = `
===[ FILE: hello.spec.ts ]======================================================
"use strict";
Expand Down Expand Up @@ -251,7 +233,7 @@ exports[`Hoisting jest.mock() & jest.unmock() should pass using template "with-b
================================================================================
`;
exports[`Hoisting jest.mock() & jest.unmock() should pass using template "with-jest-22" 1`] = `
exports[`Hoisting jest.mock() & jest.unmock() should pass using template "with-babel-7": output 1`] = `
√ jest --runInBand
↳ exit code: 0
===[ STDOUT ]===================================================================
Expand All @@ -269,7 +251,7 @@ exports[`Hoisting jest.mock() & jest.unmock() should pass using template "with-j
================================================================================
`;
exports[`Hoisting jest.mock() & jest.unmock() should pass using template "with-jest-22" 2`] = `
exports[`Hoisting jest.mock() & jest.unmock() should pass using template "with-jest-22": io 1`] = `
===[ FILE: hello.spec.ts ]======================================================
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
Expand Down Expand Up @@ -330,7 +312,7 @@ exports[`Hoisting jest.mock() & jest.unmock() should pass using template "with-j
================================================================================
`;
exports[`Hoisting jest.mock() & jest.unmock() should pass using template "with-typescript-2-7" 1`] = `
exports[`Hoisting jest.mock() & jest.unmock() should pass using template "with-jest-22": output 1`] = `
√ jest --runInBand
↳ exit code: 0
===[ STDOUT ]===================================================================
Expand All @@ -348,7 +330,7 @@ exports[`Hoisting jest.mock() & jest.unmock() should pass using template "with-t
================================================================================
`;
exports[`Hoisting jest.mock() & jest.unmock() should pass using template "with-typescript-2-7" 2`] = `
exports[`Hoisting jest.mock() & jest.unmock() should pass using template "with-typescript-2-7": io 1`] = `
===[ FILE: hello.spec.ts ]======================================================
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
Expand Down Expand Up @@ -408,3 +390,21 @@ exports[`Hoisting jest.mock() & jest.unmock() should pass using template "with-t
version: 3
================================================================================
`;
exports[`Hoisting jest.mock() & jest.unmock() should pass using template "with-typescript-2-7": output 1`] = `
√ jest --runInBand
↳ exit code: 0
===[ STDOUT ]===================================================================
===[ STDERR ]===================================================================
PASS ./hello.spec.ts
hello
√ should have been mocked
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: XXs
Ran all test suites.
================================================================================
`;
Loading

0 comments on commit 5d03c4d

Please sign in to comment.