Skip to content

Commit 6997b40

Browse files
ambroseusagilgur5
andcommitted
(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 <agilgur5@gmail.com>
1 parent ac7d235 commit 6997b40

2 files changed

Lines changed: 20 additions & 7 deletions

File tree

test/integration/tsdx-build-withBabel.test.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,28 @@ describe('integration :: tsdx build :: .babelrc.js', () => {
1515
});
1616

1717
it('should convert styled-components template tags', () => {
18-
let output = shell.exec('node ../dist/index.js build');
18+
const output = shell.exec('node ../dist/index.js build');
1919
expect(output.code).toBe(0);
2020

2121
// from styled.h1` to styled.h1(
22-
output = shell.grep(/styled.h1\(/, ['dist/build-withbabel.*.js']);
23-
expect(output.code).toBe(0);
22+
const matched = util.grep(/styled.h1\(/, ['dist/build-withbabel.*.js']);
23+
expect(matched).toBeTruthy();
2424
});
2525

2626
// TODO: make this test work by allowing customization of plugin order
2727
it.skip('should remove comments in the CSS', () => {
2828
// the "should be removed" comment shouldn't be there (gets error code)
29-
const output = shell.grep(/should be removed/, [
29+
const matched = util.grep(/should be removed/, [
3030
'dist/build-withbabel.*.js',
3131
]);
32-
expect(output.code).toBe(1);
32+
expect(matched).toBeTruthy();
3333
});
3434

3535
it('should add an import of regeneratorRuntime', () => {
36-
const output = shell.grep(/@babel\/runtime\/regenerator/, [
36+
const matched = util.grep(/@babel\/runtime\/regenerator/, [
3737
'dist/build-withbabel.*.js',
3838
]);
39-
expect(output.code).toBe(0);
39+
expect(matched).toBeTruthy();
4040
});
4141

4242
it('should compile files into a dist directory', () => {

test/util.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@ const rootDir = process.cwd();
44

55
shell.config.silent = true;
66

7+
// shelljs.grep wrapper
8+
// @param {RegExp} pattern
9+
// @param {string} fileName
10+
// @returns {boolean} true if pattern has matches in file
11+
function grep(pattern, fileName) {
12+
const output = shell.grep(pattern, fileName);
13+
// output.code is always 0 regardless of matched/unmatched patterns
14+
// so need to test output.stdout
15+
// https://github.com/jaredpalmer/tsdx/pull/525#discussion_r395571779
16+
return Boolean(output.stdout.match(pattern));
17+
}
18+
719
module.exports = {
820
setupStageWithFixture: (testDir, stageName, fixtureName) => {
921
const stagePath = path.join(rootDir, stageName);
@@ -24,5 +36,6 @@ module.exports = {
2436
shell.rm('-rf', path.join(rootDir, stageName));
2537
},
2638

39+
grep,
2740
rootDir,
2841
};

0 commit comments

Comments
 (0)