Skip to content

Commit

Permalink
added a snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
GeeWee committed Jan 26, 2018
1 parent 97ed727 commit 432a3f8
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 29 deletions.
28 changes: 8 additions & 20 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as fs from 'fs';
import * as path from 'path';
import { JestConfig } from './jest-types';

/**
* Logger file that enables logging things just once. Does this by traversing the array of previously recorded
Expand All @@ -9,35 +8,24 @@ import { JestConfig } from './jest-types';
*/

const logs: any[] = [];
let loggingDisabled: boolean = true;
let logsFlushed: boolean = false;

export function enableLoggingIfNeeded(config: JestConfig) {
if (
config.globals &&
config.globals['ts-jest'] &&
config.globals['ts-jest'].debug
) {
loggingDisabled = false;
logOnce('Logging enabled');
}
function shouldLog(): boolean {
// If the env variable is set and the logs have not already been flushed, log the line
return process.env.TS_JEST_DEBUG && !logsFlushed;
}

// Log function. Only logs prior to calls to flushLogs.
export function logOnce(...thingsToLog: any[]) {
if (loggingDisabled) {
if (!shouldLog()) {
return;
}
thingsToLog.forEach(msg => {
if (includes(logs, msg)) {
return; // Message has already been logged
}
logs.push(msg);
});
logs.push(thingsToLog);
}

// This function JSONifies logs and flushes them to disk.
export function flushLogs() {
if (loggingDisabled || logsFlushed) {
if (!shouldLog()) {
return; // only output stuff for the first invocation and if logging is enabled.
}
logsFlushed = true;
Expand All @@ -56,6 +44,6 @@ function convertToJSONIfPossible(object: any): string {
try {
return JSON.stringify(object, null, 2);
} catch {
return object; // if unable to parse, simply return.
return object.toString(); // if unable to parse, simply return the string variant
}
}
3 changes: 1 addition & 2 deletions src/preprocessor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as crypto from 'crypto';
import * as tsc from 'typescript';
import { JestConfig, Path, TransformOptions } from './jest-types';
import { enableLoggingIfNeeded, flushLogs, logOnce } from './logger';
import { flushLogs, logOnce } from './logger';
import { getPostProcessHook } from './postprocess';
import {
cacheFile,
Expand All @@ -16,7 +16,6 @@ export function process(
jestConfig: JestConfig,
transformOptions: TransformOptions = { instrument: false },
) {
enableLoggingIfNeeded(jestConfig);
// transformOptions.instrument is a proxy for collectCoverage
// https://github.com/kulshekhar/ts-jest/issues/201#issuecomment-300572902
const compilerOptions = getTSConfig(
Expand Down
4 changes: 2 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as fsExtra from 'fs-extra';
import * as path from 'path';
import * as tsc from 'typescript';
import { JestConfig, TsJestConfig } from './jest-types';
import { logOnce } from './logger';

export function getTSJestConfig(globals: any): TsJestConfig {
return globals && globals['ts-jest'] ? globals['ts-jest'] : {};
Expand Down Expand Up @@ -121,8 +122,6 @@ export function getTSConfig(globals, collectCoverage: boolean = false) {
let configPath = getTSConfigPathFromConfig(globals);
const skipBabel = getTSJestConfig(globals).skipBabel;

logOnce('Original tsConfig before modifications: ', config);

// check cache before resolving configuration
// NB: We use JSON.stringify() to create a consistent, unique signature. Although it lacks a uniform
// shape, this is simpler and faster than using the crypto package to generate a hash signature.
Expand All @@ -136,6 +135,7 @@ export function getTSConfig(globals, collectCoverage: boolean = false) {
}

const config = readCompilerOptions(configPath);
logOnce('Original typescript config before modifications: ', config);

// ts-jest will map lines numbers properly if inlineSourceMap and
// inlineSources are set to true. For testing, we don't need the
Expand Down
3 changes: 2 additions & 1 deletion tests/__helpers__/runJest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const JEST_PATH = 'jest';
// return the result of the spawned proccess:
// [ 'status', 'signal', 'output', 'pid', 'stdout', 'stderr',
// 'envPairs', 'options', 'args', 'file' ]
export default function runJest(dir: string, args: string[]): Result {
export default function runJest(dir: string, args: string[], env = {}): Result {
const isRelative = dir[0] !== '/';

if (isRelative) {
Expand All @@ -30,6 +30,7 @@ export default function runJest(dir: string, args: string[]): Result {

const result = spawnSync(JEST_PATH, args || [], {
cwd: dir,
env,
});

// Call to string on byte arrays and strip ansi color codes for more accurate string comparison.
Expand Down
42 changes: 42 additions & 0 deletions tests/__tests__/__snapshots__/debug.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Debug output should create a debug file with the correct output if the flag is set 1`] = `
"[
\\"Original typescript config before modifications: \\",
{
\\"target\\": 1,
\\"module\\": 1,
\\"moduleResolution\\": 2,
\\"noEmitOnError\\": false,
\\"jsx\\": 2,
\\"allowJs\\": true,
\\"inlineSourceMap\\": true,
\\"inlineSources\\": true
}
]
[
\\"final compilerOptions:\\",
{
\\"target\\": 1,
\\"module\\": 1,
\\"moduleResolution\\": 2,
\\"noEmitOnError\\": false,
\\"jsx\\": 2,
\\"allowJs\\": true,
\\"inlineSourceMap\\": true,
\\"inlineSources\\": true
}
]
[
\\"tsJestConfig: \\",
{}
]
[
\\"Using babel with options:\\",
{
\\"babelrc\\": false,
\\"plugins\\": [],
\\"presets\\": []
}
]"
`;
6 changes: 5 additions & 1 deletion tests/__tests__/debug.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ import runJest from '../__helpers__/runJest';

describe('Debug output', () => {
it('should create a debug file with the correct output if the flag is set', async () => {
runJest('../debug', ['--no-cache', '-u']);
runJest('../debug', ['--no-cache', '-u'], {
TS_JEST_DEBUG: 'true',
});
const logFile = fs.readFileSync(
path.resolve(__dirname, '../debug/node_modules/ts-jest/debug.txt'),
'utf-8',
);

expect(logFile).not.toBeNull();
expect(logFile).toMatchSnapshot(); // Ensure we have some actual output that doesn't change
});

it('Should not create a file if the debug flag is not set', () => {
Expand Down
3 changes: 0 additions & 3 deletions tests/debug/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
"json"
],
"globals": {
"ts-jest": {
"debug": true
}
}
}
}

0 comments on commit 432a3f8

Please sign in to comment.