Skip to content
This repository has been archived by the owner on Jul 2, 2020. It is now read-only.

Commit

Permalink
feat: test support cov (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
echosoar committed May 13, 2020
1 parent 7954ff1 commit b949b59
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 15 deletions.
2 changes: 2 additions & 0 deletions packages/faas-cli-plugin-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
"typings": "dist/index.d.ts",
"dependencies": {
"@midwayjs/fcli-command-core": "^0.2.94",
"fs-extra": "^9.0.0",
"globby": "^11.0.0",
"mocha": "^7.1.2",
"nyc": "^15.0.1",
"ts-node": "^8.9.1"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions packages/faas-cli-plugin-test/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export class TestPlugin extends BasePlugin {
env: process.env,
argv: Object.assign(process.argv, {
_: testFiles,
cov: this.options.cov,
nyc: '--reporter=json --reporter=lcov --reporter=text',
watch: options.watch,
extension: 'ts,js',
Expand Down
54 changes: 39 additions & 15 deletions packages/faas-cli-plugin-test/src/test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
import { fork } from 'child_process';
import * as globby from 'globby';
import { existsSync } from 'fs';
import { join } from 'path';
import { existsSync, remove } from 'fs-extra';
export class Test {
config: any;
argv: any;
public async run(config) {
this.config = config;
this.argv = config.argv;
const execArgv = this.argv.execArgv || [];
if (this.argv.typescript) {
execArgv.push('--require', require.resolve('ts-node/register'));
}

// if cov need not exists report dir
if (this.argv.cov) {
const coverageDir = join(this.config.cwd, 'coverage');
if (existsSync(coverageDir)) {
await remove(coverageDir);
}
const outputDir = join(this.config.cwd, 'node_modules/.nyc_output');
if (existsSync(outputDir)) {
await remove(outputDir);
}
}
const opt = {
cwd: this.config.cwd,
env: Object.assign(
Expand All @@ -23,13 +31,37 @@ export class Test {
),
execArgv,
};
const mochaFile = require.resolve('mocha/bin/_mocha');

// exec bin file
let binFile;
if (this.argv.cov) {
binFile = require.resolve('nyc/bin/nyc.js');
} else {
binFile = require.resolve('mocha/bin/_mocha');
}
const args = await this.formatTestArgs();
return this.forkNode(mochaFile, args, opt);
return this.forkNode(binFile, args, opt);
}

private async formatTestArgs() {
const argsPre = [];
const args = [];
if (this.argv.typescript) {
argsPre.push('--require', require.resolve('ts-node/register'));
}
if (this.argv.cov) {
if (this.argv.nyc) {
argsPre.push(...this.argv.nyc.split(' '));
argsPre.push('--temp-directory', './node_modules/.nyc_output');
}
if (this.argv.typescript) {
argsPre.push(`--extension`);
argsPre.push(`.ts`);
}
argsPre.push(require.resolve('mocha/bin/_mocha'));
} else if (this.argv.extension) {
args.push(`--extension=${this.argv.extension}`);
}
let timeout = this.argv.timeout || process.env.TEST_TIMEOUT || 60000;
if (process.env.JB_DEBUG_FILE) {
// --no-timeout
Expand All @@ -53,14 +85,6 @@ export class Test {
args.push(`--require=${requireItem}`);
});

if (this.argv.extension) {
args.push(`--extension=${this.argv.extension}`);
}

if (this.argv.nyc) {
args.push(`--nyc=${this.argv.nyc}`);
}

let pattern;

if (!pattern) {
Expand Down Expand Up @@ -93,7 +117,7 @@ export class Test {
if (existsSync(setupFile)) {
args.unshift(setupFile);
}
return args;
return argsPre.concat(args);
}

protected forkNode(modulePath, args = [], options: any = {}) {
Expand Down

0 comments on commit b949b59

Please sign in to comment.