Skip to content

Commit a706c3b

Browse files
abetomoDeviaVir
authored andcommitted
Add invalidation of log output to make the test result easier to read (#367)
* Add function to enable / disable console.[log, warn, info] * Remove meaningless test * Add invalidation of log output to make the test result easier to read
1 parent 5149afd commit a706c3b

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

test/main.js

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,18 @@ const _awsRestore = () => {
113113
awsMock.restore('Lambda')
114114
}
115115

116+
const disableLog = () => {
117+
['log', 'warn', 'info'].forEach((f) => {
118+
console[f] = () => {}
119+
})
120+
}
121+
122+
const enableLog = () => {
123+
['log', 'warn', 'info'].forEach((f) => {
124+
if (String(console[f]) === '() => {}') delete console[f]
125+
})
126+
}
127+
116128
/* global before, after, beforeEach, afterEach, describe, it */
117129
describe('lib/main', function () {
118130
if (process.platform === 'win32') {
@@ -508,16 +520,18 @@ describe('lib/main', function () {
508520
})
509521

510522
it('should not throw any errors if no script', () => {
523+
disableLog()
511524
return lambda._postInstallScript(program, codeDirectory).then((dummy) => {
525+
enableLog()
512526
assert.isUndefined(dummy)
513527
})
514528
})
515529

516530
it('should throw any errors if script fails', () => {
517531
fs.writeFileSync(postInstallScriptPath, '___fails___')
518-
return lambda._postInstallScript(program, codeDirectory).then((dummy) => {
519-
assert.isUndefined(dummy)
520-
}).catch((err) => {
532+
disableLog()
533+
return lambda._postInstallScript(program, codeDirectory).catch((err) => {
534+
enableLog()
521535
assert.instanceOf(err, Error)
522536
assert.match(err.message, /^Error: Command failed:/)
523537
})
@@ -563,7 +577,9 @@ describe('lib/main', function () {
563577
it('Compress the file. `index.js` and `bin/node-lambda` are included and the permission is also preserved.', function () {
564578
_timeout({ this: this, sec: 30 }) // give it time to zip
565579

580+
disableLog()
566581
return lambda._zip(program, codeDirectory).then((data) => {
582+
enableLog()
567583
const archive = new Zip(data)
568584
assert.include(archive.files['index.js'].name, 'index.js')
569585
assert.include(archive.files['bin/node-lambda'].name, 'bin/node-lambda')
@@ -597,7 +613,9 @@ describe('lib/main', function () {
597613
it('installs and zips with an index.js file and node_modules/aws-sdk (It is also a test of `_buildAndArchive`)', function () {
598614
_timeout({ this: this, sec: 30 }) // give it time to zip
599615

616+
disableLog()
600617
return lambda._archive(program).then((data) => {
618+
enableLog()
601619
const archive = new Zip(data)
602620
const contents = Object.keys(archive.files).map((k) => {
603621
return archive.files[k].name.toString()
@@ -620,7 +638,9 @@ describe('lib/main', function () {
620638
fs.writeFileSync(path.join(buildDir, 'd', 'testb'), '...')
621639

622640
program.prebuiltDirectory = buildDir
641+
disableLog()
623642
return lambda._archive(program).then((data) => {
643+
enableLog()
624644
const archive = new Zip(data)
625645
const contents = Object.keys(archive.files).map((k) => {
626646
return archive.files[k].name.toString()
@@ -642,7 +662,9 @@ describe('lib/main', function () {
642662
before(function () {
643663
_timeout({ this: this, sec: 30 }) // give it time to zip
644664

665+
disableLog()
645666
return lambda._zip(program, codeDirectory).then((data) => {
667+
enableLog()
646668
bufferExpected = data
647669
fs.writeFileSync(testZipFile, data)
648670
})
@@ -682,7 +704,9 @@ describe('lib/main', function () {
682704
const filePath = path.join(path.resolve('/aaaa'), 'bbbb')
683705
const _program = Object.assign({ deployZipfile: filePath }, program)
684706
_timeout({ this: this, sec: 30 }) // give it time to zip
707+
disableLog()
685708
return lambda._archive(_program).then((data) => {
709+
enableLog()
686710
// same test as "installs and zips with an index.js file and node_modules/aws-sdk"
687711
const archive = new Zip(data)
688712
const contents = Object.keys(archive.files).map((k) => {
@@ -736,7 +760,9 @@ describe('lib/main', function () {
736760
})
737761

738762
it('should create sample files', () => {
763+
disableLog()
739764
lambda.setup(program)
765+
enableLog()
740766

741767
const libPath = path.join(__dirname, '..', 'lib')
742768
targetFiles.forEach((targetFile) => {
@@ -1039,7 +1065,9 @@ describe('lib/main', function () {
10391065
program.eventFile = 'newEvent.json'
10401066
program.contextFile = 'newContext.json'
10411067

1068+
disableLog()
10421069
lambda.setup(program)
1070+
enableLog()
10431071

10441072
assert.equal(fs.readFileSync('newContext.json').toString(), '{"FOO"="bar"\n"BAZ"="bing"\n}')
10451073
assert.equal(fs.readFileSync('newEvent.json').toString(), '{"FOO"="bar"}')

0 commit comments

Comments
 (0)