Skip to content

Commit

Permalink
feat: support grep pattern in 6.x (#220)
Browse files Browse the repository at this point in the history
  • Loading branch information
whxaxes authored Feb 14, 2023
1 parent feaa839 commit 7fedc6d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
10 changes: 10 additions & 0 deletions src/cmd/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ export class TestCommand extends BaseCommand {
})
timeout: number | boolean;

@Option({
description: 'only run tests matching <pattern>',
alias: 'g',
type: 'string',
array: true,
default: [],
})
grep: string[];

@Option({
description: 'only test with changed files and match test/**/*.test.(js|ts), default is false',
alias: 'c',
Expand Down Expand Up @@ -176,6 +185,7 @@ export class TestCommand extends BaseCommand {
this.dryRun ? '--dry-run' : '',
// force exit
'--exit',
this.grep.map(pattern => `--grep='${pattern}'`).join(' '),
this.timeout === false ? '--no-timeout' : `--timeout ${this.timeout}`,
this.parallel ? '--parallel' : '',
this.parallel && this.jobs ? `--jobs ${this.jobs}` : '',
Expand Down
19 changes: 17 additions & 2 deletions test/cmd/cov.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,28 @@ describe('test/cmd/cov.test.ts', () => {
.end();
});

it('should grep pattern without error', () => {
return coffee.fork(eggBin, [ 'cov', 'test/a.test.js', '--grep', 'should success' ], {
cwd,
})
// .debug()
.expect('stdout', /should success/)
.expect('stdout', /a\.test\.js/)
.notExpect('stdout', /should show tmp/)
.expect('code', 0)
.end();
});

it('should fail when test fail', () => {
return coffee.fork(eggBin, [ 'cov' ], { cwd, env: { TESTS: 'test/fail.js' } })
// .debug()
.expect('stdout', /1\) should fail/)
.expect('stdout', /1 failing/)
.expect('code', 1)
.end();
.end((err, { stdout, code }) => {
assert(err);
assert(stdout.match(/AssertionError/));
assert(code === 1);
});
});

it('should run cov when no test files', () => {
Expand Down
12 changes: 12 additions & 0 deletions test/cmd/test.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ describe('test/cmd/test.test.ts', () => {
.end();
});

it('should grep pattern without error', () => {
return coffee.fork(eggBin, [ 'test', 'test/a.test.js', '--grep', 'should success' ], {
cwd,
})
// .debug()
.expect('stdout', /should success/)
.expect('stdout', /a\.test\.js/)
.notExpect('stdout', /should show tmp/)
.expect('code', 0)
.end();
});

it('should exit when not test files', () => {
return coffee.fork(eggBin, [ 'test', 'test/**/*.nth.js' ], { cwd })
// .debug()
Expand Down
14 changes: 10 additions & 4 deletions test/ts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ describe('test/ts.test.ts', () => {
return coffee.fork(eggBin, [ 'test', '--typescript' ], { cwd, env: { NODE_ENV: 'development' } })
// .debug()
.expect('stdout', /'egg from ts' == 'wrong assert ts'/)
.expect('code', 1)
.end();
.end((err, { stdout, code }) => {
assert(err);
assert(stdout.match(/AssertionError/));
assert(code === 1);
});
});

describe('real application', () => {
Expand Down Expand Up @@ -140,8 +143,11 @@ describe('test/ts.test.ts', () => {
.expect('stdout', /2 failing/)
.expect('stdout', /test[\/\\]index\.test\.ts:\d+:\d+\)/)
.expect('stdout', /AssertionError \[ERR_ASSERTION]: '111' == '222'/)
.expect('code', 1)
.end();
.end((err, { stdout, code }) => {
assert(err);
assert(stdout.match(/AssertionError/));
assert(code === 1);
});
});

it('should correct error stack line number in covering app', () => {
Expand Down

0 comments on commit 7fedc6d

Please sign in to comment.