UNPKG

1.85 kBPlain TextView Raw
1import { BasePlugin } from '@midwayjs/fcli-command-core';
2import { existsSync } from 'fs';
3import { join } from 'path';
4
5export class TestPlugin extends BasePlugin {
6 commands = {
7 test: {
8 usage: 'Test a Serverless service',
9 lifecycleEvents: ['test'],
10 options: {
11 cov: {
12 usage: 'get code coverage report',
13 shortcut: 'c',
14 },
15 watch: {
16 usage: 'watch',
17 shortcut: 'w',
18 },
19 reporter: {
20 usage: 'set mocha reporter',
21 shortcut: 'r',
22 },
23 file: {
24 usage: 'specify a test file',
25 shortcut: 'f',
26 },
27 },
28 },
29 };
30
31 hooks = {
32 'test:test': async () => {
33 const servicePath = this.core.config.servicePath;
34 let testFiles = [];
35 if (this.options.f) {
36 testFiles = [this.options.f];
37 this.core.cli.log(`Testing ${this.options.f}`);
38 } else {
39 this.core.cli.log('Testing all *.test.js/ts...');
40 }
41 const options = this.options;
42 const TestCommand = require('midway-bin/lib/cmd/test');
43 const CovCommand = require('midway-bin/lib/cmd/cov');
44 const co = require('co');
45 const Command = options.cov ? CovCommand : TestCommand;
46 const tester = new Command();
47 await co(function*() {
48 process.env.TS_NODE_FILES = 'true';
49 yield tester.run({
50 cwd: servicePath,
51 env: process.env,
52 argv: Object.assign(process.argv, {
53 _: testFiles,
54 nyc: '--reporter=json --reporter=lcov --reporter=text',
55 watch: options.watch,
56 extension: 'ts,js',
57 reporter: options.reporter,
58 typescript: existsSync(join(servicePath, 'tsconfig.json')),
59 }),
60 execArgv: process.execArgv,
61 });
62 });
63 },
64 };
65}