From 25587a532b47b85af6248801e7568a75f22f4f18 Mon Sep 17 00:00:00 2001 From: Eugene Samonenko Date: Wed, 25 Mar 2020 02:49:21 -0400 Subject: [PATCH] (fix/test): wrap shell.grep bc it shouldn't always succeed - regular grep will have an error code if it failed to match, but shell.grep doesn't, at least not when silent (maybe it shouldn't be silent? that might be too noisy) - so add a wrapper that checks that stdout also has the pattern that was being matched (grep outputs line:pattern for each match) Co-Authored-By: Anton Gilgur --- test/integration/tsdx-build-withBabel.test.js | 20 +++++++++---------- test/utils/shell.js | 13 ++++++++++++ 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/test/integration/tsdx-build-withBabel.test.js b/test/integration/tsdx-build-withBabel.test.js index 502394544..518baff6a 100644 --- a/test/integration/tsdx-build-withBabel.test.js +++ b/test/integration/tsdx-build-withBabel.test.js @@ -1,7 +1,7 @@ const shell = require('shelljs'); const util = require('../utils/fixture'); -const { execWithCache } = require('../utils/shell'); +const { execWithCache, grep } = require('../utils/shell'); shell.config.silent = false; @@ -16,32 +16,32 @@ describe('integration :: tsdx build :: .babelrc.js', () => { }); it('should convert styled-components template tags', () => { - let output = execWithCache('node ../dist/index.js build'); + const output = execWithCache('node ../dist/index.js build'); expect(output.code).toBe(0); // from styled.h1` to styled.h1( - output = shell.grep(/styled.h1\(/, ['dist/build-withbabel.*.js']); - expect(output.code).toBe(0); + const matched = grep(/styled.h1\(/, ['dist/build-withbabel.*.js']); + expect(matched).toBeTruthy(); }); // TODO: make this test work by allowing customization of plugin order it.skip('should remove comments in the CSS', () => { - let output = execWithCache('node ../dist/index.js build'); + const output = execWithCache('node ../dist/index.js build'); expect(output.code).toBe(0); // the "should be removed" comment shouldn't be there (gets error code) - output = shell.grep(/should be removed/, ['dist/build-withbabel.*.js']); - expect(output.code).toBe(1); + const matched = grep(/should be removed/, ['dist/build-withbabel.*.js']); + expect(matched).toBeTruthy(); }); it('should add an import of regeneratorRuntime', () => { - let output = execWithCache('node ../dist/index.js build'); + const output = execWithCache('node ../dist/index.js build'); expect(output.code).toBe(0); - output = shell.grep(/@babel\/runtime\/regenerator/, [ + const matched = grep(/@babel\/runtime\/regenerator/, [ 'dist/build-withbabel.*.js', ]); - expect(output.code).toBe(0); + expect(matched).toBeTruthy(); }); it('should compile files into a dist directory', () => { diff --git a/test/utils/shell.js b/test/utils/shell.js index c539f1b00..c267ddc0b 100644 --- a/test/utils/shell.js +++ b/test/utils/shell.js @@ -24,6 +24,19 @@ function execWithCache(command, { noCache = false } = {}) { return output; } +// shelljs.grep wrapper +// @param {RegExp} pattern +// @param {string} fileName +// @returns {boolean} true if pattern has matches in file +function grep(pattern, fileName) { + const output = shell.grep(pattern, fileName); + // output.code is always 0 regardless of matched/unmatched patterns + // so need to test output.stdout + // https://github.com/jaredpalmer/tsdx/pull/525#discussion_r395571779 + return Boolean(output.stdout.match(pattern)); +} + module.exports = { execWithCache, + grep, };