UNPKG

aws-cdk-lib

Version:

Version 2 of the AWS Cloud Development Kit library

4 lines (3 loc) 2.17 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var jest_environment_node_1=()=>{var tmp=require("jest-environment-node");return jest_environment_node_1=()=>tmp,tmp};class TestEnvironment extends jest_environment_node_1().TestEnvironment{constructor(config,context){super(config,context),this.log=new Array,this.handleTestEvent=async(event,_state)=>{if(event.name==="test_done"&&event.test.errors.length>0&&this.log.length>0){this.stopCapture(),this.originalConsole.log(`[Console output] ${fullTestName(event.test)} `);for(const item of this.log)this.originalConsole[item.type].apply(this.originalConsole,[" ",...item.args]);this.originalConsole.log(` `),this.startCapture()}event.name==="test_done"&&(this.log=[])}}async setup(){await super.setup(),this.log=[],this.startCapture()}async teardown(){this.stopCapture(),await super.teardown()}startCapture(){this.originalConsole=console,this.originalStdoutWrite=process.stdout.write,this.originalStderrWrite=process.stderr.write,this.global.console={...console,log:(...args)=>this.log.push({type:"log",args}),error:(...args)=>this.log.push({type:"error",args}),warn:(...args)=>this.log.push({type:"warn",args}),info:(...args)=>this.log.push({type:"info",args}),debug:(...args)=>this.log.push({type:"debug",args})};const self=this;process.stdout.write=function(chunk,enccb){const encoding=typeof enccb=="string"?enccb:"utf-8",message=Buffer.isBuffer(chunk)?chunk.toString(encoding):chunk;self.log.push({type:"log",args:[message.replace(/\n$/,"")]}),typeof enccb=="function"&&enccb()},process.stderr.write=function(chunk,enccb){const encoding=typeof enccb=="string"?enccb:"utf-8",message=Buffer.isBuffer(chunk)?chunk.toString(encoding):chunk;self.log.push({type:"error",args:[message.replace(/\n$/,"")]}),typeof enccb=="function"&&enccb()}}stopCapture(){this.global.console=this.originalConsole,process.stdout.write=this.originalStdoutWrite,process.stderr.write=this.originalStderrWrite}}exports.default=TestEnvironment;function fullTestName(test){let ret=test.name;for(;test.parent!=null&&test.parent.name!=="ROOT_DESCRIBE_BLOCK";)ret=test.parent.name+" \u203A "+ret,test=test.parent;return ret}