Skip to content

Commit

Permalink
(fix/test): wrap shell.grep bc it shouldn't always succeed
Browse files Browse the repository at this point in the history
- 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 <agilgur5@gmail.com>
  • Loading branch information
ambroseus and agilgur5 committed Mar 26, 2020
1 parent b00270b commit 2a04c68
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
20 changes: 10 additions & 10 deletions test/integration/tsdx-build-withBabel.test.js
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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', () => {
Expand Down
13 changes: 13 additions & 0 deletions test/utils/shell.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

0 comments on commit 2a04c68

Please sign in to comment.