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 25, 2020
1 parent ac7d235 commit 6997b40
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
14 changes: 7 additions & 7 deletions test/integration/tsdx-build-withBabel.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,28 @@ describe('integration :: tsdx build :: .babelrc.js', () => {
});

it('should convert styled-components template tags', () => {
let output = shell.exec('node ../dist/index.js build');
const output = shell.exec('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 = util.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', () => {
// the "should be removed" comment shouldn't be there (gets error code)
const output = shell.grep(/should be removed/, [
const matched = util.grep(/should be removed/, [
'dist/build-withbabel.*.js',
]);
expect(output.code).toBe(1);
expect(matched).toBeTruthy();
});

it('should add an import of regeneratorRuntime', () => {
const output = shell.grep(/@babel\/runtime\/regenerator/, [
const matched = util.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/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@ const rootDir = process.cwd();

shell.config.silent = true;

// 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 = {
setupStageWithFixture: (testDir, stageName, fixtureName) => {
const stagePath = path.join(rootDir, stageName);
Expand All @@ -24,5 +36,6 @@ module.exports = {
shell.rm('-rf', path.join(rootDir, stageName));
},

grep,
rootDir,
};

0 comments on commit 6997b40

Please sign in to comment.