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

Commit

Permalink
fix: faas-cli test
Browse files Browse the repository at this point in the history
  • Loading branch information
echosoar committed Dec 25, 2019
1 parent 5391f8a commit 08b1722
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
2 changes: 2 additions & 0 deletions packages/faas-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
"@midwayjs/faas": "^0.1.5",
"@midwayjs/runtime-mock": "^0.1.7",
"@midwayjs/spec-builder": "^0.1.5",
"co": "^4.6.0",
"midway-bin": "^1.15.1",
"minimist": "^1.2.0",
"ts-node": "^8.5.2",
"urllib": "^2.34.1",
Expand Down
8 changes: 7 additions & 1 deletion packages/faas-cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { join } = require('path');
import { loadSpec } from './utils/loadSpec';
import CommandPlugin from './plugins/pluginManager';
import CommandInvoke from './plugins/invoke';
import CommandTest from './plugins/test';

const baseDir = process.cwd();
export * from './plugins/invoke/main';
Expand Down Expand Up @@ -37,6 +38,7 @@ export class Cli {
loadDefaultPlugin() {
this.loadCommandPlugin();
this.loadCommandInvoke();
this.loadCommandTest();
}

loadCommandPlugin() {
Expand All @@ -47,8 +49,12 @@ export class Cli {
this.core.addPlugin(CommandInvoke);
}

loadCommandTest() {
this.core.addPlugin(CommandTest);
}

loadPlatformPlugin() {
if (this.argv.skipPlatformPlugin) {
if (this.argv.skipPlatformPlugin || !this.spec.provider) {
return;
}
this.core.addPlugin('npm::serverless-midway-plugin');
Expand Down
57 changes: 57 additions & 0 deletions packages/faas-cli/src/plugins/test/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@

const BasePlugin = require('@midwayjs/command-core/dist/plugin');
import { TestCommand, CovCommand } from 'midway-bin';
import * as co from 'co';
import { existsSync } from 'fs';
import { join } from 'path';

export default class Test extends BasePlugin {
commands = {
test: {
usage: 'Test a Serverless service',
lifecycleEvents: [
'test',
],
options: {
cov: {
usage: 'get code coverage report',
shortcut: 'c'
},
watch: {
usage: 'watch',
shortcut: 'w'
},
reporter: {
usage: 'set mocha reporter',
shortcut: 'r'
}
},
},
};

hooks = {
'test:test': async () => {
this.core.cli.log('Testing all *.test.js/ts...');
const options = this.options;
const Command = options.cov ? CovCommand : TestCommand;
const servicePath = this.core.config.servicePath;
const tester = new Command();
await co(function* () {
process.env.TS_NODE_FILES = 'true';
yield tester.run({
cwd: servicePath,
env: process.env,
argv: Object.assign(process.argv, {
_: [],
nyc: '--reporter=json --reporter=lcov --reporter=text',
watch: options.watch,
extension: 'ts,js',
reporter: options.reporter,
typescript: existsSync(join(servicePath, 'tsconfig.json'))
}),
execArgv: process.execArgv
});
});
}
};
}
2 changes: 1 addition & 1 deletion packages/faas-cli/src/utils/loadSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const loadSpec = (baseDir) => {
'serverless.yaml',
].find(spec => existsSync(join(baseDir, spec)));
if (!specPath) {
throw new Error('need yml config');
return {};
}
return transform(specPath);
};

0 comments on commit 08b1722

Please sign in to comment.