11// tslint:disable-file:no-shadowed-variable
22import { sync as spawnSync } from 'cross-spawn'
3- import { join , relative , sep } from 'path'
3+ import { join , relative , sep , isAbsolute } from 'path'
44import * as Paths from '../../scripts/lib/paths'
55import * as fs from 'fs-extra'
66import { RawSourceMap } from 'source-map'
7- import { relativiseSourceRoot , extractSourceMaps } from './source-maps'
7+ import { extractSourceMaps } from './source-maps'
88import { SpawnSyncReturns } from 'child_process'
99import merge from 'lodash.merge'
1010import { TsJestConfig } from '../../src/lib/types'
11+ import { rewriteSourceMaps , relativisePaths } from '../../src/__helpers__/source-maps'
1112
1213const TEMPLATE_EXCLUDED_ITEMS = [ 'node_modules' , 'package-lock.json' ]
1314
@@ -144,35 +145,57 @@ export class TestRunResult {
144145 constructor (
145146 readonly cwd : string ,
146147 readonly result : SpawnSyncReturns < Buffer > ,
147- readonly ioDir ?: string ,
148+ readonly context : Readonly < {
149+ ioDir ?: string | undefined ,
150+ cmd : string ,
151+ args : string [ ] ,
152+ env : { [ key : string ] : string } ,
153+ } > ,
148154 ) { }
149155 get isPass ( ) { return this . status === 0 }
150156 get isFail ( ) { return ! this . isPass }
151157 get status ( ) { return this . result . status }
152158 get output ( ) { return stripAnsiColors ( ( this . result . output ? this . result . output . join ( '\n\n' ) : '' ) ) }
153159 get stderr ( ) { return stripAnsiColors ( ( this . result . stderr || '' ) . toString ( ) ) }
154160 get stdout ( ) { return stripAnsiColors ( ( this . result . stdout || '' ) . toString ( ) ) }
161+ get cmdLine ( ) {
162+ return [ this . context . cmd , ...this . context . args ] . join ( ' ' )
163+ }
155164 ioFor ( relFilePath : string ) : TestFileIo {
156- if ( ! this . ioDir ) {
165+ if ( ! this . context . ioDir ) {
157166 throw new Error ( 'IO not written for test, you must configure the test with `writeIo: true`.' )
158167 }
159- const io = require ( `${ this . ioDir } /${ relFilePath } .json` )
160- return new TestFileIo ( this . cwd , io . in , io . out )
168+ const io = require ( `${ this . context . ioDir } /${ relFilePath } .json` )
169+ return new TestFileIo ( this . cwd , relFilePath , io . in , io . out )
161170 }
162171}
163172
164173// tslint:disable-next-line:max-classes-per-file
165- class TestFileIo {
174+ export class TestFileIo {
166175 constructor (
167176 private _cwd : string ,
177+ readonly filename : string ,
168178 readonly input : [ string , jest . Path , jest . ProjectConfig , jest . TransformOptions ?] ,
169179 readonly output : string | jest . TransformedSource ,
170180 ) { }
171181 get inputCode ( ) : string { return this . input [ 0 ] }
172182 get inputPath ( ) : string { return this . input [ 1 ] }
173183 get outputCode ( ) : string { return typeof this . output === 'object' ? this . output . code : this . output }
174184 get outputSourceMaps ( ) : RawSourceMap | undefined { return extractSourceMaps ( this . outputCode ) }
175- get normalizedOutputCode ( ) : string { return relativiseSourceRoot ( this . _cwd , this . outputCode , '<cwd>/' ) }
185+ get normalizedOutputCode ( ) : string {
186+ return rewriteSourceMaps (
187+ this . outputCode ,
188+ this . sourceMapsNormalizer ,
189+ )
190+ }
191+ get normalizedOutputSourceMaps ( ) : RawSourceMap | undefined {
192+ const maps = this . outputSourceMaps
193+ if ( maps ) return this . sourceMapsNormalizer ( maps )
194+ return
195+ }
196+ get sourceMapsNormalizer ( ) {
197+ return ( maps : RawSourceMap ) : RawSourceMap => relativisePaths ( maps , this . _cwd , '<cwd>/' )
198+ }
176199}
177200
178201// tslint:disable-next-line:interface-over-type-literal
@@ -240,6 +263,7 @@ export function run(name: string, options: RunTestOptions = {}): TestRunResult {
240263 )
241264 const pkg = require ( join ( dir , 'package.json' ) )
242265
266+ let shortCmd : string
243267 let cmdArgs : string [ ] = [ ]
244268 if ( inject ) {
245269 cmdArgs . push ( '--testPathPattern="/__eval\\\\.ts$"' )
@@ -253,8 +277,10 @@ export function run(name: string, options: RunTestOptions = {}): TestRunResult {
253277 cmdArgs . unshift ( '--' )
254278 }
255279 cmdArgs = [ 'npm' , '-s' , 'run' , 'test' , ...cmdArgs ]
280+ shortCmd = 'npm'
256281 } else {
257282 cmdArgs . unshift ( join ( dir , 'node_modules' , '.bin' , 'jest' ) )
283+ shortCmd = 'jest'
258284 }
259285
260286 // merge given config extend
@@ -316,7 +342,12 @@ export function run(name: string, options: RunTestOptions = {}): TestRunResult {
316342 } )
317343 } )
318344
319- return new TestRunResult ( fs . realpathSync ( dir ) , result , writeIo ? ioDir : undefined )
345+ return new TestRunResult ( fs . realpathSync ( dir ) , result , {
346+ cmd : shortCmd ,
347+ args : cmdArgs ,
348+ env : mergedEnv ,
349+ ioDir : writeIo ? ioDir : undefined ,
350+ } )
320351}
321352
322353// from https://stackoverflow.com/questions/25245716/remove-all-ansi-colors-styles-from-strings
0 commit comments